From: Tom Tromey Date: Mon, 3 Jun 2024 16:06:51 +0000 (-0600) Subject: Wrap help options when building help string X-Git-Tag: gdb-16-branchpoint~472 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a03d5b9bf375452fadb34b27638be3951c3f482e;p=thirdparty%2Fbinutils-gdb.git Wrap help options when building help string When building a help string, it's possible that the resulting options will go over 80 columns. This patch changes this code to add line wrapping where needed. This can most be seen by looking "help bt" and in particular the "-frame-info" help text. --- diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h index 7365c3f0157..7e25374fc80 100644 --- a/gdb/cli/cli-decode.h +++ b/gdb/cli/cli-decode.h @@ -29,6 +29,9 @@ #include "gdbsupport/intrusive_list.h" #include "gdbsupport/buildargv.h" +/* The allowed length of a line in a documentation string. */ +constexpr int cli_help_line_length = 80; + /* Not a set/show command. Note that some commands which begin with "set" or "show" might be in this category, if their syntax does not fall into one of the following categories. */ diff --git a/gdb/cli/cli-option.c b/gdb/cli/cli-option.c index 9eb9ff81154..fa00b913c99 100644 --- a/gdb/cli/cli-option.c +++ b/gdb/cli/cli-option.c @@ -732,52 +732,71 @@ process_options (const char **args, } } -/* Helper for build_help. Return a fragment of a help string showing - OPT's possible values. Returns NULL if OPT doesn't take an - argument. */ +/* Helper for build_help. Append a fragment of a help string showing + OPT's possible values. LEN_AT_START is the length of HELP at the + start of the current line. This is used when wrapping is + needed. */ -static const char * -get_val_type_str (const option_def &opt, std::string &buffer) +static void +append_val_type_str (std::string &help, const option_def &opt, + size_t len_at_start) { if (!opt.have_argument) - return nullptr; + return; switch (opt.type) { case var_boolean: - return "[on|off]"; + help += " [on|off]"; + break; case var_uinteger: case var_integer: case var_pinteger: { - buffer = "NUMBER"; + help += " NUMBER"; if (opt.extra_literals != nullptr) for (const literal_def *l = opt.extra_literals; l->literal != nullptr; l++) { - buffer += '|'; - buffer += l->literal; + help += '|'; + help += l->literal; } - return buffer.c_str (); } + break; case var_enum: { - buffer = ""; + help += ' '; + /* If wrapping is needed, subsequent lines will be indented + this amount. */ + size_t indent = help.length () - len_at_start; for (size_t i = 0; opt.enums[i] != nullptr; i++) { if (i != 0) - buffer += "|"; - buffer += opt.enums[i]; + { + size_t new_len = help.length () + 1 + strlen (opt.enums[i]); + + if (new_len - len_at_start >= cli_help_line_length) + { + help += "\n"; + len_at_start = help.length (); + + help.append (indent, ' '); + } + help += "|"; + } + help += opt.enums[i]; } - return buffer.c_str (); } + break; case var_string: - return "STRING"; + help += "STRING"; + break; case var_filename: - return "FILENAME"; + help += "FILENAME"; + break; default: - return nullptr; + break; } } @@ -815,15 +834,11 @@ build_help_option (gdb::array_view options, if (o.set_doc == nullptr) continue; + size_t initial_len = help.length (); help += " -"; help += o.name; - const char *val_type_str = get_val_type_str (o, buffer); - if (val_type_str != nullptr) - { - help += ' '; - help += val_type_str; - } + append_val_type_str (help, o, initial_len); help += "\n"; append_indented_doc (o.set_doc, help); if (o.help_doc != nullptr)