Skip to content

Commit

Permalink
Optimize DISK I/O Rate precision and add shading for clearer display
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanGitHub committed Oct 21, 2024
1 parent 56e9ee6 commit 28c0201
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions Row.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}

Expand Down

0 comments on commit 28c0201

Please sign in to comment.