diff --git a/Row.c b/Row.c index d795787e6..f18214565 100644 --- a/Row.c +++ b/Row.c @@ -468,28 +468,49 @@ void Row_printRate(RichString* str, double rate, bool coloring) { } if (!isNonnegative(rate)) { - RichString_appendAscii(str, shadowColor, " N/A "); - } else if (rate < 0.005) { - int len = snprintf(buffer, sizeof(buffer), "%7.2f B/s ", rate); - RichString_appendnAscii(str, shadowColor, buffer, len); + RichString_appendAscii(str, shadowColor, " N/A "); } else if (rate < ONE_K) { - int len = snprintf(buffer, sizeof(buffer), "%7.2f B/s ", rate); - RichString_appendnAscii(str, baseColor, buffer, len); - } else if (rate < ONE_M) { - int len = snprintf(buffer, sizeof(buffer), "%7.2f K/s ", rate / ONE_K); - RichString_appendnAscii(str, baseColor, buffer, len); - } else if (rate < ONE_G) { - int len = snprintf(buffer, sizeof(buffer), "%7.2f M/s ", rate / ONE_M); - RichString_appendnAscii(str, megabytesColor, buffer, len); - } else if (rate < ONE_T) { - int len = snprintf(buffer, sizeof(buffer), "%7.2f G/s ", rate / ONE_G); - RichString_appendnAscii(str, largeNumberColor, buffer, len); - } else if (rate < ONE_P) { - int len = snprintf(buffer, sizeof(buffer), "%7.2f T/s ", rate / ONE_T); - RichString_appendnAscii(str, largeNumberColor, buffer, len); + int len = snprintf(buffer, sizeof(buffer), "%4.0f B/s ", rate); + RichString_appendnAscii(str, shadowColor, buffer, len); } else { - int len = snprintf(buffer, sizeof(buffer), "%7.2f P/s ", rate / ONE_P); - RichString_appendnAscii(str, largeNumberColor, buffer, len); + size_t unitPrefixIndex = 0; + do { + if (unitPrefixIndex > ARRAYSIZE(unitPrefixes)-1) { + int len = snprintf(buffer, sizeof(buffer), " INF "); + RichString_appendnAscii(str, largeNumberColor, buffer, len); + return; + } + unitPrefixIndex++; + rate /= ONE_K; + } while (rate >= ONE_K); + + unitPrefixIndex--; // unitPrefixes starts from K + + int precision = 0; + if (rate <= 999.9) { + if (rate <= 99.99) { + if (rate <= 9.999) + precision = 3; + else + precision = 2; + } + else + precision = 1; + } + + if (precision < 3) { + double upper_limit = (precision < 2) ? ((precision < 1) ? 1000 : 100) : 10; + if (rate < upper_limit) { + rate = upper_limit; + } + } + + int len = snprintf(buffer, sizeof(buffer), "%5.*f", precision, rate); + int rateDisplayColor = (unitPrefixIndex < 2) ? ((!unitPrefixIndex) ? baseColor : megabytesColor) : largeNumberColor; + RichString_appendnAscii(str, rateDisplayColor, buffer, len); + len = snprintf(buffer, sizeof(buffer), "%c/s ", unitPrefixes[unitPrefixIndex]); + RichString_appendnAscii(str, shadowColor, buffer, len); + } }