Trimming top's header to display width

From: Giorgos Keramidas <keramida_at_freebsd.org>
Date: Thu, 12 May 2005 15:36:37 +0300
The following patch trims the header printed by top(1) to the
display_width.  A lot of people have complained how top's header wraps
around when some columns are too wide, with the more recent thread
related to this being:

http://lists.freebsd.org/pipermail/freebsd-current/2005-May/050014.html

Does the diff below look like a good way to fix this?

%%%
Index: contrib/top/display.c
===================================================================
RCS file: /tmp/cvsroot/src/contrib/top/display.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- contrib/top/display.c	12 May 2005 01:15:45 -0000	1.1.1.1
+++ contrib/top/display.c	12 May 2005 11:06:21 -0000	1.2
_at__at_ -626,6 +626,33 _at__at_
 static int header_length;
 
 /*
+ * Trim a header string to the current display width and return a newly
+ * allocated area with the trimmed header.
+ */
+
+char *
+trim_header(text)
+
+char *text;
+
+{
+	char *s;
+	int width;
+
+	s = NULL;
+	width = display_width;
+	header_length = strlen(text);
+	if (header_length >= width) {
+		s = malloc((width + 1) * sizeof(char));
+		if (s == NULL)
+			return (NULL);
+		strncpy(s, text, width);
+		s[width] = '\0';
+	}
+	return (s);
+}
+
+/*
  *  *_header(text) - print the header for the process area
  *
  *  Assumptions:  cursor is on the previous line and lastline is consistent
_at__at_ -636,7 +663,12 _at__at_
 char *text;
 
 {
-    header_length = strlen(text);
+    char *s;
+
+    s = trim_header(text);
+    if (s != NULL)
+	text = s;
+
     if (header_status == ON)
     {
 	putchar('\n');
_at__at_ -647,6 +679,7 _at__at_
     {
 	header_status = OFF;
     }
+    free(s);
 }
 
 /*ARGSUSED*/
_at__at_ -655,6 +688,12 _at__at_
 char *text;		/* ignored */
 
 {
+    char *s;
+
+    s = trim_header(text);
+    if (s != NULL)
+	text = s;
+
     if (header_status == ERASE)
     {
 	putchar('\n');
_at__at_ -662,6 +701,7 _at__at_
 	clear_eol(header_length);
 	header_status = OFF;
     }
+    free(s);
 }
 
 /*
%%%
Received on Thu May 12 2005 - 10:36:40 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:34 UTC