Skip to content

Commit 59742bc

Browse files
committed
Fastfetch: improves frequency display config validation and schema
1 parent 303edb9 commit 59742bc

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

doc/json_schema.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,10 +1290,19 @@
12901290
"description": "Set how frequency values should be displayed",
12911291
"properties": {
12921292
"ndigits": {
1293-
"type": "integer",
1294-
"description": "Set the number of digits to keep after the decimal point when formatting frequency values\nA positive value will show the frequency in GHz with decimal\n-1 will show the frequency in MHz",
1295-
"minimum": -1,
1296-
"maximum": 9,
1293+
"description": "Set the number of decimal places to display when formatting frequency values",
1294+
"oneOf": [
1295+
{
1296+
"type": "integer",
1297+
"minimum": 0,
1298+
"maximum": 9,
1299+
"description": "Integer value displays the frequency in GHz with specified decimal places"
1300+
},
1301+
{
1302+
"type": "null",
1303+
"description": "Null value display the frequency as integer MHz"
1304+
}
1305+
],
12971306
"default": 2
12981307
},
12991308
"spaceBeforeUnit": {

src/options/display.c

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
136136
}
137137

138138
yyjson_val* ndigits = yyjson_obj_get(val, "ndigits");
139-
if (ndigits) options->sizeNdigits = (uint8_t) yyjson_get_uint(ndigits);
139+
if (ndigits)
140+
{
141+
if (!yyjson_is_uint(ndigits))
142+
return "display.size.ndigits must be an unsigned integer";
143+
uint64_t val = yyjson_get_uint(ndigits);
144+
if (val > 9)
145+
return "display.size.ndigits must be between 0 and 9";
146+
options->sizeNdigits = (uint8_t) val;
147+
}
140148

141149
yyjson_val* spaceBeforeUnit = yyjson_obj_get(val, "spaceBeforeUnit");
142150
if (spaceBeforeUnit)
@@ -177,7 +185,15 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
177185
}
178186

179187
yyjson_val* ndigits = yyjson_obj_get(val, "ndigits");
180-
if (ndigits) options->tempNdigits = (uint8_t) yyjson_get_uint(ndigits);
188+
if (ndigits)
189+
{
190+
if (!yyjson_is_uint(ndigits))
191+
return "display.temperature.ndigits must be an unsigned integer";
192+
uint64_t val = yyjson_get_uint(ndigits);
193+
if (val > 9)
194+
return "display.temperature.ndigits must be between 0 and 9";
195+
options->tempNdigits = (uint8_t) val;
196+
}
181197

182198
yyjson_val* color = yyjson_obj_get(val, "color");
183199
if (color)
@@ -222,7 +238,15 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
222238
}
223239

224240
yyjson_val* ndigits = yyjson_obj_get(val, "ndigits");
225-
if (ndigits) options->percentNdigits = (uint8_t) yyjson_get_uint(ndigits);
241+
if (ndigits)
242+
{
243+
if (!yyjson_is_uint(ndigits))
244+
return "display.percent.ndigits must be an unsigned integer";
245+
uint64_t val = yyjson_get_uint(ndigits);
246+
if (val > 9)
247+
return "display.percent.ndigits must be between 0 and 9";
248+
options->percentNdigits = (uint8_t) val;
249+
}
226250

227251
yyjson_val* color = yyjson_obj_get(val, "color");
228252
if (color)
@@ -380,7 +404,10 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
380404
{
381405
if (!yyjson_is_int(ndigits))
382406
return "display.fraction.ndigits must be an integer";
383-
options->fractionNdigits = (int8_t) yyjson_get_int(ndigits);
407+
int64_t val = yyjson_get_int(ndigits);
408+
if (val < -1 || val > 9)
409+
return "display.fraction.ndigits must be between -1 and 9";
410+
options->fractionNdigits = (int8_t) val;
384411
}
385412
}
386413
yyjson_val* trailingZeros = yyjson_obj_get(val, "trailingZeros");
@@ -452,7 +479,21 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
452479
return "display.freq must be an object";
453480

454481
yyjson_val* ndigits = yyjson_obj_get(val, "ndigits");
455-
if (ndigits) options->freqNdigits = (int8_t) yyjson_get_int(ndigits);
482+
if (ndigits)
483+
{
484+
if (yyjson_is_null(ndigits))
485+
options->freqNdigits = -1;
486+
else
487+
{
488+
if (!yyjson_is_int(ndigits))
489+
return "display.freq.ndigits must be an integer";
490+
int64_t val = yyjson_get_int(ndigits);
491+
if (val < -1 || val > 9)
492+
return "display.freq.ndigits must be between -1 and 9";
493+
options->freqNdigits = (int8_t) val;
494+
}
495+
options->freqNdigits = (int8_t) yyjson_get_int(ndigits);
496+
}
456497

457498
yyjson_val* spaceBeforeUnit = yyjson_obj_get(val, "spaceBeforeUnit");
458499
if (spaceBeforeUnit)

0 commit comments

Comments
 (0)