https://github.com/DavidSpickett created https://github.com/llvm/llvm-project/pull/178208
So I have an easier time fixing #177570. Changes I have made: * Added const to some variables. * Early return if we print a single line, and dedent the "else" that handles multiple lines. * Only convert lldb's short codes into ansi codes once. * Rename a couple of variables where they could have either referred to the visible text or the raw data with the ansi codes in. >From b107526897db4aa2ad583a74ed8327d2c36910a2 Mon Sep 17 00:00:00 2001 From: David Spickett <[email protected]> Date: Tue, 27 Jan 2026 13:42:14 +0000 Subject: [PATCH] [lldb] Refactor command option printing So I have an easier time fixing #177570. Changes I have made: * Added const to some variables. * Early return if we print a single line, and dedent the "else" that handles multiple lines. * Only convert lldb's short codes into ansi codes once. * Rename a couple of variables where they could have either referred to the visible text or the raw data with the ansi codes in. --- lldb/source/Interpreter/Options.cpp | 75 ++++++++++++++--------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/lldb/source/Interpreter/Options.cpp b/lldb/source/Interpreter/Options.cpp index eab452cddf3a5..baac1702f9083 100644 --- a/lldb/source/Interpreter/Options.cpp +++ b/lldb/source/Interpreter/Options.cpp @@ -266,8 +266,7 @@ void Options::OutputFormattedUsageText(Stream &strm, bool use_color) { std::string actual_text; if (option_def.validator) { - const char *condition = option_def.validator->ShortConditionString(); - if (condition) { + if (const char *condition = option_def.validator->ShortConditionString()) { actual_text = "["; actual_text.append(condition); actual_text.append("] "); @@ -278,50 +277,48 @@ void Options::OutputFormattedUsageText(Stream &strm, const size_t visible_length = ansi::ColumnWidth(actual_text); // Will it all fit on one line? - if (static_cast<uint32_t>(visible_length + strm.GetIndentLevel()) < output_max_columns) { // Output it as a single line. - strm.Indent(ansi::FormatAnsiTerminalCodes(actual_text, use_color)); + strm.Indent(actual_text); strm.EOL(); - } else { - // We need to break it up into multiple lines. - - int text_width = output_max_columns - strm.GetIndentLevel() - 1; - int start = 0; - int end = start; - int final_end = visible_length; - int sub_len; - - while (end < final_end) { - // Don't start the 'text' on a space, since we're already outputting the - // indentation. - while ((start < final_end) && (actual_text[start] == ' ')) - start++; - - end = start + text_width; - if (end > final_end) - end = final_end; - else { - // If we're not at the end of the text, make sure we break the line on - // white space. - while (end > start && actual_text[end] != ' ' && - actual_text[end] != '\t' && actual_text[end] != '\n') - end--; - } + return; + } - sub_len = end - start; - if (start != 0) - strm.EOL(); - strm.Indent(); - assert(start < final_end); - assert(start + sub_len <= final_end); - strm.PutCString(ansi::FormatAnsiTerminalCodes( - llvm::StringRef(actual_text.c_str() + start, sub_len), use_color)); - start = end + 1; + // We need to break it up into multiple lines. + + const int allowed_text_width = output_max_columns - strm.GetIndentLevel() - 1; + int start = 0; + int end = start; + const int final_end = visible_length; + + while (end < final_end) { + // Don't start the 'text' on a space, since we're already outputting the + // indentation. + while ((start < final_end) && (actual_text[start] == ' ')) + start++; + + end = start + allowed_text_width; + if (end > final_end) + end = final_end; + else { + // If we're not at the end of the text, make sure we break the line on + // white space. + while (end > start && actual_text[end] != ' ' && + actual_text[end] != '\t' && actual_text[end] != '\n') + end--; } - strm.EOL(); + + const int sub_len = end - start; + if (start != 0) + strm.EOL(); + strm.Indent(); + assert(start < final_end); + assert(start + sub_len <= final_end); + strm.PutCString(llvm::StringRef(actual_text.c_str() + start, sub_len)); + start = end + 1; } + strm.EOL(); } bool Options::SupportsLongOption(const char *long_option) { _______________________________________________ lldb-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
