From 6326405c26c5a8ef4f02a830b30e8a39d751f133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 24 May 2024 10:19:58 +0800 Subject: [PATCH] Percentage: support monochrome bar type Fix #961 --- doc/json_schema.json | 2 +- src/common/percent.c | 36 ++++++++++++++++++++++++++++-------- src/common/percent.h | 3 ++- src/data/help.json | 5 +++-- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index 255873158..1a953fa95 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -515,7 +515,7 @@ "properties": { "type": { "type": "number", - "description": "Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only, 9 for colored number", + "description": "Set the percentage output type. 1 for percentage number, 2 for multi-color bar, 3 for both, 6 for bar only, 9 for colored number, 10 for monochrome bar", "minimum": 0, "maximum": 255, "default": 9 diff --git a/src/common/percent.c b/src/common/percent.c index 14ab93f55..0570797eb 100644 --- a/src/common/percent.c +++ b/src/common/percent.c @@ -50,14 +50,34 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFColorRangeConfig con { if(!options->pipe) { - uint32_t section1Begin = (uint32_t) ((green <= yellow ? green : yellow) / 100.0 * options->barWidth + 0.5); - uint32_t section2Begin = (uint32_t) ((green > yellow ? green : yellow) / 100.0 * options->barWidth + 0.5); - if (i == section2Begin) - ffStrbufAppendF(buffer, "\e[%sm", (green > yellow ? colorGreen : colorRed)); - else if (i == section1Begin) - ffStrbufAppendF(buffer, "\e[%sm", colorYellow); - else if (i == 0) - ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? colorGreen : colorRed)); + if (options->percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT) + { + const char* color = NULL; + if (green <= yellow) + { + if (percent < green) color = colorGreen; + else if (percent < yellow) color = colorYellow; + else color = colorRed; + } + else + { + if (percent < yellow) color = colorRed; + else if (percent < green) color = colorYellow; + else color = colorGreen; + } + ffStrbufAppendF(buffer, "\e[%sm", color); + } + else + { + uint32_t section1Begin = (uint32_t) ((green <= yellow ? green : yellow) / 100.0 * options->barWidth + 0.5); + uint32_t section2Begin = (uint32_t) ((green > yellow ? green : yellow) / 100.0 * options->barWidth + 0.5); + if (i == section2Begin) + ffStrbufAppendF(buffer, "\e[%sm", (green > yellow ? colorGreen : colorRed)); + else if (i == section1Begin) + ffStrbufAppendF(buffer, "\e[%sm", colorYellow); + else if (i == 0) + ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? colorGreen : colorRed)); + } } ffStrbufAppend(buffer, &options->barCharElapsed); } diff --git a/src/common/percent.h b/src/common/percent.h index 8ae87708c..33921f689 100644 --- a/src/common/percent.h +++ b/src/common/percent.h @@ -4,12 +4,13 @@ #include "common/parsing.h" #include "common/option.h" -enum +enum FFPercentageTypeFlags { FF_PERCENTAGE_TYPE_NUM_BIT = 1 << 0, FF_PERCENTAGE_TYPE_BAR_BIT = 1 << 1, FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT = 1 << 2, FF_PERCENTAGE_TYPE_NUM_COLOR_BIT = 1 << 3, + FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT = FF_PERCENTAGE_TYPE_NUM_COLOR_BIT, }; // if (green <= yellow) diff --git a/src/data/help.json b/src/data/help.json index fa9670f51..5bf0eba71 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -534,10 +534,11 @@ "desc": "Set the percentage output type", "remark": [ "1 for percentage number", - "2 for bar", + "2 for multi-color bar", "3 for both", "6 for bar only", - "9 for colored number" + "9 for colored number", + "10 for monochrome bar" ], "arg": { "type": "num",