]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb/tui] Fix wrapping strings
authorTom de Vries <tdevries@suse.de>
Mon, 4 Dec 2023 07:48:48 +0000 (08:48 +0100)
committerTom de Vries <tdevries@suse.de>
Mon, 4 Dec 2023 07:48:48 +0000 (08:48 +0100)
I noticed that after resizing to a narrow window, I got:
...
┌────────────────┐
│                │
│[ No Source Avail
able ]           │
│                │
└────────────────┘
...

Fix this by adding two new functions:
- tui_win_info::display_string (int y, int x, const char *str)
- tui_win_info::display_string (const char *str)
that make sure that borders are not overwritten, which get us instead:
...
┌────────────────┐
│                │
│[ No Source Avai│
│                │
│                │
└────────────────┘
...

Tested on x86_64-linux.

gdb/testsuite/gdb.tui/narrow.exp [new file with mode: 0644]
gdb/testsuite/lib/tuiterm.exp
gdb/tui/tui-data.c
gdb/tui/tui-data.h
gdb/tui/tui-regs.c
gdb/tui/tui-source.c
gdb/tui/tui-winsource.c

diff --git a/gdb/testsuite/gdb.tui/narrow.exp b/gdb/testsuite/gdb.tui/narrow.exp
new file mode 100644 (file)
index 0000000..7407398
--- /dev/null
@@ -0,0 +1,49 @@
+# Copyright 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test narrow window.
+
+tuiterm_env
+
+# We'd like to start out here with a narrow window, but that's currently not
+# possible.  So instead, we start out with a large one, and resize.
+Term::clean_restart 24 80
+
+if {![Term::enter_tui]} {
+    unsupported "TUI not supported"
+    return
+}
+
+# Disable resize messages.  They are long and will cause wrapping.
+Term::command "maint set tui-resize-message off"
+
+# Scroll previous command off screen.  It is long and will cause wrapping.
+for {set i 0} {$i < 8} {incr i} {
+    Term::command "echo"
+}
+
+Term::check_box "src box" 0 0 80 15
+
+# Resize to narrow screen.
+Term::resize 24 20 0
+
+# Instead of waiting for the resize messages, wait for the resized box.
+Term::wait_for_box "narrow src box" 0 0 20 15
+
+Term::command "layout asm"
+Term::check_box "narrow asm box" 0 0 20 15
+
+Term::command "layout regs"
+Term::check_box "narrow regs box" 0 0 20 8
index 854fde563932a0eb083e1c6ad5f6d0abe5907326..e668fe207e3f5c75c27e52237d5b487dd43dc275 100644 (file)
@@ -1102,6 +1102,21 @@ namespace eval Term {
        }
     }
 
+    # Wait until a box appears at the given coordinates.
+    proc wait_for_box {test_name x y width height} {
+       while 1 {
+           if { [accept_gdb_output] == 0 } {
+               return 0
+           }
+
+           set why [_check_box $x $y $width $height]
+           if {$why == ""} {
+               pass $test_name
+               break
+           }
+       }
+    }
+
     # Check whether the text contents of the terminal match the
     # regular expression.  Note that text styling is not considered.
     proc check_contents {test_name regexp} {
index fc90df25ddd9f6d806eb2fc73bb1e86116567c87..c51bd118165a3229740d143a90e6dff53b063d63 100644 (file)
@@ -164,6 +164,36 @@ tui_win_info::set_title (std::string &&new_title)
     }
 }
 
+/* See tui-data.h.  */
+
+void
+tui_win_info::display_string (int y, int x, const char *str) const
+{
+  int n = width - box_width () - x;
+  if (n <= 0)
+    return;
+
+  mvwaddnstr (handle.get (), y, x, str, n);
+}
+
+/* See tui-data.h.  */
+
+void
+tui_win_info::display_string (const char *str) const
+{
+  int y, x;
+  getyx (handle.get (), y, x);
+
+  /* Avoid Wunused-but-set-variable.  */
+  (void) y;
+
+  int n = width - box_width () - x;
+  if (n <= 0)
+    return;
+
+  waddnstr (handle.get (), str, n);
+}
+
 void
 tui_win_info::rerender ()
 {
index 82340e594eed50c4803b16355d3037ea845cbccf..76d0de3a3f0cf0ce15bae8ff85d0a2eb7e5f18b6 100644 (file)
@@ -167,6 +167,14 @@ public:
   const std::string &title () const
   { return m_title; }
 
+  /* Display string STR in the window at position (Y,X), abbreviated if
+     necessary.  */
+  void display_string (int y, int x, const char *str) const;
+
+  /* Display string STR in the window at the current cursor position,
+     abbreviated if necessary.  */
+  void display_string (const char *str) const;
+
   /* Window handle.  */
   std::unique_ptr<WINDOW, curses_deleter> handle;
   /* Window width.  */
index 01538d499611f44503f861a8b91e09b47e99358c..4c6ea8aff0d45ea2ccd6749299770773bd97f96b 100644 (file)
@@ -407,7 +407,7 @@ tui_data_window::erase_data_content (const char *prompt)
        x_pos = 1;
       else
        x_pos = half_width - strlen (prompt);
-      mvwaddstr (handle.get (), (height / 2), x_pos, (char *) prompt);
+      display_string (height / 2, x_pos, prompt);
     }
   tui_wrefresh (handle.get ());
 }
index 5e9a954f7c2eecd15c8fe55af9b83b6172419014..bce381add28946bb8539615888b196f2e472bd2f 100644 (file)
@@ -253,5 +253,5 @@ tui_source_window::show_line_number (int offset) const
                 tui_left_margin_verbose ? "%0*d%c" : "%*d%c", m_digits - 1,
                 lineno, space);
     }
-  waddstr (handle.get (), text);
+  display_string (text);
 }
index ea4ca219292d5f08ac61fb6089d9a33da9ff6bc5..acbaef95e0c175a2893a2e6880a221223d747132 100644 (file)
@@ -223,10 +223,7 @@ tui_source_window_base::do_erase_source_content (const char *str)
        x_pos = 1;
       else
        x_pos = half_width - strlen (str);
-      mvwaddstr (handle.get (),
-                (height / 2),
-                x_pos,
-                (char *) str);
+      display_string (height / 2, x_pos, str);
 
       refresh_window ();
     }
@@ -697,7 +694,7 @@ tui_source_window_base::update_exec_info (bool refresh_p)
       if (src_element->is_exec_point)
        element[TUI_EXEC_POS] = '>';
 
-      mvwaddstr (handle.get (), i + box_width (), box_width (), element);
+      display_string (i + box_width (), box_width (), element);
 
       show_line_number (i);
     }