Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions lldb/include/lldb/Core/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ class Debugger : public std::enable_shared_from_this<Debugger>,

llvm::StringRef GetPrompt() const;

llvm::StringRef GetPromptAnsiPrefix() const;

llvm::StringRef GetPromptAnsiSuffix() const;

void SetPrompt(llvm::StringRef p);
void SetPrompt(const char *) = delete;

Expand Down
15 changes: 12 additions & 3 deletions lldb/include/lldb/Host/Editline.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ class Editline {
m_fix_indentation_callback_chars = indent_chars;
}

void SetPromptAnsiPrefix(std::string prefix) {
m_prompt_ansi_prefix = std::move(prefix);
}

void SetPromptAnsiSuffix(std::string suffix) {
m_prompt_ansi_suffix = std::move(suffix);
}

void SetSuggestionAnsiPrefix(std::string prefix) {
m_suggestion_ansi_prefix = std::move(prefix);
}
Expand Down Expand Up @@ -250,9 +258,8 @@ class Editline {
void SetCurrentLine(int line_index);

/// Determines the width of the prompt in characters. The width is guaranteed
/// to be the same for
/// all lines of the current multi-line session.
int GetPromptWidth();
/// to be the same for all lines of the current multi-line session.
size_t GetPromptWidth();

/// Returns true if the underlying EditLine session's keybindings are
/// Emacs-based, or false if
Expand Down Expand Up @@ -404,6 +411,8 @@ class Editline {
CompleteCallbackType m_completion_callback;
SuggestionCallbackType m_suggestion_callback;

std::string m_prompt_ansi_prefix;
std::string m_prompt_ansi_suffix;
std::string m_suggestion_ansi_prefix;
std::string m_suggestion_ansi_suffix;

Expand Down
4 changes: 4 additions & 0 deletions lldb/packages/Python/lldbsuite/test/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,10 @@ def skipIfEditlineSupportMissing(func):
return _get_bool_config_skip_if_decorator("editline")(func)


def skipIfEditlineWideCharSupportMissing(func):
return _get_bool_config_skip_if_decorator("editline_wchar")(func)


def skipIfFBSDVMCoreSupportMissing(func):
return _get_bool_config_skip_if_decorator("fbsdvmcore")(func)

Expand Down
2 changes: 2 additions & 0 deletions lldb/packages/Python/lldbsuite/test/lldbpexpect.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def launch(
dimensions=None,
run_under=None,
post_spawn=None,
encoding=None,
use_colors=False,
):
logfile = getattr(sys.stdout, "buffer", sys.stdout) if self.TraceOn() else None
Expand Down Expand Up @@ -60,6 +61,7 @@ def launch(
timeout=timeout,
dimensions=dimensions,
env=env,
encoding=encoding,
)
self.child.ptyproc.delayafterclose = timeout / 10
self.child.ptyproc.delayafterterminate = timeout / 10
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/API/SBDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,9 @@ SBStructuredData SBDebugger::GetBuildConfiguration() {
AddBoolConfigEntry(
*config_up, "editline", LLDB_ENABLE_LIBEDIT,
"A boolean value that indicates if editline support is enabled in LLDB");
AddBoolConfigEntry(*config_up, "editline_wchar", LLDB_EDITLINE_USE_WCHAR,
"A boolean value that indicates if editline wide "
"characters support is enabled in LLDB");
AddBoolConfigEntry(
*config_up, "lzma", LLDB_ENABLE_LZMA,
"A boolean value that indicates if lzma support is enabled in LLDB");
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/Core/CoreProperties.td
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ let Definition = "debugger" in {
DefaultEnumValue<"OptionValueString::eOptionEncodeCharacterEscapeSequences">,
DefaultStringValue<"(lldb) ">,
Desc<"The debugger command line prompt displayed for the user.">;
def PromptAnsiPrefix: Property<"prompt-ansi-prefix", "String">,
Global,
DefaultStringValue<"${ansi.faint}">,
Desc<"When in a color-enabled terminal, use the ANSI terminal code specified in this format immediately before the prompt.">;
def PromptAnsiSuffix: Property<"prompt-ansi-suffix", "String">,
Global,
DefaultStringValue<"${ansi.normal}">,
Desc<"When in a color-enabled terminal, use the ANSI terminal code specified in this format immediately after the prompt.">;
def ScriptLanguage: Property<"script-lang", "Enum">,
Global,
DefaultEnumValue<"eScriptLanguagePython">,
Expand Down
19 changes: 19 additions & 0 deletions lldb/source/Core/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
// use-color changed. Ping the prompt so it can reset the ansi terminal
// codes.
SetPrompt(GetPrompt());
} 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());
} else if (property_path ==
g_debugger_properties[ePropertyUseSourceCache].name) {
// use-source-cache changed. Wipe out the cache contents if it was
Expand Down Expand Up @@ -301,6 +308,18 @@ llvm::StringRef Debugger::GetPrompt() const {
idx, g_debugger_properties[idx].default_cstr_value);
}

llvm::StringRef Debugger::GetPromptAnsiPrefix() const {
const uint32_t idx = ePropertyPromptAnsiPrefix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}

llvm::StringRef Debugger::GetPromptAnsiSuffix() const {
const uint32_t idx = ePropertyPromptAnsiSuffix;
return GetPropertyAtIndexAs<llvm::StringRef>(
idx, g_debugger_properties[idx].default_cstr_value);
}

void Debugger::SetPrompt(llvm::StringRef p) {
constexpr uint32_t idx = ePropertyPrompt;
SetPropertyAtIndex(idx, p);
Expand Down
9 changes: 8 additions & 1 deletion lldb/source/Core/IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,15 @@ bool IOHandlerEditline::SetPrompt(llvm::StringRef prompt) {
m_prompt = std::string(prompt);

#if LLDB_ENABLE_LIBEDIT
if (m_editline_up)
if (m_editline_up) {
m_editline_up->SetPrompt(m_prompt.empty() ? nullptr : m_prompt.c_str());
if (m_debugger.GetUseColor()) {
m_editline_up->SetPromptAnsiPrefix(
ansi::FormatAnsiTerminalCodes(m_debugger.GetPromptAnsiPrefix()));
m_editline_up->SetPromptAnsiSuffix(
ansi::FormatAnsiTerminalCodes(m_debugger.GetPromptAnsiSuffix()));
}
}
#endif
return true;
}
Expand Down
57 changes: 30 additions & 27 deletions lldb/source/Host/common/Editline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "lldb/Utility/Timeout.h"

#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Locale.h"
#include "llvm/Support/Threading.h"

using namespace lldb_private;
Expand Down Expand Up @@ -52,10 +53,6 @@ int setupterm(char *term, int fildes, int *errret);

/// https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
#define ESCAPE "\x1b"
/// Faint, decreased intensity or second colour.
#define ANSI_FAINT ESCAPE "[2m"
/// Normal colour or normal intensity (neither bold nor faint).
#define ANSI_UNFAINT ESCAPE "[0m"
#define ANSI_CLEAR_BELOW ESCAPE "[J"
#define ANSI_CLEAR_RIGHT ESCAPE "[K"
#define ANSI_SET_COLUMN_N ESCAPE "[%dG"
Expand Down Expand Up @@ -101,6 +98,10 @@ bool IsOnlySpaces(const EditLineStringType &content) {
return true;
}

static size_t ColumnWidth(llvm::StringRef str) {
return llvm::sys::locale::columnWidth(str);
}

static int GetOperation(HistoryOperation op) {
// The naming used by editline for the history operations is counter
// intuitive to how it's used in LLDB's editline implementation.
Expand Down Expand Up @@ -328,14 +329,16 @@ std::string Editline::PromptForIndex(int line_index) {
std::string continuation_prompt = prompt;
if (m_set_continuation_prompt.length() > 0) {
continuation_prompt = m_set_continuation_prompt;

// Ensure that both prompts are the same length through space padding
while (continuation_prompt.length() < prompt.length()) {
continuation_prompt += ' ';
}
while (prompt.length() < continuation_prompt.length()) {
prompt += ' ';
}
const size_t prompt_width = ColumnWidth(prompt);
const size_t cont_prompt_width = ColumnWidth(continuation_prompt);
const size_t padded_prompt_width =
std::max(prompt_width, cont_prompt_width);
if (prompt_width < padded_prompt_width)
prompt += std::string(padded_prompt_width - prompt_width, ' ');
else if (cont_prompt_width < padded_prompt_width)
continuation_prompt +=
std::string(padded_prompt_width - cont_prompt_width, ' ');
}

if (use_line_numbers) {
Expand All @@ -353,7 +356,7 @@ void Editline::SetCurrentLine(int line_index) {
m_current_prompt = PromptForIndex(line_index);
}

int Editline::GetPromptWidth() { return (int)PromptForIndex(0).length(); }
size_t Editline::GetPromptWidth() { return ColumnWidth(PromptForIndex(0)); }

bool Editline::IsEmacs() {
const char *editor;
Expand Down Expand Up @@ -424,15 +427,13 @@ void Editline::MoveCursor(CursorLocation from, CursorLocation to) {
void Editline::DisplayInput(int firstIndex) {
fprintf(m_output_file, ANSI_SET_COLUMN_N ANSI_CLEAR_BELOW, 1);
int line_count = (int)m_input_lines.size();
const char *faint = m_color_prompts ? ANSI_FAINT : "";
const char *unfaint = m_color_prompts ? ANSI_UNFAINT : "";

for (int index = firstIndex; index < line_count; index++) {
fprintf(m_output_file, "%s"
"%s"
"%s" EditLineStringFormatSpec " ",
faint, PromptForIndex(index).c_str(), unfaint,
m_input_lines[index].c_str());
fprintf(m_output_file,
"%s"
"%s"
"%s" EditLineStringFormatSpec " ",
m_prompt_ansi_prefix.c_str(), PromptForIndex(index).c_str(),
m_prompt_ansi_suffix.c_str(), m_input_lines[index].c_str());
if (index < line_count - 1)
fprintf(m_output_file, "\n");
}
Expand All @@ -441,7 +442,7 @@ void Editline::DisplayInput(int firstIndex) {
int Editline::CountRowsForLine(const EditLineStringType &content) {
std::string prompt =
PromptForIndex(0); // Prompt width is constant during an edit session
int line_length = (int)(content.length() + prompt.length());
int line_length = (int)(content.length() + ColumnWidth(prompt));
return (line_length / m_terminal_width) + 1;
}

Expand Down Expand Up @@ -541,14 +542,16 @@ unsigned char Editline::RecallHistory(HistoryOperation op) {
int Editline::GetCharacter(EditLineGetCharType *c) {
const LineInfoW *info = el_wline(m_editline);

// Paint a faint version of the desired prompt over the version libedit draws
// (will only be requested if colors are supported)
// Paint a ANSI formatted version of the desired prompt over the version
// libedit draws. (will only be requested if colors are supported)
if (m_needs_prompt_repaint) {
MoveCursor(CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
fprintf(m_output_file, "%s"
"%s"
"%s",
ANSI_FAINT, Prompt(), ANSI_UNFAINT);
fprintf(m_output_file,
"%s"
"%s"
"%s",
m_prompt_ansi_prefix.c_str(), Prompt(),
m_prompt_ansi_suffix.c_str());
MoveCursor(CursorLocation::EditingPrompt, CursorLocation::EditingCursor);
m_needs_prompt_repaint = false;
}
Expand Down
36 changes: 36 additions & 0 deletions lldb/test/API/terminal/TestEditline.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,39 @@ def test_left_right_arrow(self):
)

self.quit()

@skipIfAsan
@skipIfEditlineSupportMissing
@skipIfEditlineWideCharSupportMissing
def test_prompt_unicode(self):
"""Test that we can use Unicode in the LLDB prompt."""
self.launch(use_colors=True, encoding="utf-8")
self.child.send('settings set prompt "🐛 "\n')
# Check that the cursor is at position 4 ([4G)
# Prompt: 🐛 _
# Column: 1..4
self.child.expect(re.escape("🐛 \x1b[0m\x1b[4G"))

@skipIfAsan
@skipIfEditlineSupportMissing
def test_prompt_color(self):
"""Test that we can change the prompt color with prompt-ansi-prefix."""
self.launch(use_colors=True)
self.child.send('settings set prompt-ansi-prefix "${ansi.fg.red}"\n')
# Make sure this change is reflected immediately. Check that the color
# is set (31) and the cursor position (8) is correct.
# Prompt: (lldb) _
# Column: 1....6.8
self.child.expect(re.escape("\x1b[31m(lldb) \x1b[0m\x1b[8G"))

@skipIfAsan
@skipIfEditlineSupportMissing
def test_prompt_no_color(self):
"""Test that prompt-ansi-prefix doesn't color the prompt when colors are off."""
self.launch(use_colors=False)
self.child.send('settings set prompt-ansi-prefix "${ansi.fg.red}"\n')
# Send foo so we can match the newline before the prompt and the foo
# after the prompt.
self.child.send("foo")
# Check that there are no escape codes.
self.child.expect(re.escape("\n(lldb) foo"))