Skip to content

Commit b443a37

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

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

Row.c

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -468,28 +468,47 @@ 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);
473+
int len = snprintf(buffer, sizeof(buffer), "%4.0f", rate);
480474
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);
475+
len = snprintf(buffer, sizeof(buffer), " B/s ");
476+
RichString_appendnAscii(str, shadowColor, buffer, len);
490477
} else {
491-
int len = snprintf(buffer, sizeof(buffer), "%7.2f P/s ", rate / ONE_P);
492-
RichString_appendnAscii(str, largeNumberColor, buffer, len);
478+
size_t unitPrefixIndex = 0;
479+
do {
480+
if (unitPrefixIndex > ARRAYSIZE(unitPrefixes)-1) {
481+
int len = snprintf(buffer, sizeof(buffer), "infinity ");
482+
RichString_appendnAscii(str, largeNumberColor, buffer, len);
483+
return;
484+
}
485+
unitPrefixIndex++;
486+
rate /= ONE_K;
487+
} while (rate >= ONE_K);
488+
489+
unitPrefixIndex--; // unitPrefixes starts from K
490+
491+
int precision = 0;
492+
if (rate <= 99.9) {
493+
if (rate <= 9.99)
494+
precision = 2;
495+
else
496+
precision = 1;
497+
}
498+
499+
if (precision < 2) {
500+
double upper_limit = (precision == 1) ? 10 : 100;
501+
if (rate < upper_limit) {
502+
rate = upper_limit;
503+
}
504+
}
505+
506+
int len = snprintf(buffer, sizeof(buffer), "%4.*f", precision, rate);
507+
int rateDisplayColor = (unitPrefixIndex < 2) ? ((!unitPrefixIndex) ? baseColor : megabytesColor) : largeNumberColor;
508+
RichString_appendnAscii(str, rateDisplayColor, buffer, len);
509+
len = snprintf(buffer, sizeof(buffer), " %c/s ", unitPrefixes[unitPrefixIndex]);
510+
RichString_appendnAscii(str, shadowColor, buffer, len);
511+
493512
}
494513
}
495514

0 commit comments

Comments
 (0)