From 720768ad02bbaa911543ffe902ae388f904ab92b Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sat, 23 Jan 2010 17:30:24 +0000 Subject: [PATCH] automatically align the ouptut of various lists to the right sizes git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1705 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- urjtag/ChangeLog | 8 ++++++++ urjtag/src/cmd/cmd.h | 12 ++++++++---- urjtag/src/cmd/cmd_cable.c | 10 +++++----- urjtag/src/cmd/cmd_eraseflash.c | 10 +++++----- urjtag/src/cmd/cmd_flashmem.c | 8 ++++---- urjtag/src/cmd/cmd_help.c | 6 ++++-- urjtag/src/cmd/cmd_initbus.c | 10 +++++----- 7 files changed, 39 insertions(+), 25 deletions(-) diff --git a/urjtag/ChangeLog b/urjtag/ChangeLog index 92df11fb..e815098a 100644 --- a/urjtag/ChangeLog +++ b/urjtag/ChangeLog @@ -17,6 +17,14 @@ src/cmd/cmd_help.c (cmd_help_run), src/cmd/cmd_cable.c (cmd_cable_help): Automatically sort lists before displaying them. + * src/cmd/cmd.h (urj_cmd_sort): Automatically calculate the longest name in + the array of structures. + * src/cmd/cmd_eraseflash.c (cmd_eraseflash_help), src/cmd/cmd_flashmem.c + (cmd_flashmem_help), src/cmd/cmd_initbus.c (cmd_initbus_help), + src/cmd/cmd_help.c (cmd_help_run), src/cmd/cmd_cable.c (cmd_cable_help): + Use the length to automatically align output correctly. Standardize + gettext handling of strings here too (only translate descriptions). + 2010-01-22 Mike Frysinger * src/cmd/cmd_bit.c (cmd_bit_print_params): Fix strncat length handling. diff --git a/urjtag/src/cmd/cmd.h b/urjtag/src/cmd/cmd.h index 993fcdc8..8012bcab 100644 --- a/urjtag/src/cmd/cmd.h +++ b/urjtag/src/cmd/cmd.h @@ -72,14 +72,18 @@ int urj_cmd_params (char *params[]); int urj_cmd_get_number (const char *s, long unsigned *i); /** - * Internal command to help with sorting structures + * Internal command to help with sorting structures for display */ int _urj_cmd_alphasort (const void *a, const void *b); -#define urj_cmd_sort(arr) \ +#define urj_cmd_sort(arr, max_len) \ do { \ size_t nmemb; \ - for (nmemb = 0; (arr)[nmemb]; ++nmemb) \ - continue; \ + max_len = 0; \ + for (nmemb = 0; (arr)[nmemb]; ++nmemb) { \ + int this_len = strlen(*(char **)((arr)[nmemb])); \ + if (max_len < this_len) \ + max_len = this_len; \ + } \ qsort (arr, nmemb, sizeof(*arr), _urj_cmd_alphasort); \ } while (0) diff --git a/urjtag/src/cmd/cmd_cable.c b/urjtag/src/cmd/cmd_cable.c index f503db52..0adee7fe 100644 --- a/urjtag/src/cmd/cmd_cable.c +++ b/urjtag/src/cmd/cmd_cable.c @@ -161,7 +161,7 @@ cmd_cable_run (urj_chain_t *chain, char *params[]) static void cmd_cable_help (void) { - int i; + int i, pad; urj_log (URJ_LOG_LEVEL_NORMAL, _("Usage: %s DRIVER [DRIVER_OPTS]\n" @@ -174,11 +174,11 @@ cmd_cable_help (void) "\n" "List of supported cables:\n"), "cable"); - urj_cmd_sort (urj_tap_cable_drivers); + urj_cmd_sort (urj_tap_cable_drivers, pad); for (i = 0; urj_tap_cable_drivers[i]; i++) - urj_log (URJ_LOG_LEVEL_NORMAL, - _("%-13s %s\n"), urj_tap_cable_drivers[i]->name, - _(urj_tap_cable_drivers[i]->description)); + urj_log (URJ_LOG_LEVEL_NORMAL, "%-*s %s\n", pad + 1, + urj_tap_cable_drivers[i]->name, + _(urj_tap_cable_drivers[i]->description)); } const urj_cmd_t urj_cmd_cable = { diff --git a/urjtag/src/cmd/cmd_eraseflash.c b/urjtag/src/cmd/cmd_eraseflash.c index 9f9e7afc..e4f8ea88 100644 --- a/urjtag/src/cmd/cmd_eraseflash.c +++ b/urjtag/src/cmd/cmd_eraseflash.c @@ -68,7 +68,7 @@ cmd_eraseflash_run (urj_chain_t *chain, char *params[]) static void cmd_eraseflash_help (void) { - int i; + int i, pad; urj_log (URJ_LOG_LEVEL_NORMAL, _("Usage: %s ADDR BLOCKS\n" @@ -81,11 +81,11 @@ cmd_eraseflash_help (void) "\n" "Supported Flash Memories:\n"), "eraseflash"); - urj_cmd_sort (urj_flash_flash_drivers); + urj_cmd_sort (urj_flash_flash_drivers, pad); for (i = 0; urj_flash_flash_drivers[i]; i++) - urj_log (URJ_LOG_LEVEL_NORMAL, - _("%s\n %s\n"), _(urj_flash_flash_drivers[i]->name), - _(urj_flash_flash_drivers[i]->description)); + urj_log (URJ_LOG_LEVEL_NORMAL, "%-*s %s\n", pad + 1, + urj_flash_flash_drivers[i]->name, + _(urj_flash_flash_drivers[i]->description)); } const urj_cmd_t urj_cmd_eraseflash = { diff --git a/urjtag/src/cmd/cmd_flashmem.c b/urjtag/src/cmd/cmd_flashmem.c index ca290cf1..d619acc5 100644 --- a/urjtag/src/cmd/cmd_flashmem.c +++ b/urjtag/src/cmd/cmd_flashmem.c @@ -90,7 +90,7 @@ cmd_flashmem_run (urj_chain_t *chain, char *params[]) static void cmd_flashmem_help (void) { - int i; + int i, pad; urj_log (URJ_LOG_LEVEL_NORMAL, _("Usage: %s ADDR FILENAME [noverify]\n" @@ -107,10 +107,10 @@ cmd_flashmem_help (void) "Supported Flash Memories:\n"), "flashmem", "flashmem msbin", "msbin", "noverify"); - urj_cmd_sort (urj_flash_flash_drivers); + urj_cmd_sort (urj_flash_flash_drivers, pad); for (i = 0; urj_flash_flash_drivers[i]; i++) - urj_log (URJ_LOG_LEVEL_NORMAL, - _("%s\n %s\n"), _(urj_flash_flash_drivers[i]->name), + urj_log (URJ_LOG_LEVEL_NORMAL, "%-*s %s\n", pad + 1, + urj_flash_flash_drivers[i]->name, _(urj_flash_flash_drivers[i]->description)); } diff --git a/urjtag/src/cmd/cmd_help.c b/urjtag/src/cmd/cmd_help.c index e8acfb5d..77475542 100644 --- a/urjtag/src/cmd/cmd_help.c +++ b/urjtag/src/cmd/cmd_help.c @@ -48,10 +48,12 @@ cmd_help_run (urj_chain_t *chain, char *params[]) /* short description generation */ if (urj_cmd_params (params) == 1) { - urj_cmd_sort (urj_cmds); + int pad; + urj_cmd_sort (urj_cmds, pad); urj_log (URJ_LOG_LEVEL_NORMAL, _("Command list:\n\n")); for (i = 0; urj_cmds[i]; i++) - urj_log (URJ_LOG_LEVEL_NORMAL, _("%-13s %s\n"), urj_cmds[i]->name, + urj_log (URJ_LOG_LEVEL_NORMAL, "%-*s %s\n", pad + 1, + urj_cmds[i]->name, urj_cmds[i]->desc ? _(urj_cmds[i]->desc) : _("(no description available)")); urj_log (URJ_LOG_LEVEL_NORMAL, diff --git a/urjtag/src/cmd/cmd_initbus.c b/urjtag/src/cmd/cmd_initbus.c index ded259ed..fbb4100f 100644 --- a/urjtag/src/cmd/cmd_initbus.c +++ b/urjtag/src/cmd/cmd_initbus.c @@ -90,7 +90,7 @@ cmd_initbus_run (urj_chain_t *chain, char *params[]) static void cmd_initbus_help (void) { - int i; + int i, pad; urj_log (URJ_LOG_LEVEL_NORMAL, _("Usage: %s BUSNAME\n" @@ -100,11 +100,11 @@ cmd_initbus_help (void) "\n" "List of available buses:\n"), "initbus"); - urj_cmd_sort (urj_bus_drivers); + urj_cmd_sort (urj_bus_drivers, pad); for (i = 0; urj_bus_drivers[i] != NULL; i++) - urj_log (URJ_LOG_LEVEL_NORMAL, - _("%-10s %s\n"), urj_bus_drivers[i]->name, - urj_bus_drivers[i]->description); + urj_log (URJ_LOG_LEVEL_NORMAL, "%-*s %s\n", pad + 1, + urj_bus_drivers[i]->name, + _(urj_bus_drivers[i]->description)); } const const urj_cmd_t urj_cmd_initbus = {