Skip to content

Commit d7d1706

Browse files
[lldb] Fix use-color settings not persistent (llvm#135626)
Fixes llvm#22981 If `settings set use-color` is changed when lldb is running it does not take effect. This is fixes that. --------- Signed-off-by: Ebuka Ezike <[email protected]> Co-authored-by: Jonas Devlieghere <[email protected]>
1 parent a095ebc commit d7d1706

File tree

8 files changed

+75
-7
lines changed

8 files changed

+75
-7
lines changed

lldb/include/lldb/Core/IOHandler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ class IOHandler {
9999
// Prompt support isn't mandatory
100100
return false;
101101
}
102+
103+
virtual bool SetUseColor(bool use_color) {
104+
// Color support isn't mandatory.
105+
return false;
106+
};
107+
102108
bool SetPrompt(const char *) = delete;
103109

104110
virtual llvm::StringRef GetControlSequence(char ch) { return {}; }
@@ -375,6 +381,8 @@ class IOHandlerEditline : public IOHandler {
375381
bool SetPrompt(llvm::StringRef prompt) override;
376382
bool SetPrompt(const char *prompt) = delete;
377383

384+
bool SetUseColor(bool use_color) override;
385+
378386
const char *GetContinuationPrompt();
379387

380388
void SetContinuationPrompt(llvm::StringRef prompt);

lldb/include/lldb/Host/Editline.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ class Editline {
168168
DisplayCompletions(Editline &editline,
169169
llvm::ArrayRef<CompletionResult::Completion> results);
170170

171+
/// Sets if editline should use color.
172+
void UseColor(bool use_color);
173+
171174
/// Sets a string to be used as a prompt, or combined with a line number to
172175
/// form a prompt.
173176
void SetPrompt(const char *prompt);
@@ -223,21 +226,29 @@ class Editline {
223226
void SetPromptAnsiPrefix(std::string prefix) {
224227
if (m_color)
225228
m_prompt_ansi_prefix = std::move(prefix);
229+
else
230+
m_prompt_ansi_prefix.clear();
226231
}
227232

228233
void SetPromptAnsiSuffix(std::string suffix) {
229234
if (m_color)
230235
m_prompt_ansi_suffix = std::move(suffix);
236+
else
237+
m_prompt_ansi_suffix.clear();
231238
}
232239

233240
void SetSuggestionAnsiPrefix(std::string prefix) {
234241
if (m_color)
235242
m_suggestion_ansi_prefix = std::move(prefix);
243+
else
244+
m_suggestion_ansi_prefix.clear();
236245
}
237246

238247
void SetSuggestionAnsiSuffix(std::string suffix) {
239248
if (m_color)
240249
m_suggestion_ansi_suffix = std::move(suffix);
250+
else
251+
m_suggestion_ansi_suffix.clear();
241252
}
242253

243254
/// Prompts for and reads a single line of user input.

lldb/include/lldb/Interpreter/CommandInterpreter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ class CommandInterpreter : public Broadcaster,
476476

477477
void UpdatePrompt(llvm::StringRef prompt);
478478

479+
void UpdateUseColor(bool use_color);
480+
479481
bool Confirm(llvm::StringRef message, bool default_answer);
480482

481483
void LoadCommandDictionary();

lldb/source/Core/Debugger.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,16 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
237237
CommandInterpreter::eBroadcastBitResetPrompt, bytes.release());
238238
GetCommandInterpreter().BroadcastEvent(prompt_change_event_sp);
239239
} else if (property_path == g_debugger_properties[ePropertyUseColor].name) {
240-
// use-color changed. Ping the prompt so it can reset the ansi terminal
241-
// codes.
242-
SetPrompt(GetPrompt());
240+
// use-color changed. set use-color, this also pings the prompt so it can
241+
// reset the ansi terminal codes.
242+
SetUseColor(GetUseColor());
243243
} else if (property_path ==
244244
g_debugger_properties[ePropertyPromptAnsiPrefix].name ||
245245
property_path ==
246246
g_debugger_properties[ePropertyPromptAnsiSuffix].name) {
247-
// Prompt colors changed. Ping the prompt so it can reset the ansi
248-
// terminal codes.
249-
SetPrompt(GetPrompt());
247+
// Prompt color changed. set use-color, this also pings the prompt so it
248+
// can reset the ansi terminal codes.
249+
SetUseColor(GetUseColor());
250250
} else if (property_path ==
251251
g_debugger_properties[ePropertyShowStatusline].name) {
252252
// Statusline setting changed. If we have a statusline instance, update it
@@ -455,6 +455,8 @@ bool Debugger::GetUseColor() const {
455455
bool Debugger::SetUseColor(bool b) {
456456
const uint32_t idx = ePropertyUseColor;
457457
bool ret = SetPropertyAtIndex(idx, b);
458+
459+
GetCommandInterpreter().UpdateUseColor(b);
458460
SetPrompt(GetPrompt());
459461
return ret;
460462
}

lldb/source/Core/IOHandler.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,21 @@ bool IOHandlerEditline::SetPrompt(llvm::StringRef prompt) {
476476
return true;
477477
}
478478

479+
bool IOHandlerEditline::SetUseColor(bool use_color) {
480+
m_color = use_color;
481+
482+
#if LLDB_ENABLE_LIBEDIT
483+
if (m_editline_up) {
484+
m_editline_up->UseColor(use_color);
485+
m_editline_up->SetSuggestionAnsiPrefix(ansi::FormatAnsiTerminalCodes(
486+
m_debugger.GetAutosuggestionAnsiPrefix()));
487+
m_editline_up->SetSuggestionAnsiSuffix(ansi::FormatAnsiTerminalCodes(
488+
m_debugger.GetAutosuggestionAnsiSuffix()));
489+
}
490+
#endif
491+
return true;
492+
}
493+
479494
const char *IOHandlerEditline::GetContinuationPrompt() {
480495
return (m_continuation_prompt.empty() ? nullptr
481496
: m_continuation_prompt.c_str());

lldb/source/Host/common/Editline.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,8 @@ void Editline::DisplayCompletions(
11131113
}
11141114
}
11151115

1116+
void Editline::UseColor(bool use_color) { m_color = use_color; }
1117+
11161118
unsigned char Editline::TabCommand(int ch) {
11171119
if (!m_completion_callback)
11181120
return CC_ERROR;

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,12 +2236,17 @@ CommandInterpreter::GetAutoSuggestionForCommand(llvm::StringRef line) {
22362236
void CommandInterpreter::UpdatePrompt(llvm::StringRef new_prompt) {
22372237
EventSP prompt_change_event_sp(
22382238
new Event(eBroadcastBitResetPrompt, new EventDataBytes(new_prompt)));
2239-
;
2239+
22402240
BroadcastEvent(prompt_change_event_sp);
22412241
if (m_command_io_handler_sp)
22422242
m_command_io_handler_sp->SetPrompt(new_prompt);
22432243
}
22442244

2245+
void CommandInterpreter::UpdateUseColor(bool use_color) {
2246+
if (m_command_io_handler_sp)
2247+
m_command_io_handler_sp->SetUseColor(use_color);
2248+
}
2249+
22452250
bool CommandInterpreter::Confirm(llvm::StringRef message, bool default_answer) {
22462251
// Check AutoConfirm first:
22472252
if (m_debugger.GetAutoConfirm())

lldb/test/API/terminal/TestEditline.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,26 @@ def test_prompt_no_color(self):
9595
self.child.send("foo")
9696
# Check that there are no escape codes.
9797
self.child.expect(re.escape("\n(lldb) foo"))
98+
99+
@skipIfAsan
100+
@skipIfEditlineSupportMissing
101+
def test_enable_and_disable_color(self):
102+
"""Test that when we change the color during debugging it applies the changes"""
103+
# launch with colors enabled.
104+
self.launch(use_colors=True)
105+
self.child.send('settings set prompt-ansi-prefix "${ansi.fg.red}"\n')
106+
self.child.expect(re.escape("\x1b[31m(lldb) \x1b[0m\x1b[8G"))
107+
108+
# set use color to false.
109+
self.child.send("settings set use-color false\n")
110+
111+
# check that there is no color.
112+
self.child.send("foo\n")
113+
self.child.expect(re.escape("(lldb) foo"))
114+
115+
# set use-color to true
116+
self.child.send("settings set use-color true\n")
117+
118+
# check that there is colors;
119+
self.child.send("foo")
120+
self.child.expect(re.escape("\x1b[31m(lldb) \x1b[0m\x1b[8Gfoo"))

0 commit comments

Comments
 (0)