Author: Ebuka Ezike Date: 2025-04-22T12:21:23+01:00 New Revision: d7d170656404e1cb29a51689fd66a12bc060c630
URL: https://github.com/llvm/llvm-project/commit/d7d170656404e1cb29a51689fd66a12bc060c630 DIFF: https://github.com/llvm/llvm-project/commit/d7d170656404e1cb29a51689fd66a12bc060c630.diff LOG: [lldb] Fix use-color settings not persistent (#135626) Fixes https://github.com/llvm/llvm-project/issues/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 <yerimy...@gmail.com> Co-authored-by: Jonas Devlieghere <jo...@devlieghere.com> Added: Modified: lldb/include/lldb/Core/IOHandler.h lldb/include/lldb/Host/Editline.h lldb/include/lldb/Interpreter/CommandInterpreter.h lldb/source/Core/Debugger.cpp lldb/source/Core/IOHandler.cpp lldb/source/Host/common/Editline.cpp lldb/source/Interpreter/CommandInterpreter.cpp lldb/test/API/terminal/TestEditline.py Removed: ################################################################################ diff --git a/lldb/include/lldb/Core/IOHandler.h b/lldb/include/lldb/Core/IOHandler.h index 794d229bc1337..2fb3d7a7c9cc3 100644 --- a/lldb/include/lldb/Core/IOHandler.h +++ b/lldb/include/lldb/Core/IOHandler.h @@ -99,6 +99,12 @@ class IOHandler { // Prompt support isn't mandatory return false; } + + virtual bool SetUseColor(bool use_color) { + // Color support isn't mandatory. + return false; + }; + bool SetPrompt(const char *) = delete; virtual llvm::StringRef GetControlSequence(char ch) { return {}; } @@ -375,6 +381,8 @@ class IOHandlerEditline : public IOHandler { bool SetPrompt(llvm::StringRef prompt) override; bool SetPrompt(const char *prompt) = delete; + bool SetUseColor(bool use_color) override; + const char *GetContinuationPrompt(); void SetContinuationPrompt(llvm::StringRef prompt); diff --git a/lldb/include/lldb/Host/Editline.h b/lldb/include/lldb/Host/Editline.h index 705ec9c49f7c7..c202a76758e13 100644 --- a/lldb/include/lldb/Host/Editline.h +++ b/lldb/include/lldb/Host/Editline.h @@ -168,6 +168,9 @@ class Editline { DisplayCompletions(Editline &editline, llvm::ArrayRef<CompletionResult::Completion> results); + /// Sets if editline should use color. + void UseColor(bool use_color); + /// Sets a string to be used as a prompt, or combined with a line number to /// form a prompt. void SetPrompt(const char *prompt); @@ -223,21 +226,29 @@ class Editline { void SetPromptAnsiPrefix(std::string prefix) { if (m_color) m_prompt_ansi_prefix = std::move(prefix); + else + m_prompt_ansi_prefix.clear(); } void SetPromptAnsiSuffix(std::string suffix) { if (m_color) m_prompt_ansi_suffix = std::move(suffix); + else + m_prompt_ansi_suffix.clear(); } void SetSuggestionAnsiPrefix(std::string prefix) { if (m_color) m_suggestion_ansi_prefix = std::move(prefix); + else + m_suggestion_ansi_prefix.clear(); } void SetSuggestionAnsiSuffix(std::string suffix) { if (m_color) m_suggestion_ansi_suffix = std::move(suffix); + else + m_suggestion_ansi_suffix.clear(); } /// Prompts for and reads a single line of user input. diff --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h index b65edcf68b251..724d88d65f6ac 100644 --- a/lldb/include/lldb/Interpreter/CommandInterpreter.h +++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h @@ -476,6 +476,8 @@ class CommandInterpreter : public Broadcaster, void UpdatePrompt(llvm::StringRef prompt); + void UpdateUseColor(bool use_color); + bool Confirm(llvm::StringRef message, bool default_answer); void LoadCommandDictionary(); diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp index 8fe80b3841883..7b60e1a14f414 100644 --- a/lldb/source/Core/Debugger.cpp +++ b/lldb/source/Core/Debugger.cpp @@ -237,16 +237,16 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx, CommandInterpreter::eBroadcastBitResetPrompt, bytes.release()); GetCommandInterpreter().BroadcastEvent(prompt_change_event_sp); } else if (property_path == g_debugger_properties[ePropertyUseColor].name) { - // use-color changed. Ping the prompt so it can reset the ansi terminal - // codes. - SetPrompt(GetPrompt()); + // use-color changed. set use-color, this also pings the prompt so it can + // reset the ansi terminal codes. + SetUseColor(GetUseColor()); } else if (property_path == g_debugger_properties[ePropertyPromptAnsiPrefix].name || property_path == g_debugger_properties[ePropertyPromptAnsiSuffix].name) { - // Prompt colors changed. Ping the prompt so it can reset the ansi - // terminal codes. - SetPrompt(GetPrompt()); + // Prompt color changed. set use-color, this also pings the prompt so it + // can reset the ansi terminal codes. + SetUseColor(GetUseColor()); } else if (property_path == g_debugger_properties[ePropertyShowStatusline].name) { // Statusline setting changed. If we have a statusline instance, update it @@ -455,6 +455,8 @@ bool Debugger::GetUseColor() const { bool Debugger::SetUseColor(bool b) { const uint32_t idx = ePropertyUseColor; bool ret = SetPropertyAtIndex(idx, b); + + GetCommandInterpreter().UpdateUseColor(b); SetPrompt(GetPrompt()); return ret; } diff --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp index d336cb0592d5b..8aac507eaa0c2 100644 --- a/lldb/source/Core/IOHandler.cpp +++ b/lldb/source/Core/IOHandler.cpp @@ -476,6 +476,21 @@ bool IOHandlerEditline::SetPrompt(llvm::StringRef prompt) { return true; } +bool IOHandlerEditline::SetUseColor(bool use_color) { + m_color = use_color; + +#if LLDB_ENABLE_LIBEDIT + if (m_editline_up) { + m_editline_up->UseColor(use_color); + m_editline_up->SetSuggestionAnsiPrefix(ansi::FormatAnsiTerminalCodes( + m_debugger.GetAutosuggestionAnsiPrefix())); + m_editline_up->SetSuggestionAnsiSuffix(ansi::FormatAnsiTerminalCodes( + m_debugger.GetAutosuggestionAnsiSuffix())); + } +#endif + return true; +} + const char *IOHandlerEditline::GetContinuationPrompt() { return (m_continuation_prompt.empty() ? nullptr : m_continuation_prompt.c_str()); diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp index ff71cd0cdb241..b251ded6c3793 100644 --- a/lldb/source/Host/common/Editline.cpp +++ b/lldb/source/Host/common/Editline.cpp @@ -1113,6 +1113,8 @@ void Editline::DisplayCompletions( } } +void Editline::UseColor(bool use_color) { m_color = use_color; } + unsigned char Editline::TabCommand(int ch) { if (!m_completion_callback) return CC_ERROR; diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index 112d2f20fda41..eb4741feb0aa5 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -2236,12 +2236,17 @@ CommandInterpreter::GetAutoSuggestionForCommand(llvm::StringRef line) { void CommandInterpreter::UpdatePrompt(llvm::StringRef new_prompt) { EventSP prompt_change_event_sp( new Event(eBroadcastBitResetPrompt, new EventDataBytes(new_prompt))); - ; + BroadcastEvent(prompt_change_event_sp); if (m_command_io_handler_sp) m_command_io_handler_sp->SetPrompt(new_prompt); } +void CommandInterpreter::UpdateUseColor(bool use_color) { + if (m_command_io_handler_sp) + m_command_io_handler_sp->SetUseColor(use_color); +} + bool CommandInterpreter::Confirm(llvm::StringRef message, bool default_answer) { // Check AutoConfirm first: if (m_debugger.GetAutoConfirm()) diff --git a/lldb/test/API/terminal/TestEditline.py b/lldb/test/API/terminal/TestEditline.py index ddaa441d5f7c1..f797cc22b61fb 100644 --- a/lldb/test/API/terminal/TestEditline.py +++ b/lldb/test/API/terminal/TestEditline.py @@ -95,3 +95,26 @@ def test_prompt_no_color(self): self.child.send("foo") # Check that there are no escape codes. self.child.expect(re.escape("\n(lldb) foo")) + + @skipIfAsan + @skipIfEditlineSupportMissing + def test_enable_and_disable_color(self): + """Test that when we change the color during debugging it applies the changes""" + # launch with colors enabled. + self.launch(use_colors=True) + self.child.send('settings set prompt-ansi-prefix "${ansi.fg.red}"\n') + self.child.expect(re.escape("\x1b[31m(lldb) \x1b[0m\x1b[8G")) + + # set use color to false. + self.child.send("settings set use-color false\n") + + # check that there is no color. + self.child.send("foo\n") + self.child.expect(re.escape("(lldb) foo")) + + # set use-color to true + self.child.send("settings set use-color true\n") + + # check that there is colors; + self.child.send("foo") + self.child.expect(re.escape("\x1b[31m(lldb) \x1b[0m\x1b[8Gfoo")) _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits