]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix TUI text centering
authorTom Tromey <tom@tromey.com>
Fri, 9 Feb 2024 23:22:53 +0000 (16:22 -0700)
committerTom Tromey <tom@tromey.com>
Sat, 2 Mar 2024 01:15:35 +0000 (18:15 -0700)
In a couple of spots, the TUI tries to center some text in the window.
Andrew noticed that the calculation is done strangely and the text
ends up somewhat to the left of center.

This patch fixes the problem.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31355

gdb/testsuite/gdb.tui/regs.exp
gdb/tui/tui-data.c
gdb/tui/tui-data.h
gdb/tui/tui-regs.c
gdb/tui/tui-winsource.c

index ea78b57d1a72c103907a7a8199b49de40af184e2..183c2ea0f036ef1879f8d1e9bc0afe665fa005a0 100644 (file)
@@ -46,7 +46,10 @@ Term::check_box "source box in regs layout" 0 7 80 8
 
 # The current frame is main, check that registers are available.
 set re_reg_vals_unavailable \
-    [string_to_regexp {[ Register Values Unavailable ]}]
+    [string_to_regexp \
+        [string cat \
+             [string repeat " " 23] \
+             {[ Register Values Unavailable ]}]]
 gdb_assert \
     { ![Term::check_region_contents_p 0 0 80 8 $re_reg_vals_unavailable] } \
     "Register values available"
index 38a87a2a6e96328963bee0d03276708c3796e48d..03395d2df09daba0f807e0126b9777421dafd49e 100644 (file)
@@ -169,13 +169,23 @@ 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
+tui_win_info::center_string (const char *str)
 {
-  int n = width - box_width () - x;
-  if (n <= 0)
-    return;
+  werase (handle.get ());
+  check_and_display_highlight_if_needed ();
+
+  int avail_width = width - box_size ();
+  int len = strlen (str);
+
+  int x_pos = box_width ();
+  if (len < avail_width)
+    x_pos += (avail_width - len) / 2;
+
+  int n = avail_width - x_pos;
+  gdb_assert (n > 0);
 
-  mvwaddnstr (handle.get (), y, x, str, n);
+  mvwaddnstr (handle.get (), height / 2, x_pos, str, n);
+  refresh_window ();
 }
 
 /* See tui-data.h.  */
index 90ab01f79afd41f8c39afe7c8af131069752ea6b..ce9c1a8a95a325027923d06a2e00d3fd1bb00000 100644 (file)
@@ -167,9 +167,9 @@ 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;
+  /* Clear the window, maybe draw the boarder, and then display string
+     STR centered in the window, abbreviated if necessary.  */
+  void center_string (const char *str);
 
   /* Display string STR in the window at the current cursor position,
      abbreviated if necessary.  */
index e7c1839656c812eabcba6049b6080ad95f8bd2a9..05211eb7b221f04cfcc945610bc292fd7ae1af37 100644 (file)
@@ -354,17 +354,7 @@ tui_data_window::first_data_item_displayed ()
 void
 tui_data_window::erase_data_content ()
 {
-  werase (handle.get ());
-  check_and_display_highlight_if_needed ();
-
-  const char *prompt = _("[ Register Values Unavailable ]");
-  int half_width = (width - box_size ()) / 2;
-  int x_pos;
-  if (strlen (prompt) >= half_width)
-    x_pos = 1;
-  else
-    x_pos = half_width - strlen (prompt);
-  display_string (height / 2, x_pos, prompt);
+  center_string (_("[ Register Values Unavailable ]"));
 }
 
 /* See tui-regs.h.  */
index 2631714f248f6a2ec143e9eefcdea8bbf04edf6f..6b8716cd27cf9f697302634d96566c15271b7b8d 100644 (file)
@@ -222,23 +222,9 @@ tui_update_source_windows_with_line (struct symtab_and_line sal)
 void
 tui_source_window_base::do_erase_source_content (const char *str)
 {
-  int x_pos;
-  int half_width = (width - box_size ()) / 2;
-
   m_content.clear ();
-  if (handle != NULL)
-    {
-      werase (handle.get ());
-      check_and_display_highlight_if_needed ();
-
-      if (strlen (str) >= half_width)
-       x_pos = 1;
-      else
-       x_pos = half_width - strlen (str);
-      display_string (height / 2, x_pos, str);
-
-      refresh_window ();
-    }
+  if (handle != nullptr)
+    center_string (str);
 }
 
 /* See tui-winsource.h.  */
@@ -714,7 +700,7 @@ tui_source_window_base::update_exec_info (bool refresh_p)
       if (src_element->is_exec_point)
        element[TUI_EXEC_POS] = '>';
 
-      display_string (i + box_width (), box_width (), element);
+      mvwaddstr (handle.get (), i + box_width (), box_width (), element);
 
       show_line_number (i);
     }