Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/top/dist Avoid steps and other artefacts in hea...



details:   https://anonhg.NetBSD.org/src/rev/b21ab9d3a058
branches:  trunk
changeset: 755950:b21ab9d3a058
user:      is <is%NetBSD.org@localhost>
date:      Wed Jun 30 11:08:12 2010 +0000

description:
Avoid steps and other artefacts in head for more than 9 CPUs.

diffstat:

 external/bsd/top/dist/display.c |  30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)

diffs (54 lines):

diff -r d0f7406673b8 -r b21ab9d3a058 external/bsd/top/dist/display.c
--- a/external/bsd/top/dist/display.c   Wed Jun 30 07:58:11 2010 +0000
+++ b/external/bsd/top/dist/display.c   Wed Jun 30 11:08:12 2010 +0000
@@ -854,7 +854,8 @@
            *ip++ = cpustate_total_length;
            if ((i = strlen(*pp++)) > 0)
            {
-               cpustate_total_length += i + 8;
+               cpustate_total_length += i + 7;
+               /* strlen(" 100% ") is 6, strlen(" 99.9% ") is 7. Never 8. */
            }
        }
     }
@@ -1150,27 +1151,26 @@
 cpustates_tag(int c)
 
 {
-    register const char *use;
+    unsigned width, u;
 
     static char fmttag[100];
 
-    const char *short_tag = ncpu > 1 && multi ? "CPU%d: " : "CPU: ";
-    const char *long_tag = ncpu > 1 && multi ? "CPU%d states: " : "CPU states: ";
+    const char *short_tag = !multi || ncpu <= 1 ? "CPU: " : "CPU%0*d";
+    const char *long_tag = !multi || ncpu <= 1 ?
+       "CPU states: " : "CPU%0*d states: ";
 
-    /* if length + strlen(long_tag) >= screen_width, then we have to
-       use the shorter tag (we subtract 2 to account for ": ") */
-    if (cpustate_total_length + (int)strlen(long_tag) - 2 - ((ncpu > 1) ? 1 : 0)
-       >= screen_width)
-    {
-       use = short_tag;
+    for (width=0, u=ncpu; u>0; u /= 10) {
+       ++width;
     }
-    else
-    {
-       use = long_tag;
+    /* if length + strlen(long_tag) > screen_width, then we have to
+       use the shorter tag */
+
+    snprintf(fmttag, sizeof(fmttag), long_tag, width, c);
+
+    if (cpustate_total_length + (signed)strlen(fmttag)  > screen_width) {
+       snprintf(fmttag, sizeof(fmttag), short_tag, width, c);
     }
 
-    snprintf(fmttag, sizeof(fmttag), use, c);
-
     /* set x_cpustates accordingly then return result */
     x_cpustates = strlen(fmttag);
     return(fmttag);



Home | Main Index | Thread Index | Old Index