From: R.E. Wolff Date: Tue, 9 Aug 2016 08:07:43 +0000 (+0200) Subject: format sent and rcvd fields correctly for big numbers #66 X-Git-Tag: v0.88~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=caad4c42fb8ef0d830fd37608a96702d6774f93f;p=thirdparty%2Fmtr.git format sent and rcvd fields correctly for big numbers #66 --- diff --git a/curses.c b/curses.c index e60fba0..b4201d8 100644 --- 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 d748412..6081421 100644 --- a/mtr.c +++ b/mtr.c @@ -107,8 +107,8 @@ struct fields data_fields[MAXFLD] = { {' ', ": 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 },