Skip to content

[lldb] Use the "reverse video" effect when colors are disabled. #134203

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 2 commits into from
Apr 3, 2025

Conversation

JDevlieghere
Copy link
Member

@JDevlieghere JDevlieghere commented Apr 3, 2025

When you run lldb without colors (-X), the status line looks weird because it doesn't have a background. You end up with what appears to be floating text at the bottom of your terminal.

This patch changes the statusline to use the reverse video effect, even when colors are off. The effect doesn't introduce any new colors and just inverts the foreground and background color.

I considered an alternative approach which changes the behavior of the -X option, so that turning off colors doesn't prevent emitting non-color related control characters such as bold, underline, and reverse video. I decided to go with this more targeted fix as (1) nobody is asking for this more general change and (2) it introduces significant complexity to plumb this through using a setting and driver flag so that it can be disabled when running the tests.

Fixes #134112.

When you run lldb without colors (`-X), the status line looks weird
because it doesn't have a background. You end up with what appears to be
floating text at the bottom of your terminal.

This patch changes the statusline to use the reverse video effect, even
when colors are off. The effect doesn't introduce any new colors and
just inverts the foreground and background color.

I considered an alternative approach which changes the behavior of the
`-X` option, so that turning off colors doesn't prevent emitting
non-color related control characters such as bold, underline, and
reverse video. I decided to go with this more targeted fix as (1) nobody
is asking for this more general change and (2) it introduces significant
complexity to plumb this through using a setting and driver flag so that
it can be disabled when running the tests.

Fixes llvm#134112.
@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

When you run lldb without colors (-X), the status line looks weird because it doesn't have a background. You end up with what appears to be floating text at the bottom of your terminal.

This patch changes the statusline to use the reverse video effect, even when colors are off. The effect doesn't introduce any new colors and just inverts the foreground and background color.

I considered an alternative approach which changes the behavior of the -X option, so that turning off colors doesn't prevent emitting non-color related control characters such as bold, underline, and reverse video. I decided to go with this more targeted fix as (1) nobody is asking for this more general change and (2) it introduces significant complexity to plumb this through using a setting and driver flag so that it can be disabled when running the tests.

Fixes #134112.


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

2 Files Affected:

  • (modified) lldb/source/Core/Statusline.cpp (+7)
  • (modified) lldb/test/API/functionalities/statusline/TestStatusline.py (+23)
diff --git a/lldb/source/Core/Statusline.cpp b/lldb/source/Core/Statusline.cpp
index c18fbb6c5561e..b7650503e16bc 100644
--- a/lldb/source/Core/Statusline.cpp
+++ b/lldb/source/Core/Statusline.cpp
@@ -27,6 +27,7 @@
 #define ANSI_CLEAR_LINE ESCAPE "[2K"
 #define ANSI_SET_SCROLL_ROWS ESCAPE "[0;%ur"
 #define ANSI_TO_START_OF_ROW ESCAPE "[%u;0f"
+#define ANSI_REVERSE_VIDEO ESCAPE "[7m"
 #define ANSI_UP_ROWS ESCAPE "[%dA"
 
 using namespace lldb;
@@ -74,6 +75,12 @@ void Statusline::Draw(std::string str) {
   locked_stream << ANSI_SAVE_CURSOR;
   locked_stream.Printf(ANSI_TO_START_OF_ROW,
                        static_cast<unsigned>(m_terminal_height));
+
+  // Use "reverse video" to make sure the statusline has a background. Only do
+  // this when colors are disabled, and rely on the statusline format otherwise.
+  if (!m_debugger.GetUseColor())
+    locked_stream << ANSI_REVERSE_VIDEO;
+
   locked_stream << str;
   locked_stream << ANSI_NORMAL;
   locked_stream << ANSI_RESTORE_CURSOR;
diff --git a/lldb/test/API/functionalities/statusline/TestStatusline.py b/lldb/test/API/functionalities/statusline/TestStatusline.py
index a58dc5470ed6d..391f788b9df48 100644
--- a/lldb/test/API/functionalities/statusline/TestStatusline.py
+++ b/lldb/test/API/functionalities/statusline/TestStatusline.py
@@ -55,3 +55,26 @@ def test(self):
         self.expect(
             "set set show-statusline false", ["\x1b[0;{}r".format(terminal_height)]
         )
+
+
+    # PExpect uses many timeouts internally and doesn't play well
+    # under ASAN on a loaded machine..
+    @skipIfAsan
+    def test_no_color(self):
+        """Basic test for the statusline with colors disabled."""
+        self.build()
+        self.launch(use_colors=False)
+        self.do_setup()
+
+        # Change the terminal dimensions.
+        terminal_height = 10
+        terminal_width = 60
+        self.child.setwinsize(terminal_height, terminal_width)
+
+        # Enable the statusline and check for the "reverse video" control character.
+        self.expect(
+            "set set show-statusline true",
+            [
+                "\x1b[7m",
+            ],
+        )

Copy link

github-actions bot commented Apr 3, 2025

✅ With the latest revision this PR passed the Python code formatter.

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

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

LGTM

@JDevlieghere JDevlieghere merged commit 5f99e0d into llvm:main Apr 3, 2025
10 checks passed
@JDevlieghere JDevlieghere deleted the issue-134112 branch April 3, 2025 20:51
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Apr 22, 2025
…#134203)

When you run lldb without colors (`-X`), the status line looks weird
because it doesn't have a background. You end up with what appears to be
floating text at the bottom of your terminal.

This patch changes the statusline to use the reverse video effect, even
when colors are off. The effect doesn't introduce any new colors and
just inverts the foreground and background color.

I considered an alternative approach which changes the behavior of the
`-X` option, so that turning off colors doesn't prevent emitting
non-color related control characters such as bold, underline, and
reverse video. I decided to go with this more targeted fix as (1) nobody
is asking for this more general change and (2) it introduces significant
complexity to plumb this through using a setting and driver flag so that
it can be disabled when running the tests.

Fixes llvm#134112.

(cherry picked from commit 5f99e0d)
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.

The LLDB statusline looks odd with colors off
3 participants