Skip to content

Conversation

@imkiva
Copy link
Member

@imkiva imkiva commented Oct 31, 2025

This PR fixes #157637 by printing ANSI sequence "\r\x1b[K" to empty the line before calling to el_wgets. Later when el_wgets prints the real prompt, all wrongly printed characters are removed from the terminal.

@imkiva imkiva requested a review from JDevlieghere as a code owner October 31, 2025 05:45
@llvmbot llvmbot added the lldb label Oct 31, 2025
@imkiva imkiva changed the title [LLDB][Editline] clear right characters before getLine [LLDB][Editline] empty current line before el_wgets Oct 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2025

@llvm/pr-subscribers-lldb

Author: Kiva (imkiva)

Changes

This PR fixes #157637 by printing ANSI sequence "\r\x1b[K" to empty the line before calling to el_wgets. Later when el_wgets prints the real prompt, all wrongly printed characters are removed from the terminal.


Full diff: https://github.com/llvm/llvm-project/pull/165830.diff

2 Files Affected:

  • (modified) lldb/source/Host/common/Editline.cpp (+6)
  • (modified) lldb/test/API/terminal/TestEditline.py (+1-1)
diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp
index 1b1922e710764..03b442cce973e 100644
--- a/lldb/source/Host/common/Editline.cpp
+++ b/lldb/source/Host/common/Editline.cpp
@@ -1626,6 +1626,12 @@ bool Editline::GetLine(std::string &line, bool &interrupted) {
   m_editor_status = EditorStatus::Editing;
   m_revert_cursor_index = -1;
 
+  if (m_locked_output && m_output_stream_sp) {
+    FILE *f = m_locked_output->GetFile().GetStream();
+    fprintf(f, "\r" ANSI_CLEAR_RIGHT);
+    fflush(f);
+  }
+
   int count;
   auto input = el_wgets(m_editline, &count);
 
diff --git a/lldb/test/API/terminal/TestEditline.py b/lldb/test/API/terminal/TestEditline.py
index 38f4f34ed740b..4696b1e1b112e 100644
--- a/lldb/test/API/terminal/TestEditline.py
+++ b/lldb/test/API/terminal/TestEditline.py
@@ -94,7 +94,7 @@ def test_prompt_no_color(self):
         # after the prompt.
         self.child.send("foo")
         # Check that there are no escape codes.
-        self.child.expect(re.escape("\n(lldb) foo"))
+        self.child.expect(re.escape("\n\r\x1b[K(lldb) foo"))
 
     @skipIfAsan
     @skipIfEditlineSupportMissing

@DavidSpickett
Copy link
Collaborator

Thanks for looking at this! I don't know enough here to review, but I think @JDevlieghere does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flush shouldn't be necessary, the locked output streams will flush when they're unlocked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have to check m_output_stream_sp, and the code on 1638 assumes that we have a locked output stream, so let's replace the check with an assert here (to cover both) and then use it unconditionally like we do below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks~

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@imkiva imkiva merged commit ca0866b into llvm:main Nov 7, 2025
10 checks passed
vinay-deshmukh pushed a commit to vinay-deshmukh/llvm-project that referenced this pull request Nov 8, 2025
This PR fixes llvm#157637 by printing ANSI sequence "\r\x1b[K" to empty the
line before calling to `el_wgets`. Later when `el_wgets` prints the real
prompt, all wrongly printed characters are removed from the terminal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When I use settings to minimise LLDB's default stop output, the (lldb) prompt is printed twice

4 participants