Author: Jonas Devlieghere
Date: 2025-03-31T23:53:35-07:00
New Revision: aa889ed129ff26d9341c50a9eaba4db728ca6212

URL: 
https://github.com/llvm/llvm-project/commit/aa889ed129ff26d9341c50a9eaba4db728ca6212
DIFF: 
https://github.com/llvm/llvm-project/commit/aa889ed129ff26d9341c50a9eaba4db728ca6212.diff

LOG: [lldb] Fix statusline terminal resizing

Simplify and fix the logic to clear the old statusline when the terminal
window dimensions have changed. I accidentally broke the terminal
resizing behavior when addressing code review feedback.

I'd really like to figure out a way to test this. PExpect isn't a good
fit for this, because I really need to check the result, rather than the
control characters, as the latter doesn't tell me whether any part of
the old statusline is still visible.

Added: 
    

Modified: 
    lldb/include/lldb/Core/Statusline.h
    lldb/source/Core/Statusline.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/Statusline.h 
b/lldb/include/lldb/Core/Statusline.h
index c1449f0f69081..521b9f2526f6b 100644
--- a/lldb/include/lldb/Core/Statusline.h
+++ b/lldb/include/lldb/Core/Statusline.h
@@ -10,8 +10,6 @@
 #define LLDB_CORE_STATUSLINE_H
 
 #include "lldb/lldb-forward.h"
-#include "llvm/ADT/StringRef.h"
-#include <csignal>
 #include <cstdint>
 #include <string>
 
@@ -34,10 +32,6 @@ class Statusline {
   /// Inform the statusline that the terminal dimensions have changed.
   void TerminalSizeChanged();
 
-protected:
-  /// Pad and trim the given string to fit to the given width.
-  static std::string TrimAndPad(std::string str, size_t width);
-
 private:
   /// Draw the statusline with the given text.
   void Draw(std::string msg);
@@ -46,20 +40,15 @@ class Statusline {
   void UpdateTerminalProperties();
 
   enum ScrollWindowMode {
-    ScrollWindowExtend,
-    ScrollWindowShrink,
+    EnableStatusline,
+    DisableStatusline,
   };
 
   /// Set the scroll window for the given mode.
   void UpdateScrollWindow(ScrollWindowMode mode);
 
-  /// Clear the statusline (without redrawing the background).
-  void Reset();
-
   Debugger &m_debugger;
   std::string m_last_str;
-
-  volatile std::sig_atomic_t m_terminal_size_has_changed = 1;
   uint64_t m_terminal_width = 0;
   uint64_t m_terminal_height = 0;
 };

diff  --git a/lldb/source/Core/Statusline.cpp b/lldb/source/Core/Statusline.cpp
index c01388eb7e7b5..c18fbb6c5561e 100644
--- a/lldb/source/Core/Statusline.cpp
+++ b/lldb/source/Core/Statusline.cpp
@@ -23,7 +23,8 @@
 #define ANSI_SAVE_CURSOR ESCAPE "7"
 #define ANSI_RESTORE_CURSOR ESCAPE "8"
 #define ANSI_CLEAR_BELOW ESCAPE "[J"
-#define ANSI_CLEAR_LINE "\r\x1B[2K"
+#define ANSI_CURSOR_DOWN ESCAPE "[B"
+#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_UP_ROWS ESCAPE "[%dA"
@@ -31,12 +32,16 @@
 using namespace lldb;
 using namespace lldb_private;
 
-Statusline::Statusline(Debugger &debugger) : m_debugger(debugger) { Enable(); }
+Statusline::Statusline(Debugger &debugger)
+    : m_debugger(debugger), m_terminal_width(m_debugger.GetTerminalWidth()),
+      m_terminal_height(m_debugger.GetTerminalHeight()) {
+  Enable();
+}
 
 Statusline::~Statusline() { Disable(); }
 
 void Statusline::TerminalSizeChanged() {
-  m_terminal_size_has_changed = 1;
+  UpdateTerminalProperties();
 
   // This definitely isn't signal safe, but the best we can do, until we
   // have proper signal-catching thread.
@@ -44,20 +49,16 @@ void Statusline::TerminalSizeChanged() {
 }
 
 void Statusline::Enable() {
-  UpdateTerminalProperties();
-
   // Reduce the scroll window to make space for the status bar below.
-  UpdateScrollWindow(ScrollWindowShrink);
+  UpdateScrollWindow(EnableStatusline);
 
   // Draw the statusline.
-  Redraw();
+  Redraw(/*update=*/true);
 }
 
 void Statusline::Disable() {
-  UpdateTerminalProperties();
-
   // Extend the scroll window to cover the status bar.
-  UpdateScrollWindow(ScrollWindowExtend);
+  UpdateScrollWindow(DisableStatusline);
 }
 
 void Statusline::Draw(std::string str) {
@@ -65,8 +66,6 @@ void Statusline::Draw(std::string str) {
   if (!stream_sp)
     return;
 
-  UpdateTerminalProperties();
-
   m_last_str = str;
 
   str = ansi::TrimAndPad(str, m_terminal_width);
@@ -80,58 +79,37 @@ void Statusline::Draw(std::string str) {
   locked_stream << ANSI_RESTORE_CURSOR;
 }
 
-void Statusline::Reset() {
-  lldb::LockableStreamFileSP stream_sp = m_debugger.GetOutputStreamSP();
-  if (!stream_sp)
-    return;
-
-  LockedStreamFile locked_stream = stream_sp->Lock();
-  locked_stream << ANSI_SAVE_CURSOR;
-  locked_stream.Printf(ANSI_TO_START_OF_ROW,
-                       static_cast<unsigned>(m_terminal_height));
-  locked_stream << ANSI_CLEAR_LINE;
-  locked_stream << ANSI_RESTORE_CURSOR;
-}
-
 void Statusline::UpdateTerminalProperties() {
-  if (m_terminal_size_has_changed == 0)
-    return;
-
-  // Clear the previous statusline using the previous dimensions.
-  Reset();
-
+  UpdateScrollWindow(DisableStatusline);
   m_terminal_width = m_debugger.GetTerminalWidth();
   m_terminal_height = m_debugger.GetTerminalHeight();
-
-  // Set the scroll window based on the new terminal height.
-  UpdateScrollWindow(ScrollWindowShrink);
-
-  // Clear the flag.
-  m_terminal_size_has_changed = 0;
+  UpdateScrollWindow(EnableStatusline);
 }
 
 void Statusline::UpdateScrollWindow(ScrollWindowMode mode) {
+  assert(m_terminal_width != 0 && m_terminal_height != 0);
+
   lldb::LockableStreamFileSP stream_sp = m_debugger.GetOutputStreamSP();
   if (!stream_sp)
     return;
 
   const unsigned scroll_height =
-      (mode == ScrollWindowExtend) ? m_terminal_height : m_terminal_height - 1;
+      (mode == DisableStatusline) ? m_terminal_height : m_terminal_height - 1;
 
   LockedStreamFile locked_stream = stream_sp->Lock();
   locked_stream << ANSI_SAVE_CURSOR;
   locked_stream.Printf(ANSI_SET_SCROLL_ROWS, scroll_height);
   locked_stream << ANSI_RESTORE_CURSOR;
   switch (mode) {
-  case ScrollWindowExtend:
-    // Clear the screen below to hide the old statusline.
-    locked_stream << ANSI_CLEAR_BELOW;
-    break;
-  case ScrollWindowShrink:
+  case EnableStatusline:
     // Move everything on the screen up.
     locked_stream.Printf(ANSI_UP_ROWS, 1);
     locked_stream << '\n';
     break;
+  case DisableStatusline:
+    // Clear the screen below to hide the old statusline.
+    locked_stream << ANSI_CLEAR_BELOW;
+    break;
   }
 }
 


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to