[PATCH] Use less horizontal space in source window

Tom Tromey tom@tromey.com
Fri Aug 16 02:36:00 GMT 2019


The source window currently uses a field width of 6 for line numbers,
and it further aligns to the next tab stop.

This seemed a bit wasteful of horizontal space to me.  This patch
changes the TUI to compute the maximum field width needed for the
current source file, and to only add a single space after the line
number.  Line numbers are now right justified, as well, which I think
also looks better visually when scrolling.

gdb/ChangeLog
2019-08-15  Tom Tromey  <tom@tromey.com>

	* tui/tui-source.c (tui_set_source_content): Compute width of line
	numbers.
	* tui/tui-source.c (copy_source_line): Add "ndigits" parameter.
	Emit fewer spaces between line number and text.
---
 gdb/ChangeLog        |  7 +++++++
 gdb/tui/tui-source.c | 29 ++++++++++++++++++-----------
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/gdb/tui/tui-source.c b/gdb/tui/tui-source.c
index 3d88f66d549..e43b8381fe5 100644
--- a/gdb/tui/tui-source.c
+++ b/gdb/tui/tui-source.c
@@ -37,22 +37,22 @@
 #include "tui/tui-source.h"
 #include "gdb_curses.h"
 
+#include <math.h>
+
 /* A helper function for tui_set_source_content that extracts some
-   source text from PTR.  LINE_NO is the line number; FIRST_COL is the
-   first column to extract, and LINE_WIDTH is the number of characters
-   to display.  Returns a string holding the desired text.  */
+   source text from PTR.  LINE_NO is the line number; NDIGITS the
+   number of digits to use for printing; FIRST_COL is the first column
+   to extract, and LINE_WIDTH is the number of characters to display.
+   Returns a string holding the desired text.  */
 
 static std::string
-copy_source_line (const char **ptr, int line_no, int first_col,
-		  int line_width)
+copy_source_line (const char **ptr, int line_no, int ndigits,
+		  int first_col, int line_width)
 {
   const char *lineptr = *ptr;
 
   /* Init the line with the line number.  */
-  std::string result = string_printf ("%-6d", line_no);
-  int len = result.size ();
-  len = len - ((len / tui_tab_width) * tui_tab_width);
-  result.append (len, ' ');
+  std::string result = string_printf ("%*d ", ndigits, line_no);
 
   int column = 0;
   char c;
@@ -141,8 +141,10 @@ tui_set_source_content (tui_source_window_base *win_info,
       nlines = (line_no + (win_info->height - 2)) - line_no;
 
       std::string srclines;
+      const std::vector<off_t> *offsets;
       if (!g_source_cache.get_source_lines (s, line_no, line_no + nlines,
-					    &srclines))
+					    &srclines)
+	  || !g_source_cache.get_line_charpos (s, &offsets))
 	{
 	  if (!noerror)
 	    {
@@ -172,6 +174,11 @@ tui_set_source_content (tui_source_window_base *win_info,
 	  win_info->start_line_or_addr.loa = LOA_LINE;
 	  cur_line_no = win_info->start_line_or_addr.u.line_no = line_no;
 
+	  double l = log10 (offsets->size ());
+	  int digits = (int) l;
+	  if (l > digits)
+	    ++digits;
+
 	  const char *iter = srclines.c_str ();
 	  win_info->content.resize (nlines);
 	  while (cur_line < nlines)
@@ -181,7 +188,7 @@ tui_set_source_content (tui_source_window_base *win_info,
 
 	      std::string text;
 	      if (*iter != '\0')
-		text = copy_source_line (&iter, cur_line_no,
+		text = copy_source_line (&iter, cur_line_no, digits,
 					 win_info->horizontal_offset,
 					 line_width);
 
-- 
2.17.2



More information about the Gdb-patches mailing list