Percent: clamps bar width to 0% ~ 100%

This commit is contained in:
Carter Li
2026-10-10 13:35:11 +08:00
parent 07a7211526
commit 132e69b580
3 changed files with 18 additions and 3 deletions
+2 -1
View File
@@ -22,7 +22,8 @@ Bugfixes:
* Fixed `/boot` being hidden by default on Haiku, where it is the system volume. (Disk, Haiku)
* Improved Snapdragon X1/X2 model name detection on Linux. (#2625, CPU, Linux)
* Fixed the Android build failing with `GL/gl.h` not found, because the NDK ships the EGL headers that turned OpenGL detection on but not the desktop GL headers it includes. (OpenGL, Android)
* Fixed MAC address reporting 20:00:00:00:00:00 on macOS 27. (LocalIP, Apple)
* Fixed MAC address reporting `20:00:00:00:00:00` on macOS 27. (LocalIP, Apple)
* Fixed assertion failure when percentage exceeds 100 (Percent)
Logos:
* Added filled Artix logo (#2631)
+5 -2
View File
@@ -88,8 +88,11 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
FFPercentageTypeFlags percentType = config.type == 0 ? options->percentType : config.type;
uint8_t blocksPercent = (uint8_t) (percent / 100.0 * options->barWidth + 0.5);
assert(blocksPercent <= options->barWidth);
uint8_t blocksPercent = 0;
if (percent > 0) {
double blocks = percent / 100.0 * options->barWidth + 0.5;
blocksPercent = (uint8_t) (blocks >= options->barWidth ? options->barWidth : blocks);
}
bool autoColorElapsed = ffStrbufIgnCaseEqualS(&options->barColorElapsed, "auto");
+11
View File
@@ -180,6 +180,17 @@ int main(void) {
VERIFY_BAR(14, config, "[ ■--------- ]");
VERIFY_BAR(15, config, "[ ■■-------- ]");
// A value above 100 saturates the bar. That is reachable from real data: the load average is
// normalized against the number of cores, so it exceeds 100% as soon as the load does.
VERIFY_BAR(101, config, "[ ■■■■■■■■■■ ]");
VERIFY_BAR(150, config, "[ ■■■■■■■■■■ ]");
// Narrowing to `uint8_t` before clamping would wrap around here and silently render 4 blocks
VERIFY_BAR(2600, config, "[ ■■■■■■■■■■ ]");
// A negative value fills nothing
VERIFY_BAR(-60, config, "[ ---------- ]");
options->barWidth = 4;
VERIFY_BAR(0, config, "[ ---- ]");
VERIFY_BAR(25, config, "[ ■--- ]");