From 0af08bcfc99e9533e84b84e374ce17a3c01b81e1 Mon Sep 17 00:00:00 2001 From: Kumar Date: Thu, 14 Apr 2022 16:35:02 +0530 Subject: Process: Display single digit precision for CPU% greater than 99.9% Since commit edf319e[1], we're dynamically adjusting column width of "CPU%", showing single digit precision also for values greater than "99.9%" makes "CPU%" column consistent with all other values. [1]: edf319e53d1fb77546505e238d75160a3febe56e Change "Process_printPercentage()" function's logic to always display value (i.e. "val") with single precision. Except when value is greater than "99.9%" for columns like "MEM%", whose width is fixed to "4" and value cannot go beyond "100%". Credits: @Explorer09, thanks for the patch[2] to fix title alignment issue. [2]: https://github.com/htop-dev/htop/pull/959#issuecomment-1092480951 Closes: #957 --- Process.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'Process.c') diff --git a/Process.c b/Process.c index 600659c9..55bbae69 100644 --- a/Process.c +++ b/Process.c @@ -739,17 +739,20 @@ void Process_printLeftAlignedField(RichString* str, int attr, const char* conten void Process_printPercentage(float val, char* buffer, int n, uint8_t width, int* attr) { if (val >= 0) { - if (val < 99.9F) { - if (val < 0.05F) { - *attr = CRT_colors[PROCESS_SHADOW]; - } - xSnprintf(buffer, n, "%*.1f ", width, val); - } else { + if (val < 0.05F) + *attr = CRT_colors[PROCESS_SHADOW]; + else if (val >= 99.9F) *attr = CRT_colors[PROCESS_MEGABYTES]; - if (val < 100.0F) - val = 100.0F; // Don't round down and display "val" as "99". - xSnprintf(buffer, n, "%*.0f ", width, val); + + int precision = 1; + + // Display "val" as "100" for columns like "MEM%". + if (width == 4 && val > 99.9F) { + precision = 0; + val = 100.0F; } + + xSnprintf(buffer, n, "%*.*f ", width, precision, val); } else { *attr = CRT_colors[PROCESS_SHADOW]; xSnprintf(buffer, n, "%*.*s ", width, width, "N/A"); @@ -1280,13 +1283,14 @@ void Process_updateFieldWidth(ProcessField key, size_t width) { } void Process_updateCPUFieldWidths(float percentage) { - if (percentage < 99.9) { + if (percentage < 99.9F) { Process_updateFieldWidth(PERCENT_CPU, 4); Process_updateFieldWidth(PERCENT_NORM_CPU, 4); return; } - uint8_t width = ceil(log10(percentage + .2)); + // Add additional two characters, one for "." and another for precision. + uint8_t width = ceil(log10(percentage + 0.1)) + 2; Process_updateFieldWidth(PERCENT_CPU, width); Process_updateFieldWidth(PERCENT_NORM_CPU, width); -- cgit v1.2.3