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 20, 2024
1 parent 56e9ee6 commit 829dbe3
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions Row.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ void Row_printNanoseconds(RichString* str, unsigned long long totalNanoseconds,
Row_printTime(str, totalHundredths, coloring);
}

static inline unsigned char Row_getPrecision (double displayRate){
if (displayRate < 10) return 3;
else if (displayRate < 100) return 2;
else if (displayRate < 1000) return 1;
else return 0;
}

void Row_printRate(RichString* str, double rate, bool coloring) {
char buffer[16];

Expand All @@ -468,28 +475,39 @@ 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;
while (rate >= ONE_K) {
if (unitPrefixIndex > ARRAYSIZE(unitPrefixes)-1) {
int len = snprintf(buffer, sizeof(buffer), " INF ");
RichString_appendnAscii(str, largeNumberColor, buffer, len);
return;
}
unitPrefixIndex++;
rate /= ONE_K;
}

unitPrefixIndex--; // unitPrefixes starts from K

int precision = (rate <= 999.9) ? ((rate <= 99.9) ? ((rate <=9.99) ? 3 : 2) : 1) : 0;

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 829dbe3

Please sign in to comment.