Skip to content

Commit fa3b634

Browse files
committed
JsonConfig: support JSON5
1 parent ea86273 commit fa3b634

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/fastfetch.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,12 @@ static void listModules(bool pretty)
376376
}
377377
}
378378

379-
static bool parseJsoncFile(const char* path, bool strictJson)
379+
static bool parseJsoncFile(const char* path, yyjson_read_flag flg)
380380
{
381381
assert(!instance.state.configDoc);
382382

383383
{
384384
yyjson_read_err error;
385-
yyjson_read_flag flg = strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
386385
instance.state.configDoc = path
387386
? yyjson_read_file(path, flg, NULL, &error)
388387
: yyjson_read_fp(stdin, flg, NULL, &error);
@@ -480,11 +479,19 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
480479

481480
FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateS(value);
482481
bool strictJson = ffStrbufEndsWithIgnCaseS(&absolutePath, ".json");
483-
bool needExtension = !strictJson && !ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc");
482+
bool jsonc = !strictJson && ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc");
483+
bool json5 = !strictJson && !jsonc && ffStrbufEndsWithIgnCaseS(&absolutePath, ".json5");
484+
bool needExtension = !strictJson && !jsonc && !json5;
484485
if (needExtension)
485486
ffStrbufAppendS(&absolutePath, ".jsonc");
486487

487-
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
488+
yyjson_read_flag flag = strictJson
489+
? 0
490+
: jsonc
491+
? YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
492+
: YYJSON_READ_JSON5;
493+
494+
if (parseJsoncFile(absolutePath.chars, flag)) return;
488495

489496
//Try to load as a relative path
490497

@@ -496,7 +503,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
496503
if (needExtension)
497504
ffStrbufAppendS(&absolutePath, ".jsonc");
498505

499-
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
506+
if (parseJsoncFile(absolutePath.chars, flag)) return;
500507
}
501508

502509
//Try to load as a relative path with the directory of fastfetch binary
@@ -511,15 +518,15 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
511518
ffStrbufAppendS(&absolutePath, value);
512519
if (needExtension)
513520
ffStrbufAppendS(&absolutePath, ".jsonc");
514-
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
521+
if (parseJsoncFile(absolutePath.chars, flag)) return;
515522

516523
// Try {exePath}/presets/
517524
ffStrbufSubstrBefore(&absolutePath, lastSlash);
518525
ffStrbufAppendS(&absolutePath, "presets/");
519526
ffStrbufAppendS(&absolutePath, value);
520527
if (needExtension)
521528
ffStrbufAppendS(&absolutePath, ".jsonc");
522-
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
529+
if (parseJsoncFile(absolutePath.chars, flag)) return;
523530
}
524531

525532
//File not found
@@ -685,7 +692,12 @@ static void parseConfigFiles(void)
685692
uint32_t dirLength = dir->length;
686693

687694
ffStrbufAppendS(dir, "fastfetch/config.jsonc");
688-
bool success = parseJsoncFile(dir->chars, false);
695+
bool success = parseJsoncFile(dir->chars, YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS);
696+
ffStrbufSubstrBefore(dir, dirLength);
697+
if (success) return;
698+
699+
ffStrbufAppendS(dir, "fastfetch/config.json5");
700+
success = parseJsoncFile(dir->chars, YYJSON_READ_JSON5);
689701
ffStrbufSubstrBefore(dir, dirLength);
690702
if (success) return;
691703
}

0 commit comments

Comments
 (0)