Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lldb/include/lldb/Core/IOHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ class IOHandler {
// Prompt support isn't mandatory
return false;
}

virtual bool SetUseColor(bool use_color) {
// Use color isn't mandatory
return false;
};

bool SetPrompt(const char *) = delete;

virtual llvm::StringRef GetControlSequence(char ch) { return {}; }
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions lldb/include/lldb/Host/Editline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/Interpreter/CommandInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 8 additions & 6 deletions lldb/source/Core/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions lldb/source/Core/IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions lldb/source/Host/common/Editline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,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;
Expand Down
7 changes: 6 additions & 1 deletion lldb/source/Interpreter/CommandInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
23 changes: 23 additions & 0 deletions lldb/test/API/terminal/TestEditline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Loading