]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
format sent and rcvd fields correctly for big numbers #66
authorR.E. Wolff <R.E.Wolff@BitWizard.nl>
Tue, 9 Aug 2016 08:07:43 +0000 (10:07 +0200)
committerR.E. Wolff <R.E.Wolff@BitWizard.nl>
Tue, 9 Aug 2016 08:07:43 +0000 (10:07 +0200)
curses.c
mtr.c

index e60fba04d855bc3d6013f38e904e1c92d0b3d6bd..b4201d866bb2c6d683828204da5d4c3530b7b6fb 100644 (file)
--- a/curses.c
+++ b/curses.c
@@ -96,6 +96,41 @@ void pwcenter(char *str)
 }
 
 
+char *format_number (int n, int w, char *buf)
+{
+   // XXX todo: implement w != 5.. 
+   if (w != 5) return ("unimpl");
+
+   if (n < 100000) {
+     snprintf (buf, w+1, "%5d", n);
+     return buf;
+   }
+   if (n < 1000000) {
+     snprintf (buf, w+1, "%3dk%1d", n/1000, (n%1000)/100);
+     return buf;
+   }
+   if (n < 10000000) {
+     snprintf (buf, w+1, "%1dM%03d", n/1000000, (n%1000000)/1000);
+     return buf;
+   }
+   if (n < 100000000) {
+     snprintf (buf, w+1, "%2dM%02d", n/1000000, (n%1000000)/10000);
+     return buf;
+   }
+   if (n < 1000000000) {
+     snprintf (buf, w+1, "%3dM%01d", n/1000000, (n%1000000)/100000);
+     return buf;
+   }
+   //if (n < 10000000000) {
+     snprintf (buf, w+1, "%1dG%03d", n/1000000000, (n%1000000000)/1000000);
+     return buf;
+   //}
+
+   //return ("big");
+}
+
+
+
 int mtr_curses_keyaction(void)
 {
   int c = getch();
@@ -320,6 +355,21 @@ int mtr_curses_keyaction(void)
 }
 
 
+void format_field (char *dst, const char *format, int n)
+{
+  if (index (format, 'N' ) ) {
+    *dst++ = ' ';
+    format_number (n, 5, dst);
+  } else if (strchr( format, 'f' ) ) {
+    // this is for fields where we measure integer microseconds but
+    // display floating point miliseconds. Convert to float here.
+    sprintf(dst, format, n / 1000.0 ); 
+    // this was marked as a temporary hack over 10 years ago. -- REW
+  } else {
+    sprintf(dst, format, n);
+  } 
+} 
+
 void mtr_curses_hosts(int startstat) 
 {
   int max;
@@ -367,15 +417,7 @@ void mtr_curses_hosts(int startstat)
           can't be careful enough. */
        j = fld_index[fld_active[i]];
        if (j == -1) continue; 
-
-       /* temporay hack for stats usec to ms... */
-       if( index( data_fields[j].format, 'f' ) ) {
-         sprintf(buf + hd_len, data_fields[j].format,
-               data_fields[j].net_xxx(at) /1000.0 );
-       } else {
-         sprintf(buf + hd_len, data_fields[j].format,
-               data_fields[j].net_xxx(at) );
-       }
+        format_field (buf+hd_len, data_fields[j].format, data_fields[j].net_xxx(at));
        hd_len +=  data_fields[j].length;
       }
       buf[hd_len] = 0;
diff --git a/mtr.c b/mtr.c
index d748412597d8a5770f4f1c1f74a4bf79385b12bd..608142164f8c2be30e009425d3cb3d328710e89b 100644 (file)
--- a/mtr.c
+++ b/mtr.c
@@ -107,8 +107,8 @@ struct fields data_fields[MAXFLD] = {
   {' ', "<sp>: Space between fields", " ",  " ",        1, &net_drop  },
   {'L', "L: Loss Ratio",          "Loss%",  " %4.1f%%", 6, &net_loss  },
   {'D', "D: Dropped Packets",     "Drop",   " %4d",     5, &net_drop  },
-  {'R', "R: Received Packets",    "Rcv",    " %5d",     6, &net_returned},
-  {'S', "S: Sent Packets",        "Snt",    " %5d",     6, &net_xmit  },
+  {'R', "R: Received Packets",    "Rcv",    " %5N",     6, &net_returned},
+  {'S', "S: Sent Packets",        "Snt",    " %5N",     6, &net_xmit  },
   {'N', "N: Newest RTT(ms)",      "Last",   " %5.1f",   6, &net_last  },
   {'B', "B: Min/Best RTT(ms)",    "Best",   " %5.1f",   6, &net_best  },
   {'A', "A: Average RTT(ms)",     "Avg",    " %5.1f",   6, &net_avg   },