Skip to content

[lldb] Support Unicode in the prompt #66312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2023
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
5 changes: 2 additions & 3 deletions lldb/include/lldb/Host/Editline.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,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
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 @@ -26,6 +26,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 @@ -58,6 +59,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
25 changes: 16 additions & 9 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 @@ -101,6 +102,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 +333,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 +360,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 @@ -441,7 +448,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
11 changes: 11 additions & 0 deletions lldb/test/API/terminal/TestEditline.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ def test_left_right_arrow(self):
)

self.quit()

@skipIfAsan
@skipIfEditlineSupportMissing
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"))