From fa3b634c071427f44330576cc4b109c5f160ddd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 19 Aug 2025 10:25:52 +0800 Subject: [PATCH] JsonConfig: support JSON5 --- src/fastfetch.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/fastfetch.c b/src/fastfetch.c index 56818ca87..c913b4554 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -376,13 +376,12 @@ static void listModules(bool pretty) } } -static bool parseJsoncFile(const char* path, bool strictJson) +static bool parseJsoncFile(const char* path, yyjson_read_flag flg) { assert(!instance.state.configDoc); { yyjson_read_err error; - yyjson_read_flag flg = strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS; instance.state.configDoc = path ? yyjson_read_file(path, flg, NULL, &error) : yyjson_read_fp(stdin, flg, NULL, &error); @@ -480,11 +479,19 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateS(value); bool strictJson = ffStrbufEndsWithIgnCaseS(&absolutePath, ".json"); - bool needExtension = !strictJson && !ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc"); + bool jsonc = !strictJson && ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc"); + bool json5 = !strictJson && !jsonc && ffStrbufEndsWithIgnCaseS(&absolutePath, ".json5"); + bool needExtension = !strictJson && !jsonc && !json5; if (needExtension) ffStrbufAppendS(&absolutePath, ".jsonc"); - if (parseJsoncFile(absolutePath.chars, strictJson)) return; + yyjson_read_flag flag = strictJson + ? 0 + : jsonc + ? YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS + : YYJSON_READ_JSON5; + + if (parseJsoncFile(absolutePath.chars, flag)) return; //Try to load as a relative path @@ -496,7 +503,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val if (needExtension) ffStrbufAppendS(&absolutePath, ".jsonc"); - if (parseJsoncFile(absolutePath.chars, strictJson)) return; + if (parseJsoncFile(absolutePath.chars, flag)) return; } //Try to load as a relative path with the directory of fastfetch binary @@ -511,7 +518,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val ffStrbufAppendS(&absolutePath, value); if (needExtension) ffStrbufAppendS(&absolutePath, ".jsonc"); - if (parseJsoncFile(absolutePath.chars, strictJson)) return; + if (parseJsoncFile(absolutePath.chars, flag)) return; // Try {exePath}/presets/ ffStrbufSubstrBefore(&absolutePath, lastSlash); @@ -519,7 +526,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val ffStrbufAppendS(&absolutePath, value); if (needExtension) ffStrbufAppendS(&absolutePath, ".jsonc"); - if (parseJsoncFile(absolutePath.chars, strictJson)) return; + if (parseJsoncFile(absolutePath.chars, flag)) return; } //File not found @@ -685,7 +692,12 @@ static void parseConfigFiles(void) uint32_t dirLength = dir->length; ffStrbufAppendS(dir, "fastfetch/config.jsonc"); - bool success = parseJsoncFile(dir->chars, false); + bool success = parseJsoncFile(dir->chars, YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS); + ffStrbufSubstrBefore(dir, dirLength); + if (success) return; + + ffStrbufAppendS(dir, "fastfetch/config.json5"); + success = parseJsoncFile(dir->chars, YYJSON_READ_JSON5); ffStrbufSubstrBefore(dir, dirLength); if (success) return; }