Skip to content

Commit d40537c

Browse files
da-viperJDevlieghere
authored andcommitted
[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]> (cherry picked from commit d7d1706)
1 parent c89ea13 commit d40537c

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
@@ -478,6 +478,8 @@ class CommandInterpreter : public Broadcaster,
478478

479479
void UpdatePrompt(llvm::StringRef prompt);
480480

481+
void UpdateUseColor(bool use_color);
482+
481483
bool Confirm(llvm::StringRef message, bool default_answer);
482484

483485
void LoadCommandDictionary();

lldb/source/Core/Debugger.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,16 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
234234
CommandInterpreter::eBroadcastBitResetPrompt, bytes.release());
235235
GetCommandInterpreter().BroadcastEvent(prompt_change_event_sp);
236236
} else if (property_path == g_debugger_properties[ePropertyUseColor].name) {
237-
// use-color changed. Ping the prompt so it can reset the ansi terminal
238-
// codes.
239-
SetPrompt(GetPrompt());
237+
// use-color changed. set use-color, this also pings the prompt so it can
238+
// reset the ansi terminal codes.
239+
SetUseColor(GetUseColor());
240240
} else if (property_path ==
241241
g_debugger_properties[ePropertyPromptAnsiPrefix].name ||
242242
property_path ==
243243
g_debugger_properties[ePropertyPromptAnsiSuffix].name) {
244-
// Prompt colors changed. Ping the prompt so it can reset the ansi
245-
// terminal codes.
246-
SetPrompt(GetPrompt());
244+
// Prompt color changed. set use-color, this also pings the prompt so it
245+
// can reset the ansi terminal codes.
246+
SetUseColor(GetUseColor());
247247
} else if (property_path ==
248248
g_debugger_properties[ePropertyShowStatusline].name) {
249249
// Statusline setting changed. If we have a statusline instance, update it
@@ -443,6 +443,8 @@ bool Debugger::GetUseColor() const {
443443
bool Debugger::SetUseColor(bool b) {
444444
const uint32_t idx = ePropertyUseColor;
445445
bool ret = SetPropertyAtIndex(idx, b);
446+
447+
GetCommandInterpreter().UpdateUseColor(b);
446448
SetPrompt(GetPrompt());
447449
return ret;
448450
}

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
@@ -2203,12 +2203,17 @@ CommandInterpreter::GetAutoSuggestionForCommand(llvm::StringRef line) {
22032203
void CommandInterpreter::UpdatePrompt(llvm::StringRef new_prompt) {
22042204
EventSP prompt_change_event_sp(
22052205
new Event(eBroadcastBitResetPrompt, new EventDataBytes(new_prompt)));
2206-
;
2206+
22072207
BroadcastEvent(prompt_change_event_sp);
22082208
if (m_command_io_handler_sp)
22092209
m_command_io_handler_sp->SetPrompt(new_prompt);
22102210
}
22112211

2212+
void CommandInterpreter::UpdateUseColor(bool use_color) {
2213+
if (m_command_io_handler_sp)
2214+
m_command_io_handler_sp->SetUseColor(use_color);
2215+
}
2216+
22122217
bool CommandInterpreter::Confirm(llvm::StringRef message, bool default_answer) {
22132218
// Check AutoConfirm first:
22142219
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)