diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2022-12-31 16:53:39 +0000 |
---|---|---|
committer | Michael Drake <mdrake.unique@gmail.com> | 2023-01-01 18:02:44 +0000 |
commit | fde3d979ace97bd109fce443b49cbbfa5a9307a2 (patch) | |
tree | 3f17d6003d90f4321ad721bffdaf4eeae45cf184 /test/cli.c | |
parent | d63e9e479c63f8f0a1579ea58205862047297f1a (diff) | |
download | libnsgif-fde3d979ace97bd109fce443b49cbbfa5a9307a2.tar.gz |
cli: Improve help output for enum arguments
Diffstat (limited to 'test/cli.c')
-rw-r--r-- | test/cli.c | 96 |
1 files changed, 70 insertions, 26 deletions
@@ -641,46 +641,90 @@ static size_t cli__terminal_width(void) } /** - * Print an entry's description, with a given indent. + * Print a wrapped string, with a given indent. * * The indent is assumed to already be applied for the first line of the * output by the caller. - * - * \param[in] entry The entry to print the description for. + * + * \param[in] str The string to print. * \param[in] indent The number of spaces to pad the left margin with. */ -static void cli__print_description(const struct cli_table_entry *entry, - size_t indent) +static void cli__print_wrapping_string(const char *str, size_t indent) { size_t terminal_width = cli__terminal_width(); size_t avail = (indent > terminal_width) ? 0 : terminal_width - indent; size_t space = avail; - const char *desc = entry->d; - - if (desc != NULL) { - while (*desc != '\0') { - size_t word_len = strcspn(desc, " \n\t"); - if (word_len <= space || space == avail) { - fprintf(stderr, "%*.*s", - (int)word_len, - (int)word_len, desc); - desc += word_len; - if (word_len <= space) { - space -= word_len; - } - if (space > 0) { - fprintf(stderr, " "); - space--; - } - } else { - fprintf(stderr, "\n%*s", (int)indent, ""); - space = avail; + + while (*str != '\0') { + size_t word_len = strcspn(str, " \n\t"); + if (word_len <= space || space == avail) { + fprintf(stderr, "%*.*s", + (int)word_len, + (int)word_len, str); + str += word_len; + if (word_len <= space) { + space -= word_len; + } + if (space > 0) { + fprintf(stderr, " "); + space--; } - desc += strspn(desc, " \n\t"); + } else { + fprintf(stderr, "\n%*s", (int)indent, ""); + space = avail; } + str += strspn(str, " \n\t"); + } +} + +/** + * Print an entry's description, with a given indent. + * + * The indent is assumed to already be applied for the first line of the + * output by the caller. + * + * \param[in] entry The entry to print the description for. + * \param[in] indent The number of spaces to pad the left margin with. + */ +static void cli__print_description(const struct cli_table_entry *entry, + size_t indent) +{ + if (entry->d != NULL) { + cli__print_wrapping_string(entry->d, indent); } fprintf(stderr, "\n"); + + if (entry->t == CLI_ENUM) { + size_t max_len = 0; + + for (const struct cli_str_val *e = entry->v.e.desc; + e->str != NULL; e++) { + size_t len = strlen(e->str); + if (max_len < len) { + max_len = len; + } + } + + fprintf(stderr, "\n"); + + for (const struct cli_str_val *e = entry->v.e.desc; + e->str != NULL; e++) { + fprintf(stderr, " "); + + if (e->d == NULL || e->d[0] == '\0') { + fprintf(stderr, "%s\n", + e->str); + } else { + fprintf(stderr, "%-*s - ", + (int)(max_len), + e->str); + cli__print_wrapping_string(e->d, + 8 + max_len + 3); + fprintf(stderr, "\n"); + } + } + } } /* Documented in cli.h */ |