Percent: allow use enum to set type value in JSON config

This commit is contained in:
李通洲
2024-11-14 14:21:55 +08:00
parent c9e3f8d95c
commit 1c41c6ccb1
3 changed files with 52 additions and 2 deletions
+45 -1
View File
@@ -14,6 +14,45 @@ static void appendOutputColor(FFstrbuf* buffer, const FFModuleArgs* module)
ffStrbufAppendF(buffer, "\e[%sm", instance.config.display.colorOutput.chars);
}
const char* ffPercentParseTypeJsonConfig(yyjson_val* jsonVal, FFPercentageTypeFlags* result)
{
if (yyjson_is_uint(jsonVal))
{
*result = (FFPercentageTypeFlags) yyjson_get_uint(jsonVal);
return NULL;
}
if (yyjson_is_arr(jsonVal))
{
FFPercentageTypeFlags flags = 0;
yyjson_val* item;
size_t idx, max;
yyjson_arr_foreach(jsonVal, idx, max, item)
{
const char* flag = yyjson_get_str(item);
if (!flag)
return "Error: percent.type: invalid flag string";
if (ffStrEqualsIgnCase(flag, "num"))
flags |= FF_PERCENTAGE_TYPE_NUM_BIT;
else if (ffStrEqualsIgnCase(flag, "bar"))
flags |= FF_PERCENTAGE_TYPE_BAR_BIT;
else if (ffStrEqualsIgnCase(flag, "hide-others"))
flags |= FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT;
else if (ffStrEqualsIgnCase(flag, "num-color"))
flags |= FF_PERCENTAGE_TYPE_NUM_COLOR_BIT;
else if (ffStrEqualsIgnCase(flag, "bar-monochrome"))
flags |= FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT;
else
return "Error: percent.type: unknown flag string";
}
*result = flags;
return NULL;
}
return "Error: usage: percent.type must be a number or an array of strings";
}
void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConfig config, const FFModuleArgs* module)
{
uint8_t green = config.green, yellow = config.yellow;
@@ -238,7 +277,12 @@ bool ffPercentParseJsonObject(const char* key, yyjson_val* value, FFPercentageMo
yyjson_val* typeVal = yyjson_obj_get(value, "type");
if (typeVal)
{
config->type = (FFPercentageTypeFlags) yyjson_get_int(typeVal);
const char* error = ffPercentParseTypeJsonConfig(typeVal, &config->type);
if (error)
{
fputs(error, stderr);
exit(480);
}
}
return true;
+1
View File
@@ -42,3 +42,4 @@ typedef struct yyjson_mut_val yyjson_mut_val;
bool ffPercentParseCommandOptions(const char* key, const char* subkey, const char* value, FFPercentageModuleConfig* config);
bool ffPercentParseJsonObject(const char* key, yyjson_val* value, FFPercentageModuleConfig* config);
void ffPercentGenerateJsonConfig(yyjson_mut_doc* doc, yyjson_mut_val* module, FFPercentageModuleConfig defaultConfig, FFPercentageModuleConfig config);
const char* ffPercentParseTypeJsonConfig(yyjson_val* value, FFPercentageTypeFlags* result);
+6 -1
View File
@@ -1,6 +1,7 @@
#include "fastfetch.h"
#include "common/color.h"
#include "common/jsonconfig.h"
#include "common/percent.h"
#include "util/stringUtils.h"
#include "options/display.h"
@@ -166,7 +167,11 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
return "display.percent must be an object";
yyjson_val* type = yyjson_obj_get(val, "type");
if (type) options->percentType = (uint8_t) yyjson_get_uint(type);
if (type)
{
const char* error = ffPercentParseTypeJsonConfig(type, &options->percentType);
if (error) return error;
}
yyjson_val* ndigits = yyjson_obj_get(val, "ndigits");
if (ndigits) options->percentNdigits = (uint8_t) yyjson_get_uint(ndigits);