Global: support --bar-width

This commit is contained in:
李通洲 2023-08-16 16:46:21 +08:00
parent 4b714c7057
commit a0c4f2febd
No known key found for this signature in database
GPG Key ID: 269AD4F5325A22A3
17 changed files with 84 additions and 43 deletions

View File

@ -7,7 +7,7 @@ Changes:
Features:
* Add `--key-width` for aligning the left edge of values, supported both for global `--key-width` and specific module `--module-key-width`
* Add `--bar-char-elapsed`, `--bar-char-total` and `--bar-border` options
* Add `--bar-char-elapsed`, `--bar-char-total`, `--bar-width` and `--bar-border` options
Bugfixes:
* Fix label detection. Use `--disk-key 'Disk ({2})'` to display it (Disk, Linux)

View File

@ -370,6 +370,12 @@
"type": "boolean",
"title": "Whether to show a border around the bar",
"default": true
},
"width": {
"type": "integer",
"title": "Set the width of the bar",
"minimum": 1,
"default": 10
}
}
},

28
presets/examples/9.jsonc Normal file
View File

@ -0,0 +1,28 @@
{
"$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
"logo": {
"type": "small"
},
"display": {
"percentType": 6,
"keyWidth": 11,
"bar": {
"charElapsed": "=",
"charTotal": "-",
"width": 13
}
},
"modules": [
"title",
"separator",
"memory",
"swap",
"disk",
"battery",
{
"type": "colors",
"paddingLeft": 11,
"symbol": "circle"
}
]
}

View File

@ -2,19 +2,15 @@
#include "common/color.h"
#include "util/textModifier.h"
// green, yellow, red: print the color on nth (0~9) block
// set its value == 10 means the color will not be printed
void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, uint8_t red)
void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, uint8_t red)
{
assert(green <= 10 && yellow <= 10 && red <= 10);
assert(green <= 100 && yellow <= 100 && red <= 100);
// [ 0%, 5%) prints 0 blocks
// [ 5%, 15%) prints 1 block;
// ...
// [85%, 95%) prints 9 blocks;
// [95%,100%] prints 10 blocks
percent = (uint8_t)(percent + 5) / 10;
assert(percent <= 10);
uint32_t blocksPercent = (uint32_t) (percent / 100.0 * instance.config.barWidth + 0.5);
uint32_t blocksGreen = (uint32_t) (green / 100.0 * instance.config.barWidth + 0.5);
uint32_t blocksYellow = (uint32_t) (yellow / 100.0 * instance.config.barWidth + 0.5);
uint32_t blocksRed = (uint32_t) (red / 100.0 * instance.config.barWidth + 0.5);
assert(blocksPercent <= instance.config.barWidth);
if(instance.config.barBorder)
{
@ -24,25 +20,25 @@ void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_
ffStrbufAppendS(buffer, "[ ");
}
for (uint8_t i = 0; i < percent; ++i)
for (uint32_t i = 0; i < blocksPercent; ++i)
{
if(!instance.config.pipe)
{
if (i == green)
if (i == blocksGreen)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
else if (i == yellow)
else if (i == blocksYellow)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m");
else if (i == red)
else if (i == blocksRed)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
}
ffStrbufAppend(buffer, &instance.config.barCharElapsed);
}
if (percent < 10)
if (blocksPercent < instance.config.barWidth)
{
if(!instance.config.pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
for (uint8_t i = percent; i < 10; ++i)
for (uint32_t i = blocksPercent; i < instance.config.barWidth; ++i)
ffStrbufAppend(buffer, &instance.config.barCharTotal);
}
@ -67,7 +63,7 @@ void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_
// [green, 100]: print green
// [yellow, green): print yellow
// [0, yellow): PRINT RED
void ffAppendPercentNum(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, bool parentheses)
void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, bool parentheses)
{
assert(green <= 100 && yellow <= 100);
@ -97,7 +93,7 @@ void ffAppendPercentNum(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
}
}
ffStrbufAppendF(buffer, "%u%%", (unsigned) percent);
ffStrbufAppendF(buffer, "%u%%", (unsigned) (percent + 0.5));
if (colored && !instance.config.pipe)
{

View File

@ -13,7 +13,7 @@ enum
FF_PERCENTAGE_TYPE_NUM_COLOR_BIT = 1 << 3,
};
void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, uint8_t red);
void ffAppendPercentNum(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, bool parentheses);
void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, uint8_t red);
void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, bool parentheses);
#endif

View File

@ -72,6 +72,7 @@ static void defaultConfig(void)
ffStrbufInitStatic(&instance.config.barCharElapsed, "");
ffStrbufInitStatic(&instance.config.barCharTotal, "-");
instance.config.barWidth = 10;
instance.config.barBorder = true;
instance.config.percentType = 1;

View File

@ -443,12 +443,18 @@ const char* ffParseDisplayJsonConfig(void)
const char* charElapsed = yyjson_get_str(yyjson_obj_get(val, "charElapsed"));
if (charElapsed)
ffStrbufSetS(&config->barCharElapsed, charElapsed);
const char* charTotal = yyjson_get_str(yyjson_obj_get(val, "charTotal"));
if (charTotal)
ffStrbufSetS(&config->barCharTotal, charTotal);
yyjson_val* border = yyjson_obj_get(val, "border");
if (border)
config->barBorder = yyjson_get_bool(border);
yyjson_val* width = yyjson_obj_get(val, "width");
if (width)
config->barWidth = (uint8_t) yyjson_get_uint(width);
}
else
return "display.bar must be an object";

View File

@ -71,6 +71,7 @@ Display options:
--percent-type <value>: Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only, 9 for colored number. Default is 1
--bar-char-elapsed <str>: Set the character to use in elapsed part. Default is '■'
--bar-char-total <str>: Set the character to use in total part. Default is '-'
--bar-width <?num>: Set the width of the bar, in number of characters. Default is 10
--bar-border <?value>: Whether to show a border around the bar. Default is true
--no-buffer <?value>: Set if the stdout application buffer should be disabled. Default is false
--size-ndigits <value>: Set the number of digits to keep after the decimal point when formatting sizes

View File

@ -1051,6 +1051,8 @@ static void parseOption(FFdata* data, const char* key, const char* value)
ffOptionParseString(key, value, &instance.config.barCharElapsed);
else if(ffStrEqualsIgnCase(key, "--bar-char-total"))
ffOptionParseString(key, value, &instance.config.barCharTotal);
else if(ffStrEqualsIgnCase(key, "--bar-width"))
instance.config.barWidth = (uint8_t) ffOptionParseUInt32(key, value);
else if(ffStrEqualsIgnCase(key, "--bar-border"))
instance.config.barBorder = ffOptionParseBoolean(value);
else if(ffStrEqualsIgnCase(key, "--no-buffer"))

View File

@ -56,6 +56,7 @@ typedef struct FFconfig
FFTemperatureUnit temperatureUnit;
FFstrbuf barCharElapsed;
FFstrbuf barCharTotal;
uint8_t barWidth;
bool barBorder;
uint32_t percentType;
bool pipe; //disables logo and all escape sequences

View File

@ -26,11 +26,11 @@ static void printBattery(FFBatteryOptions* options, BatteryResult* result, uint8
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
if(result->capacity <= 20)
ffAppendPercentBar(&str, (uint8_t)result->capacity, 10, 10, 0);
ffAppendPercentBar(&str, result->capacity, 100, 100, 0);
else if(result->capacity <= 50)
ffAppendPercentBar(&str, (uint8_t)result->capacity, 10, 0, 10);
ffAppendPercentBar(&str, result->capacity, 100, 0, 100);
else
ffAppendPercentBar(&str, (uint8_t)result->capacity, 0, 10, 10);
ffAppendPercentBar(&str, result->capacity, 0, 100, 100);
}
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
@ -38,7 +38,7 @@ static void printBattery(FFBatteryOptions* options, BatteryResult* result, uint8
if(str.length > 0)
ffStrbufAppendC(&str, ' ');
ffAppendPercentNum(&str, (uint8_t) result->capacity, 51, 21, str.length > 0);
ffAppendPercentNum(&str, result->capacity, 51, 21, str.length > 0);
}
}

View File

@ -51,7 +51,7 @@ void ffPrintBrightness(FFBrightnessOptions* options)
if (instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffAppendPercentBar(&str, (uint8_t) (item->value + 0.5), 0, 10, 10);
ffAppendPercentBar(&str, item->value, 0, 100, 100);
}
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
@ -59,7 +59,7 @@ void ffPrintBrightness(FFBrightnessOptions* options)
if(str.length > 0)
ffStrbufAppendC(&str, ' ');
ffAppendPercentNum(&str, (uint8_t) (item->value + 0.5), 10, 10, str.length > 0);
ffAppendPercentNum(&str, item->value, 10, 10, str.length > 0);
}
ffStrbufPutTo(&str, stdout);

View File

@ -25,12 +25,12 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options)
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
ffAppendPercentBar(&str, (uint8_t)percentage, 0, 5, 8);
ffAppendPercentBar(&str, percentage, 0, 50, 80);
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
{
if(str.length > 0)
ffStrbufAppendC(&str, ' ');
ffAppendPercentNum(&str, (uint8_t) percentage, 50, 80, str.length > 0);
ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0);
}
ffStrbufPutTo(&str, stdout);
}

View File

@ -46,7 +46,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk)
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
ffParseSize(disk->bytesTotal, &totalPretty);
uint8_t bytesPercentage = disk->bytesTotal > 0 ? (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0) : 0;
double bytesPercentage = disk->bytesTotal > 0 ? (double) disk->bytesUsed / (double) disk->bytesTotal * 100.0 : 0;
if(options->moduleArgs.outputFormat.length == 0)
{
@ -58,7 +58,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk)
{
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffAppendPercentBar(&str, bytesPercentage, 0, 5, 8);
ffAppendPercentBar(&str, bytesPercentage, 0, 50, 80);
ffStrbufAppendC(&str, ' ');
}
@ -67,7 +67,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk)
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
{
ffAppendPercentNum(&str, (uint8_t) bytesPercentage, 50, 80, str.length > 0);
ffAppendPercentNum(&str, bytesPercentage, 50, 80, str.length > 0);
ffStrbufAppendC(&str, ' ');
}
}

View File

@ -58,7 +58,7 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu
if(gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
{
ffStrbufAppendS(&output, ", ");
ffAppendPercentNum(&output, (uint8_t) (gpu->dedicated.used * 100 / gpu->dedicated.total), 50, 80, false);
ffAppendPercentNum(&output, (double) gpu->dedicated.used / (double) gpu->dedicated.total * 100.0, 50, 80, false);
}
ffStrbufAppendC(&output, ')');
}

View File

@ -25,9 +25,9 @@ void ffPrintMemory(FFMemoryOptions* options)
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
ffParseSize(storage.bytesTotal, &totalPretty);
uint8_t percentage = storage.bytesTotal == 0
double percentage = storage.bytesTotal == 0
? 0
: (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0);
: (double) storage.bytesUsed / (double) storage.bytesTotal * 100.0;
if(options->moduleArgs.outputFormat.length == 0)
{
@ -40,7 +40,7 @@ void ffPrintMemory(FFMemoryOptions* options)
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffAppendPercentBar(&str, percentage, 0, 5, 8);
ffAppendPercentBar(&str, percentage, 0, 50, 80);
ffStrbufAppendC(&str, ' ');
}
@ -48,7 +48,7 @@ void ffPrintMemory(FFMemoryOptions* options)
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
ffAppendPercentNum(&str, (uint8_t) percentage, 50, 80, str.length > 0);
ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0);
ffStrbufTrimRight(&str, ' ');
ffStrbufPutTo(&str, stdout);

View File

@ -25,9 +25,9 @@ void ffPrintSwap(FFSwapOptions* options)
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
ffParseSize(storage.bytesTotal, &totalPretty);
uint8_t percentage = storage.bytesTotal == 0
double percentage = storage.bytesTotal == 0
? 0
: (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0);
: (double) storage.bytesUsed / (double) storage.bytesTotal * 100.0;
if(options->moduleArgs.outputFormat.length == 0)
{
@ -37,7 +37,7 @@ void ffPrintSwap(FFSwapOptions* options)
{
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffAppendPercentBar(&str, 0, 0, 5, 8);
ffAppendPercentBar(&str, 0, 0, 50, 80);
ffStrbufAppendC(&str, ' ');
}
if(!(instance.config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
@ -47,7 +47,7 @@ void ffPrintSwap(FFSwapOptions* options)
{
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffAppendPercentBar(&str, percentage, 0, 5, 8);
ffAppendPercentBar(&str, percentage, 0, 50, 80);
ffStrbufAppendC(&str, ' ');
}
@ -55,7 +55,7 @@ void ffPrintSwap(FFSwapOptions* options)
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
ffAppendPercentNum(&str, (uint8_t) percentage, 50, 80, str.length > 0);
ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0);
}
ffStrbufTrimRight(&str, ' ');