Skip to content

Commit bb7768a

Browse files
committed
Optimize DISK I/O Rate precision and add shading for clearer display
1 parent 56e9ee6 commit bb7768a

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

Row.c

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -468,28 +468,39 @@ void Row_printRate(RichString* str, double rate, bool coloring) {
468468
}
469469

470470
if (!isNonnegative(rate)) {
471-
RichString_appendAscii(str, shadowColor, " N/A ");
472-
} else if (rate < 0.005) {
473-
int len = snprintf(buffer, sizeof(buffer), "%7.2f B/s ", rate);
474-
RichString_appendnAscii(str, shadowColor, buffer, len);
471+
RichString_appendAscii(str, shadowColor, " N/A ");
475472
} else if (rate < ONE_K) {
476-
int len = snprintf(buffer, sizeof(buffer), "%7.2f B/s ", rate);
477-
RichString_appendnAscii(str, baseColor, buffer, len);
478-
} else if (rate < ONE_M) {
479-
int len = snprintf(buffer, sizeof(buffer), "%7.2f K/s ", rate / ONE_K);
480-
RichString_appendnAscii(str, baseColor, buffer, len);
481-
} else if (rate < ONE_G) {
482-
int len = snprintf(buffer, sizeof(buffer), "%7.2f M/s ", rate / ONE_M);
483-
RichString_appendnAscii(str, megabytesColor, buffer, len);
484-
} else if (rate < ONE_T) {
485-
int len = snprintf(buffer, sizeof(buffer), "%7.2f G/s ", rate / ONE_G);
486-
RichString_appendnAscii(str, largeNumberColor, buffer, len);
487-
} else if (rate < ONE_P) {
488-
int len = snprintf(buffer, sizeof(buffer), "%7.2f T/s ", rate / ONE_T);
489-
RichString_appendnAscii(str, largeNumberColor, buffer, len);
473+
int len = snprintf(buffer, sizeof(buffer), "%4.0f B/s ", rate);
474+
RichString_appendnAscii(str, shadowColor, buffer, len);
490475
} else {
491-
int len = snprintf(buffer, sizeof(buffer), "%7.2f P/s ", rate / ONE_P);
492-
RichString_appendnAscii(str, largeNumberColor, buffer, len);
476+
size_t unitPrefixIndex = 0;
477+
while (rate >= ONE_K) {
478+
if (unitPrefixIndex > ARRAYSIZE(unitPrefixes)-1) {
479+
int len = snprintf(buffer, sizeof(buffer), " INF ");
480+
RichString_appendnAscii(str, largeNumberColor, buffer, len);
481+
return;
482+
}
483+
unitPrefixIndex++;
484+
rate /= ONE_K;
485+
}
486+
487+
unitPrefixIndex--; // unitPrefixes starts from K
488+
489+
int precision = (rate <= 999.9) ? ((rate <= 99.9) ? ((rate <=9.99) ? 3 : 2) : 1) : 0;
490+
491+
if (precision < 3) {
492+
double upper_limit = (precision < 2) ? ((precision < 1) ? 1000 : 100) : 10;
493+
if (rate < upper_limit) {
494+
rate = upper_limit;
495+
}
496+
}
497+
498+
int len = snprintf(buffer, sizeof(buffer), "%5.*f", precision, rate);
499+
int rateDisplayColor = (unitPrefixIndex < 2) ? ((!unitPrefixIndex) ? baseColor : megabytesColor) : largeNumberColor;
500+
RichString_appendnAscii(str, rateDisplayColor, buffer, len);
501+
len = snprintf(buffer, sizeof(buffer), "%c/s ", unitPrefixes[unitPrefixIndex]);
502+
RichString_appendnAscii(str, shadowColor, buffer, len);
503+
493504
}
494505
}
495506

0 commit comments

Comments
 (0)