Improve listing of long device names

This commit is contained in:
Martin Lund 2024-07-02 17:41:28 +02:00
parent 5f70b75e90
commit da04c2c444
3 changed files with 44 additions and 3 deletions

View file

@ -87,3 +87,26 @@ void print(const char *format, ...)
print_tainted = true;
}
void print_padded(char *string, size_t length, char pad_char)
{
size_t padding = 0;
size_t string_length = 0;
size_t i;
string_length = strlen(string);
if (string_length < length)
{
padding += length - string_length;
printf("%s", string);
for (i=0; i<padding; i++)
{
putchar(pad_char);
}
}
else
{
printf("%s", string);
}
}

View file

@ -140,3 +140,4 @@ void print_normal(char c);
void print_init_ansi_formatting(void);
void tio_printf_array(const char *array);
void print_tainted_set(void);
void print_padded(char *string, size_t length, char pad_char);

View file

@ -172,6 +172,7 @@ static pthread_t thread;
static int pipefd[2];
static pthread_mutex_t mutex_input_ready = PTHREAD_MUTEX_INITIALIZER;
static char line[PATH_MAX];
static size_t listing_device_name_length_max = 0;
static void optional_local_echo(char c)
{
@ -1627,6 +1628,9 @@ static void search_reset(void)
// Indicate an empty list
device_list = NULL;
// Reset max device name length
listing_device_name_length_max = 0;
}
#if defined(__linux__)
@ -1793,6 +1797,12 @@ GList *tty_search_for_serial_devices(void)
// Add device information to device list
device_list = g_list_append(device_list, device);
// Update length of longest device name string
if (strlen(device->path) > listing_device_name_length_max)
{
listing_device_name_length_max = strlen(device->path);
}
}
if (g_list_length(device_list) == 0)
@ -1897,8 +1907,14 @@ void list_serial_devices(void)
if (g_list_length(device_list) > 0)
{
printf("Device TID Uptime [s] Driver Description\n");
printf("----------------- ---- ------------- ---------------- --------------------------\n");
if (listing_device_name_length_max < 17)
{
listing_device_name_length_max = 17;
}
print_padded("Device", listing_device_name_length_max, ' ');
printf(" TID Uptime [s] Driver Description\n");
print_padded("", listing_device_name_length_max, '-');
printf(" ---- ------------- ---------------- --------------------------\n");
// Iterate through the device list
GList *iter;
@ -1907,7 +1923,8 @@ void list_serial_devices(void)
device_t *device = (device_t *) iter->data;
// Print device information
printf("%-17s %4s %13.3f %-16s %s\n", device->path, device->tid, device->uptime, device->driver, device->description);
print_padded(device->path, listing_device_name_length_max, ' ');
printf(" %4s %13.3f %-16s %s\n", device->tid, device->uptime, device->driver, device->description);
}
printf("\n");
}