On Sat, Oct 12, 2019 at 02:51:04PM -0400, Ted Unangst wrote:
> Klemens Nanni wrote:
> > On Sat, Oct 12, 2019 at 09:53:44AM -0600, Theo de Raadt wrote:
> > > I am suggesting you put the spaces after the cpu#.
> > Is this better?
> > 
> > 4   CPUs:  0.0% user,  0.0% nice,  0.0% sys,  0.0% spin,  0.0% intr,  100% 
> > idle
> > 
> > CPU  0  :  0.0% user,  0.0% nice,  0.0% sys,  0.0% spin,  0.0% intr,  100% 
> > idle
> > CPU  1  :  0.0% user,  0.0% nice,  0.0% sys,  0.0% spin,  0.0% intr,  100% 
> > idle
> 
> 
> CPU 0:    0.0% user,  0.0% nice,  0.0% sys,  0.0% spin,  0.0% intr,  100% idle
> CPU 10:   0.0% user,  0.0% nice,  0.0% sys,  0.0% spin,  0.0% intr,  100% idle
> CPU 100:  0.0% user,  0.0% nice,  0.0% sys,  0.0% spin,  0.0% intr,  100% idle

maybe this...?

$ ./obj/top -d1                                                          
load averages:  2.99,  2.71,  1.78    jetsam.local 16:47:23
65 processes: 61 idle, 4 on processor  up 9 days, 22:29:29
CPU 0:     0.3% user,  0.0% nice,  0.1% sys,  0.0% spin,  0.1% intr, 99.6% idle
CPU 1:     0.3% user,  0.0% nice,  0.1% sys,  0.0% spin,  0.0% intr, 99.6% idle
CPU 2:     0.3% user,  0.0% nice,  0.1% sys,  0.0% spin,  0.0% intr, 99.6% idle
CPU 3:     0.2% user,  0.0% nice,  0.0% sys,  0.0% spin,  0.0% intr, 99.8% idle
Memory: Real: 468M/11G act/tot Free: 4351M Cache: 9615M Swap: 0K/32G

$ ./obj/top -d1 -1
load averages:  2.99,  2.72,  1.79    jetsam.local 16:47:30
65 processes: 61 idle, 4 on processor  up 9 days, 22:29:36
4   CPUs:  0.3% user,  0.0% nice,  0.1% sys,  0.0% spin,  0.0% intr, 99.7% idle
Memory: Real: 468M/11G act/tot Free: 4351M Cache: 9615M Swap: 0K/32G

Index: display.c
===================================================================
RCS file: /cvs/src/usr.bin/top/display.c,v
retrieving revision 1.59
diff -u -p -r1.59 display.c
--- display.c   3 Jul 2019 03:24:02 -0000       1.59
+++ display.c   12 Oct 2019 21:47:05 -0000
@@ -72,7 +72,7 @@ FILE           *debug;
 
 static int      display_width = MAX_COLS;
 
-static char    *cpustates_tag(int);
+static void     cpustates_tag(int);
 static int      string_count(char **);
 static void     summary_format(char *, size_t, int *, char **);
 static int     readlinedumb(char *, int);
@@ -338,49 +339,22 @@ i_procstates(int total, int *states, int
 
 /* cpustates_tag() calculates the correct tag to use to label the line */
 
-static char *
+static void
 cpustates_tag(int cpu)
 {
-       if (screen_length > 3 || !smart_terminal) {
-               static char *tag;
-               static int cpulen, old_width;
-               int i;
-
-               if (cpulen == 0 && ncpu > 1) {
-                       /* compute length of the cpu string */
-                       for (i = ncpu; i > 0; cpulen++, i /= 10)
-                               continue;
-               }
+       char buf[32];
 
-               if (old_width == screen_width) {
-                       if (ncpu > 1) {
-                               /* just store the cpu number in the tag */
-                               i = tag[3 + cpulen];
-                               snprintf(tag + 3, cpulen + 1, "%.*d", cpulen, 
cpu);
-                               tag[3 + cpulen] = i;
-                       }
-               } else {
-                       /*
-                        * use a long tag if it will fit, otherwise use short 
one.
-                        */
-                       free(tag);
-                       if (cpustate_total_length + 10 + cpulen >= screen_width)
-                               i = asprintf(&tag, "CPU%.*d: ", cpulen, cpu);
-                       else
-                               i = asprintf(&tag, "CPU%.*d states: ", cpulen, 
cpu);
-                       if (i == -1)
-                               tag = NULL;
-                       else
-                               old_width = screen_width;
-               }
-               return (tag);
+       if (screen_length > 3 || !smart_terminal) {
+               snprintf(buf, sizeof(buf), "CPU %d: ", cpu);
+               printwp("%-10s", buf);
        } else
-               return ("\0");
+               addstrp("");
 }
 
 void
 i_cpustates(int64_t *ostates, int *online)
 {
+       char buf[32];
        int i, first, cpu, cpu_line;
        double value;
        int64_t *states;
@@ -413,7 +387,8 @@ i_cpustates(int64_t *ostates, int *onlin
                        first = 0;
                        move(2, 0);
                        clrtoeol();
-                       printwp("%-3d CPUs: ", ncpuonline);
+                       snprintf(buf, sizeof(buf), "%-3d CPUs: ", ncpuonline);
+                       printwp("%-9s", buf);
 
                        while ((thisname = *names++) != NULL) {
                                if (*thisname != '\0') {
@@ -441,7 +416,7 @@ i_cpustates(int64_t *ostates, int *onlin
                if (screen_length > 2 + cpu_line || !smart_terminal) {
                        move(2 + cpu_line, 0);
                        clrtoeol();
-                       addstrp(cpustates_tag(cpu));
+                       cpustates_tag(cpu);
 
                        while ((thisname = *names++) != NULL) {
                                if (*thisname != '\0') {

Reply via email to