[lldb] Support Unicode in the prompt (#66312)

Account for Unicode when computing the prompt column width. Previously,
the string length (i.e. number of bytes) rather than the width of the
Glyph was used to compute the cursor position. The result was that the
cursor would be offset to the right when using a prompt containing
Unicode.
This commit is contained in:
Jonas Devlieghere
2023-09-13 20:08:05 -07:00
committed by GitHub
parent 1dae4dd0d8
commit 850e90c47b
4 changed files with 31 additions and 12 deletions

View File

@@ -248,9 +248,8 @@ private:
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

View File

@@ -26,6 +26,7 @@ class PExpectTest(TestBase):
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
@@ -58,6 +59,7 @@ class PExpectTest(TestBase):
timeout=timeout,
dimensions=dimensions,
env=env,
encoding=encoding,
)
self.child.ptyproc.delayafterclose = timeout / 10
self.child.ptyproc.delayafterterminate = timeout / 10

View File

@@ -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;
@@ -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.
@@ -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) {
@@ -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;
@@ -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;
}

View File

@@ -44,3 +44,14 @@ class EditlineTest(PExpectTest):
)
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"))