From da04c2c444b2ed9ca84763cd27808e430d318576 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Tue, 2 Jul 2024 17:41:28 +0200 Subject: [PATCH] Improve listing of long device names --- src/print.c | 23 +++++++++++++++++++++++ src/print.h | 1 + src/tty.c | 23 ++++++++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/print.c b/src/print.c index ab88ef9..06f7e73 100644 --- a/src/print.c +++ b/src/print.c @@ -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; ipath) > 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"); }