2525#include " lldb/Utility/Timeout.h"
2626
2727#include " llvm/Support/FileSystem.h"
28+ #include " llvm/Support/Locale.h"
2829#include " llvm/Support/Threading.h"
2930
3031using namespace lldb_private ;
@@ -52,10 +53,6 @@ int setupterm(char *term, int fildes, int *errret);
5253
5354// / https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
5455#define ESCAPE " \x1b "
55- // / Faint, decreased intensity or second colour.
56- #define ANSI_FAINT ESCAPE " [2m"
57- // / Normal colour or normal intensity (neither bold nor faint).
58- #define ANSI_UNFAINT ESCAPE " [0m"
5956#define ANSI_CLEAR_BELOW ESCAPE " [J"
6057#define ANSI_CLEAR_RIGHT ESCAPE " [K"
6158#define ANSI_SET_COLUMN_N ESCAPE " [%dG"
@@ -101,6 +98,10 @@ bool IsOnlySpaces(const EditLineStringType &content) {
10198 return true ;
10299}
103100
101+ static size_t ColumnWidth (llvm::StringRef str) {
102+ return llvm::sys::locale::columnWidth (str);
103+ }
104+
104105static int GetOperation (HistoryOperation op) {
105106 // The naming used by editline for the history operations is counter
106107 // intuitive to how it's used in LLDB's editline implementation.
@@ -328,14 +329,16 @@ std::string Editline::PromptForIndex(int line_index) {
328329 std::string continuation_prompt = prompt;
329330 if (m_set_continuation_prompt.length () > 0 ) {
330331 continuation_prompt = m_set_continuation_prompt;
331-
332332 // Ensure that both prompts are the same length through space padding
333- while (continuation_prompt.length () < prompt.length ()) {
334- continuation_prompt += ' ' ;
335- }
336- while (prompt.length () < continuation_prompt.length ()) {
337- prompt += ' ' ;
338- }
333+ const size_t prompt_width = ColumnWidth (prompt);
334+ const size_t cont_prompt_width = ColumnWidth (continuation_prompt);
335+ const size_t padded_prompt_width =
336+ std::max (prompt_width, cont_prompt_width);
337+ if (prompt_width < padded_prompt_width)
338+ prompt += std::string (padded_prompt_width - prompt_width, ' ' );
339+ else if (cont_prompt_width < padded_prompt_width)
340+ continuation_prompt +=
341+ std::string (padded_prompt_width - cont_prompt_width, ' ' );
339342 }
340343
341344 if (use_line_numbers) {
@@ -353,7 +356,7 @@ void Editline::SetCurrentLine(int line_index) {
353356 m_current_prompt = PromptForIndex (line_index);
354357}
355358
356- int Editline::GetPromptWidth () { return ( int ) PromptForIndex (0 ). length ( ); }
359+ size_t Editline::GetPromptWidth () { return ColumnWidth ( PromptForIndex (0 )); }
357360
358361bool Editline::IsEmacs () {
359362 const char *editor;
@@ -424,15 +427,13 @@ void Editline::MoveCursor(CursorLocation from, CursorLocation to) {
424427void Editline::DisplayInput (int firstIndex) {
425428 fprintf (m_output_file, ANSI_SET_COLUMN_N ANSI_CLEAR_BELOW, 1 );
426429 int line_count = (int )m_input_lines.size ();
427- const char *faint = m_color_prompts ? ANSI_FAINT : " " ;
428- const char *unfaint = m_color_prompts ? ANSI_UNFAINT : " " ;
429-
430430 for (int index = firstIndex; index < line_count; index++) {
431- fprintf (m_output_file, " %s"
432- " %s"
433- " %s" EditLineStringFormatSpec " " ,
434- faint, PromptForIndex (index).c_str (), unfaint,
435- m_input_lines[index].c_str ());
431+ fprintf (m_output_file,
432+ " %s"
433+ " %s"
434+ " %s" EditLineStringFormatSpec " " ,
435+ m_prompt_ansi_prefix.c_str (), PromptForIndex (index).c_str (),
436+ m_prompt_ansi_suffix.c_str (), m_input_lines[index].c_str ());
436437 if (index < line_count - 1 )
437438 fprintf (m_output_file, " \n " );
438439 }
@@ -441,7 +442,7 @@ void Editline::DisplayInput(int firstIndex) {
441442int Editline::CountRowsForLine (const EditLineStringType &content) {
442443 std::string prompt =
443444 PromptForIndex (0 ); // Prompt width is constant during an edit session
444- int line_length = (int )(content.length () + prompt. length ( ));
445+ int line_length = (int )(content.length () + ColumnWidth (prompt ));
445446 return (line_length / m_terminal_width) + 1 ;
446447}
447448
@@ -541,14 +542,16 @@ unsigned char Editline::RecallHistory(HistoryOperation op) {
541542int Editline::GetCharacter (EditLineGetCharType *c) {
542543 const LineInfoW *info = el_wline (m_editline);
543544
544- // Paint a faint version of the desired prompt over the version libedit draws
545- // (will only be requested if colors are supported)
545+ // Paint a ANSI formatted version of the desired prompt over the version
546+ // libedit draws. (will only be requested if colors are supported)
546547 if (m_needs_prompt_repaint) {
547548 MoveCursor (CursorLocation::EditingCursor, CursorLocation::EditingPrompt);
548- fprintf (m_output_file, " %s"
549- " %s"
550- " %s" ,
551- ANSI_FAINT, Prompt (), ANSI_UNFAINT);
549+ fprintf (m_output_file,
550+ " %s"
551+ " %s"
552+ " %s" ,
553+ m_prompt_ansi_prefix.c_str (), Prompt (),
554+ m_prompt_ansi_suffix.c_str ());
552555 MoveCursor (CursorLocation::EditingPrompt, CursorLocation::EditingCursor);
553556 m_needs_prompt_repaint = false ;
554557 }
0 commit comments