Skip to content

Commit

Permalink
Percent: allow use enum to set type value in JSON config
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Nov 14, 2024
1 parent c9e3f8d commit 1c41c6c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
46 changes: 45 additions & 1 deletion src/common/percent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/common/percent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
7 changes: 6 additions & 1 deletion src/options/display.c
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1c41c6c

Please sign in to comment.