Skip to content

Commit 1c41c6c

Browse files
committed
Percent: allow use enum to set type value in JSON config
1 parent c9e3f8d commit 1c41c6c

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/common/percent.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,45 @@ static void appendOutputColor(FFstrbuf* buffer, const FFModuleArgs* module)
1414
ffStrbufAppendF(buffer, "\e[%sm", instance.config.display.colorOutput.chars);
1515
}
1616

17+
const char* ffPercentParseTypeJsonConfig(yyjson_val* jsonVal, FFPercentageTypeFlags* result)
18+
{
19+
if (yyjson_is_uint(jsonVal))
20+
{
21+
*result = (FFPercentageTypeFlags) yyjson_get_uint(jsonVal);
22+
return NULL;
23+
}
24+
if (yyjson_is_arr(jsonVal))
25+
{
26+
FFPercentageTypeFlags flags = 0;
27+
28+
yyjson_val* item;
29+
size_t idx, max;
30+
yyjson_arr_foreach(jsonVal, idx, max, item)
31+
{
32+
const char* flag = yyjson_get_str(item);
33+
if (!flag)
34+
return "Error: percent.type: invalid flag string";
35+
if (ffStrEqualsIgnCase(flag, "num"))
36+
flags |= FF_PERCENTAGE_TYPE_NUM_BIT;
37+
else if (ffStrEqualsIgnCase(flag, "bar"))
38+
flags |= FF_PERCENTAGE_TYPE_BAR_BIT;
39+
else if (ffStrEqualsIgnCase(flag, "hide-others"))
40+
flags |= FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT;
41+
else if (ffStrEqualsIgnCase(flag, "num-color"))
42+
flags |= FF_PERCENTAGE_TYPE_NUM_COLOR_BIT;
43+
else if (ffStrEqualsIgnCase(flag, "bar-monochrome"))
44+
flags |= FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT;
45+
else
46+
return "Error: percent.type: unknown flag string";
47+
}
48+
49+
*result = flags;
50+
return NULL;
51+
}
52+
53+
return "Error: usage: percent.type must be a number or an array of strings";
54+
}
55+
1756
void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConfig config, const FFModuleArgs* module)
1857
{
1958
uint8_t green = config.green, yellow = config.yellow;
@@ -238,7 +277,12 @@ bool ffPercentParseJsonObject(const char* key, yyjson_val* value, FFPercentageMo
238277
yyjson_val* typeVal = yyjson_obj_get(value, "type");
239278
if (typeVal)
240279
{
241-
config->type = (FFPercentageTypeFlags) yyjson_get_int(typeVal);
280+
const char* error = ffPercentParseTypeJsonConfig(typeVal, &config->type);
281+
if (error)
282+
{
283+
fputs(error, stderr);
284+
exit(480);
285+
}
242286
}
243287

244288
return true;

src/common/percent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ typedef struct yyjson_mut_val yyjson_mut_val;
4242
bool ffPercentParseCommandOptions(const char* key, const char* subkey, const char* value, FFPercentageModuleConfig* config);
4343
bool ffPercentParseJsonObject(const char* key, yyjson_val* value, FFPercentageModuleConfig* config);
4444
void ffPercentGenerateJsonConfig(yyjson_mut_doc* doc, yyjson_mut_val* module, FFPercentageModuleConfig defaultConfig, FFPercentageModuleConfig config);
45+
const char* ffPercentParseTypeJsonConfig(yyjson_val* value, FFPercentageTypeFlags* result);

src/options/display.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "fastfetch.h"
22
#include "common/color.h"
33
#include "common/jsonconfig.h"
4+
#include "common/percent.h"
45
#include "util/stringUtils.h"
56
#include "options/display.h"
67

@@ -166,7 +167,11 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
166167
return "display.percent must be an object";
167168

168169
yyjson_val* type = yyjson_obj_get(val, "type");
169-
if (type) options->percentType = (uint8_t) yyjson_get_uint(type);
170+
if (type)
171+
{
172+
const char* error = ffPercentParseTypeJsonConfig(type, &options->percentType);
173+
if (error) return error;
174+
}
170175

171176
yyjson_val* ndigits = yyjson_obj_get(val, "ndigits");
172177
if (ndigits) options->percentNdigits = (uint8_t) yyjson_get_uint(ndigits);

0 commit comments

Comments
 (0)