mirror of
https://github.com/kdave/btrfs-progs
synced 2025-01-12 16:59:51 +00:00
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 <dsterba@suse.com>
This commit is contained in:
parent
908b4b4450
commit
fede0c5057
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user