From dfff57dd855cced85dcceed254bdd2c717f8f697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 30 Apr 2026 00:10:35 +0800 Subject: [PATCH] Percent: improve reliability Co-authored-by: Copilot --- src/common/impl/percent.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/common/impl/percent.c b/src/common/impl/percent.c index ee0b8cf3f..f19fbacb7 100644 --- a/src/common/impl/percent.c +++ b/src/common/impl/percent.c @@ -59,9 +59,6 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf const bool borderAsValue = options->barBorderLeftElapsed.length && options->barBorderRightElapsed.length; - uint8_t blocksPercent = (uint8_t) (percent / 100.0 * options->barWidth + 0.5); - assert(blocksPercent <= options->barWidth); - if (!borderAsValue && options->barBorderLeft.length) { if (!options->pipe && options->barColorBorder.length > 0) { ffStrbufAppendF(buffer, "\e[%sm", options->barColorBorder.chars); @@ -86,6 +83,9 @@ 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); + bool autoColorElapsed = ffStrbufIgnCaseEqualS(&options->barColorElapsed, "auto"); bool monochrome = (percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT) || !autoColorElapsed; @@ -190,7 +190,11 @@ void ffPercentAppendNum(FFstrbuf* buffer, double percent, FFPercentageModuleConf } } } - ffStrbufAppendF(buffer, "%*.*f%s%%", options->percentWidth, options->percentNdigits, percent, options->percentSpaceBeforeUnit == FF_SPACE_BEFORE_UNIT_ALWAYS ? " " : ""); + if (percent == -DBL_MAX) { + ffStrbufAppendS(buffer, "-"); + } else { + ffStrbufAppendF(buffer, "%*.*f%s%%", options->percentWidth, options->percentNdigits, percent, options->percentSpaceBeforeUnit == FF_SPACE_BEFORE_UNIT_ALWAYS ? " " : ""); + } if (colored && !options->pipe) { ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);