Skip to content

Commit 6ebbb1f

Browse files
committed
[lldb] Simplify color logic in (IOHandler)Editline (NFC)
This patch simplifies the color handling logic in Editline and IOHandlerEditline: - Remove the m_color_prompts property from Editline and use the prompt ANSI prefix and suffix as the single source of truth. This avoids having to redraw the prompt unnecessarily, for example when colors are enabled but the prompt prefix and suffix are empty. - Rename m_color_prompts to just m_color in IOHandlerEditline and use it to ensure consistency between colored prompts and colored auto-suggestions. Some IOHandler explicitly turn off colors (such as IOHandlerConfirm) and it doesn't really make sense to have one or the other.
1 parent 17114f8 commit 6ebbb1f

File tree

5 files changed

+27
-31
lines changed

5 files changed

+27
-31
lines changed

lldb/include/lldb/Core/IOHandler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class IOHandlerEditline : public IOHandler {
328328
IOHandlerEditline(Debugger &debugger, IOHandler::Type type,
329329
const char *editline_name, // Used for saving history files
330330
llvm::StringRef prompt, llvm::StringRef continuation_prompt,
331-
bool multi_line, bool color_prompts,
331+
bool multi_line, bool color,
332332
uint32_t line_number_start, // If non-zero show line numbers
333333
// starting at
334334
// 'line_number_start'
@@ -340,7 +340,7 @@ class IOHandlerEditline : public IOHandler {
340340
const lldb::StreamFileSP &error_sp, uint32_t flags,
341341
const char *editline_name, // Used for saving history files
342342
llvm::StringRef prompt, llvm::StringRef continuation_prompt,
343-
bool multi_line, bool color_prompts,
343+
bool multi_line, bool color,
344344
uint32_t line_number_start, // If non-zero show line numbers
345345
// starting at
346346
// 'line_number_start'
@@ -432,7 +432,7 @@ class IOHandlerEditline : public IOHandler {
432432
uint32_t m_base_line_number; // If non-zero, then show line numbers in prompt
433433
uint32_t m_curr_line_idx;
434434
bool m_multi_line;
435-
bool m_color_prompts;
435+
bool m_color;
436436
bool m_interrupt_exits;
437437
std::string m_line_buffer;
438438
};

lldb/include/lldb/Host/Editline.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ using namespace line_editor;
153153
class Editline {
154154
public:
155155
Editline(const char *editor_name, FILE *input_file, FILE *output_file,
156-
FILE *error_file, std::recursive_mutex &output_mutex,
157-
bool color_prompts);
156+
FILE *error_file, std::recursive_mutex &output_mutex);
158157

159158
~Editline();
160159

@@ -371,7 +370,6 @@ class Editline {
371370
bool m_multiline_enabled = false;
372371
std::vector<EditLineStringType> m_input_lines;
373372
EditorStatus m_editor_status;
374-
bool m_color_prompts = true;
375373
int m_terminal_width = 0;
376374
int m_base_line_number = 0;
377375
unsigned m_current_line_index = 0;

lldb/source/Core/IOHandler.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ IOHandlerEditline::IOHandlerEditline(
226226
Debugger &debugger, IOHandler::Type type,
227227
const char *editline_name, // Used for saving history files
228228
llvm::StringRef prompt, llvm::StringRef continuation_prompt,
229-
bool multi_line, bool color_prompts, uint32_t line_number_start,
229+
bool multi_line, bool color, uint32_t line_number_start,
230230
IOHandlerDelegate &delegate)
231231
: IOHandlerEditline(debugger, type,
232232
FileSP(), // Inherit input from top input reader
233233
StreamFileSP(), // Inherit output from top input reader
234234
StreamFileSP(), // Inherit error from top input reader
235235
0, // Flags
236236
editline_name, // Used for saving history files
237-
prompt, continuation_prompt, multi_line, color_prompts,
237+
prompt, continuation_prompt, multi_line, color,
238238
line_number_start, delegate) {}
239239

240240
IOHandlerEditline::IOHandlerEditline(
@@ -243,16 +243,16 @@ IOHandlerEditline::IOHandlerEditline(
243243
uint32_t flags,
244244
const char *editline_name, // Used for saving history files
245245
llvm::StringRef prompt, llvm::StringRef continuation_prompt,
246-
bool multi_line, bool color_prompts, uint32_t line_number_start,
246+
bool multi_line, bool color, uint32_t line_number_start,
247247
IOHandlerDelegate &delegate)
248248
: IOHandler(debugger, type, input_sp, output_sp, error_sp, flags),
249249
#if LLDB_ENABLE_LIBEDIT
250250
m_editline_up(),
251251
#endif
252252
m_delegate(delegate), m_prompt(), m_continuation_prompt(),
253253
m_current_lines_ptr(nullptr), m_base_line_number(line_number_start),
254-
m_curr_line_idx(UINT32_MAX), m_multi_line(multi_line),
255-
m_color_prompts(color_prompts), m_interrupt_exits(true) {
254+
m_curr_line_idx(UINT32_MAX), m_multi_line(multi_line), m_color(color),
255+
m_interrupt_exits(true) {
256256
SetPrompt(prompt);
257257

258258
#if LLDB_ENABLE_LIBEDIT
@@ -262,9 +262,9 @@ IOHandlerEditline::IOHandlerEditline(
262262
m_input_sp && m_input_sp->GetIsRealTerminal();
263263

264264
if (use_editline) {
265-
m_editline_up = std::make_unique<Editline>(
266-
editline_name, GetInputFILE(), GetOutputFILE(), GetErrorFILE(),
267-
GetOutputMutex(), m_color_prompts);
265+
m_editline_up = std::make_unique<Editline>(editline_name, GetInputFILE(),
266+
GetOutputFILE(), GetErrorFILE(),
267+
GetOutputMutex());
268268
m_editline_up->SetIsInputCompleteCallback(
269269
[this](Editline *editline, StringList &lines) {
270270
return this->IsInputCompleteCallback(editline, lines);
@@ -278,10 +278,12 @@ IOHandlerEditline::IOHandlerEditline(
278278
m_editline_up->SetSuggestionCallback([this](llvm::StringRef line) {
279279
return this->SuggestionCallback(line);
280280
});
281-
m_editline_up->SetSuggestionAnsiPrefix(ansi::FormatAnsiTerminalCodes(
282-
debugger.GetAutosuggestionAnsiPrefix()));
283-
m_editline_up->SetSuggestionAnsiSuffix(ansi::FormatAnsiTerminalCodes(
284-
debugger.GetAutosuggestionAnsiSuffix()));
281+
if (m_color) {
282+
m_editline_up->SetSuggestionAnsiPrefix(ansi::FormatAnsiTerminalCodes(
283+
debugger.GetAutosuggestionAnsiPrefix()));
284+
m_editline_up->SetSuggestionAnsiSuffix(ansi::FormatAnsiTerminalCodes(
285+
debugger.GetAutosuggestionAnsiSuffix()));
286+
}
285287
}
286288
// See if the delegate supports fixing indentation
287289
const char *indent_chars = delegate.IOHandlerGetFixIndentationCharacters();
@@ -476,7 +478,7 @@ bool IOHandlerEditline::SetPrompt(llvm::StringRef prompt) {
476478
#if LLDB_ENABLE_LIBEDIT
477479
if (m_editline_up) {
478480
m_editline_up->SetPrompt(m_prompt.empty() ? nullptr : m_prompt.c_str());
479-
if (m_debugger.GetUseColor()) {
481+
if (m_color) {
480482
m_editline_up->SetPromptAnsiPrefix(
481483
ansi::FormatAnsiTerminalCodes(m_debugger.GetPromptAnsiPrefix()));
482484
m_editline_up->SetPromptAnsiSuffix(

lldb/source/Host/common/Editline.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ int Editline::GetCharacter(EditLineGetCharType *c) {
622622
}
623623

624624
const char *Editline::Prompt() {
625-
if (m_color_prompts)
625+
if (!m_prompt_ansi_prefix.empty() || !m_prompt_ansi_suffix.empty())
626626
m_needs_prompt_repaint = true;
627627
return m_current_prompt.c_str();
628628
}
@@ -1084,13 +1084,9 @@ unsigned char Editline::TypedCharacter(int ch) {
10841084
llvm::StringRef line(line_info->buffer,
10851085
line_info->lastchar - line_info->buffer);
10861086

1087-
const char *ansi_prefix =
1088-
m_color_prompts ? m_suggestion_ansi_prefix.c_str() : "";
1089-
const char *ansi_suffix =
1090-
m_color_prompts ? m_suggestion_ansi_suffix.c_str() : "";
1091-
10921087
if (std::optional<std::string> to_add = m_suggestion_callback(line)) {
1093-
std::string to_add_color = ansi_prefix + to_add.value() + ansi_suffix;
1088+
std::string to_add_color =
1089+
m_suggestion_ansi_prefix + to_add.value() + m_suggestion_ansi_suffix;
10941090
fputs(typed.c_str(), m_output_file);
10951091
fputs(to_add_color.c_str(), m_output_file);
10961092
size_t new_autosuggestion_size = line.size() + to_add->length();
@@ -1381,10 +1377,10 @@ Editline *Editline::InstanceFor(EditLine *editline) {
13811377

13821378
Editline::Editline(const char *editline_name, FILE *input_file,
13831379
FILE *output_file, FILE *error_file,
1384-
std::recursive_mutex &output_mutex, bool color_prompts)
1385-
: m_editor_status(EditorStatus::Complete), m_color_prompts(color_prompts),
1386-
m_input_file(input_file), m_output_file(output_file),
1387-
m_error_file(error_file), m_input_connection(fileno(input_file), false),
1380+
std::recursive_mutex &output_mutex)
1381+
: m_editor_status(EditorStatus::Complete), m_input_file(input_file),
1382+
m_output_file(output_file), m_error_file(error_file),
1383+
m_input_connection(fileno(input_file), false),
13881384
m_output_mutex(output_mutex) {
13891385
// Get a shared history instance
13901386
m_editor_name = (editline_name == nullptr) ? "lldb-tmp" : editline_name;

lldb/unittests/Editline/EditlineTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ EditlineAdapter::EditlineAdapter()
118118
// Create an Editline instance.
119119
_editline_sp.reset(new lldb_private::Editline(
120120
"gtest editor", *_el_secondary_file, *_el_secondary_file,
121-
*_el_secondary_file, output_mutex, false));
121+
*_el_secondary_file, output_mutex));
122122
_editline_sp->SetPrompt("> ");
123123

124124
// Hookup our input complete callback.

0 commit comments

Comments
 (0)