diff --git a/common/help.c b/common/help.c index f806ff31..fa2984e8 100644 --- a/common/help.c +++ b/common/help.c @@ -141,6 +141,38 @@ const char *output_format_name(unsigned int value) return "UNKNOWN"; } +static void hpad(int len, FILE *outf) +{ + while (len-- > 0) + fputc(' ', outf); +} + +static void format_text(const char *line, FILE *outf) +{ + int i; + + i = 0; + while (*line) { + while (*line && *line == ' ') + line++; + while (*line && *line != ' ') { + fputc(*line, outf); + line++; + i++; + } + if (i > HELPINFO_DESC_WIDTH) { + if (*line) { + fputc('\n', outf); + line++; + hpad(HELPINFO_DESC_PREFIX, outf); + } + i = 0; + } else { + hpad(1, outf); + } + } +} + static int do_usage_one_command(const char * const *usagestr, unsigned int flags, unsigned int cmd_flags, FILE *outf) @@ -223,6 +255,25 @@ static int do_usage_one_command(const char * const *usagestr, output_formats[i].name); } fputc('\n', outf); + } else if (*usagestr[0] == HELPINFO_OPTION[0]) { + const char *tmp = *usagestr + 1; + const char *text_marker = strchr(*usagestr, HELPINFO_DESC[0]); + const char *text = text_marker + 1; + int optlen = (int)(text_marker - tmp - 1); + + hpad(HELPINFO_PREFIX_WIDTH, outf); + while (tmp < text_marker) + fputc(*tmp++, outf); + + if (optlen > HELPINFO_OPTION_WIDTH) { + fputc('\n', outf); + hpad(HELPINFO_DESC_PREFIX, outf); + } else { + hpad(HELPINFO_OPTION_WIDTH + HELPINFO_OPTION_MARGIN - optlen - 1, + outf); + } + format_text(text, outf); + fputc('\n', outf); } else { fprintf(outf, "%*s%s\n", pad, "", *usagestr); } diff --git a/common/help.h b/common/help.h index f33cfc96..02286847 100644 --- a/common/help.h +++ b/common/help.h @@ -61,6 +61,19 @@ struct cmd_group; "-g|--gbytes show sizes in GiB, or GB with --si", \ "-t|--tbytes show sizes in TiB, or TB with --si" +#define HELPINFO_OPTION "\x01" +#define HELPINFO_DESC "\x02" +/* Keep the line length below 100 chars. */ +#define HELPINFO_PREFIX_WIDTH 4 +#define HELPINFO_LISTING_WIDTH 8 +#define HELPINFO_OPTION_WIDTH 24 +#define HELPINFO_OPTION_MARGIN 2 +#define HELPINFO_DESC_PREFIX (HELPINFO_PREFIX_WIDTH + \ + HELPINFO_OPTION_WIDTH + \ + HELPINFO_OPTION_MARGIN) +#define HELPINFO_DESC_WIDTH 99 - HELPINFO_DESC_PREFIX +#define OPTLINE(opt, text) HELPINFO_OPTION opt HELPINFO_DESC text + /* * Special marker in the help strings that will preemptively insert the global * options and then continue with the following text that possibly follows