From 10f5fa1e7d76e8bac30e9dd4e384997a0b600085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 6 Jun 2024 16:36:57 +0800 Subject: [PATCH] Percentage: add `--bar-border-{left|right}`; remove `--bar-border` --- CHANGELOG.md | 4 ++++ doc/json_schema.json | 13 +++++++++---- src/common/percent.c | 14 ++++++-------- src/data/help.json | 25 ++++++++++++++++--------- src/options/display.c | 25 +++++++++++++++++-------- src/options/display.h | 3 ++- 6 files changed, 54 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7e5e5047..1a1eb81e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 2.15.0 +Changes: +* `--bar-border ` has been changed to `--bar-border-left ` and `--bar-border-right `, which are used for customizing the style of bar border. + * `--bar-border-left '' --bar-border-right ''` can be used to disable the border + Features: * Add ability to skip installing license with INSTALL_LICENSE option (CMake) * Make it possible to shorten the theme and icons output (Theme / Icons) diff --git a/doc/json_schema.json b/doc/json_schema.json index ba4d6cdca..3533c651f 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -499,10 +499,15 @@ "description": "Set the character to use in total part", "default": "-" }, - "border": { - "type": "boolean", - "description": "Whether to show a border around the bar", - "default": true + "borderLeft": { + "type": "string", + "description": "Set the string to use at left border", + "default": "[ " + }, + "borderRight": { + "type": "string", + "description": "Set the string to use at right border", + "default": " ]" }, "width": { "type": "integer", diff --git a/src/common/percent.c b/src/common/percent.c index 0570797eb..9cc6e9d45 100644 --- a/src/common/percent.c +++ b/src/common/percent.c @@ -24,12 +24,11 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFColorRangeConfig con uint32_t blocksPercent = (uint32_t) (percent / 100.0 * options->barWidth + 0.5); assert(blocksPercent <= options->barWidth); - if(options->barBorder) + if(options->barBorderLeft.length) { if(!options->pipe) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m[ "); - else - ffStrbufAppendS(buffer, "[ "); + ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m"); + ffStrbufAppend(buffer, &options->barBorderLeft); } if (percent != percent) @@ -91,12 +90,11 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFColorRangeConfig con } } - if(options->barBorder) + if(options->barBorderRight.length) { if(!options->pipe) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m ]");\ - else - ffStrbufAppendS(buffer, " ]"); + ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m"); + ffStrbufAppend(buffer, &options->barBorderRight); } if(!options->pipe) diff --git a/src/data/help.json b/src/data/help.json index cf563a88e..cbecc9032 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -616,6 +616,22 @@ "default": "-" } }, + { + "long": "bar-border-left", + "desc": "Set the string to use at left border of percentage bars", + "arg": { + "type": "string", + "default": "[ " + } + }, + { + "long": "bar-border-right", + "desc": "Set the string to use at right border of percentage bars", + "arg": { + "type": "string", + "default": " ]" + } + }, { "long": "bar-width", "desc": "Set the width of percentage bars, in number of characters", @@ -624,15 +640,6 @@ "default": 10 } }, - { - "long": "bar-border", - "desc": "Whether to show a border around percentage bars", - "arg": { - "type": "bool", - "optional": true, - "default": true - } - }, { "long": "no-buffer", "desc": "Set if the stdout application buffer should be disabled", diff --git a/src/options/display.c b/src/options/display.c index bd9651801..65e9e4c65 100644 --- a/src/options/display.c +++ b/src/options/display.c @@ -180,9 +180,13 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va if (charTotal) ffStrbufSetS(&options->barCharTotal, charTotal); - yyjson_val* border = yyjson_obj_get(val, "border"); - if (border) - options->barBorder = yyjson_get_bool(border); + yyjson_val* borderLeft = yyjson_obj_get(val, "border-left"); + if (borderLeft) + ffStrbufSetS(&options->barBorderLeft, yyjson_get_str(borderLeft)); + + yyjson_val* borderRight = yyjson_obj_get(val, "border-right"); + if (borderRight) + ffStrbufSetS(&options->barBorderRight, yyjson_get_str(borderRight)); yyjson_val* width = yyjson_obj_get(val, "width"); if (width) @@ -346,8 +350,10 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key ffOptionParseString(key, value, &options->barCharTotal); else if(ffStrEqualsIgnCase(subkey, "width")) options->barWidth = (uint8_t) ffOptionParseUInt32(key, value); - else if(ffStrEqualsIgnCase(subkey, "border")) - options->barBorder = ffOptionParseBoolean(value); + else if(ffStrEqualsIgnCase(subkey, "border-left")) + ffOptionParseString(key, value, &options->barBorderLeft); + else if(ffStrEqualsIgnCase(subkey, "border-right")) + ffOptionParseString(key, value, &options->barBorderRight); else return false; } @@ -392,8 +398,9 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options) ffStrbufInitStatic(&options->barCharElapsed, "■"); ffStrbufInitStatic(&options->barCharTotal, "-"); + ffStrbufInitStatic(&options->barBorderLeft, "[ "); + ffStrbufInitStatic(&options->barBorderRight, " ]"); options->barWidth = 10; - options->barBorder = true; options->percentType = 9; options->percentNdigits = 0; ffStrbufInitStatic(&options->percentColorGreen, FF_COLOR_FG_GREEN); @@ -561,8 +568,10 @@ void ffOptionsGenerateDisplayJsonConfig(FFOptionsDisplay* options, yyjson_mut_do yyjson_mut_obj_add_strbuf(doc, bar, "charElapsed", &options->barCharElapsed); if (!ffStrbufEqual(&options->barCharTotal, &defaultOptions.barCharTotal)) yyjson_mut_obj_add_strbuf(doc, bar, "charTotal", &options->barCharTotal); - if (options->barBorder != defaultOptions.barBorder) - yyjson_mut_obj_add_bool(doc, bar, "border", options->barBorder); + if (!ffStrbufEqual(&options->barBorderLeft, &defaultOptions.barBorderLeft)) + yyjson_mut_obj_add_strbuf(doc, bar, "borderLeft", &options->barBorderLeft); + if (!ffStrbufEqual(&options->barBorderRight, &defaultOptions.barBorderRight)) + yyjson_mut_obj_add_strbuf(doc, bar, "borderRight", &options->barBorderRight); if (options->barWidth != defaultOptions.barWidth) yyjson_mut_obj_add_uint(doc, bar, "width", options->barWidth); diff --git a/src/options/display.h b/src/options/display.h index aaba0d412..7f248042b 100644 --- a/src/options/display.h +++ b/src/options/display.h @@ -43,8 +43,9 @@ typedef struct FFOptionsDisplay FFstrbuf tempColorRed; FFstrbuf barCharElapsed; FFstrbuf barCharTotal; + FFstrbuf barBorderLeft; + FFstrbuf barBorderRight; uint8_t barWidth; - bool barBorder; uint8_t percentType; uint8_t percentNdigits; FFstrbuf percentColorGreen;