From fede0c5057dcc2df1d43e4aa2b96cb9bd5ae2652 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Wed, 11 Jan 2023 18:06:41 +0100 Subject: [PATCH] btrfs-progs: help: add option text formatting infrastructure To make option formatting a bit easier so the spacing is unified add macros and formatting helpers. Usage in the help text: OPTLINE("-o value", "description") Internally the option and description are delimiters by chars that are not part of normal text, the formatter separates that and uses fixed with for output. The description text can be of any length, multi-line text should still end up as one token (i.e. newline without ',' between). Signed-off-by: David Sterba --- common/help.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ common/help.h | 13 +++++++++++++ 2 files changed, 64 insertions(+) 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