From e12642e0b9ee09bf78cd97c1d90f2e259c320f65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 30 Apr 2024 09:15:57 +0800 Subject: [PATCH] Fastfetch: make config in jsonc overridable Fix #821 --- src/fastfetch.c | 60 ++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/fastfetch.c b/src/fastfetch.c index 3959d7f8a..629c4dad0 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -321,21 +321,42 @@ static void parseOption(FFdata* data, const char* key, const char* value); static bool parseJsoncFile(const char* path) { - yyjson_read_err error; - yyjson_doc* doc = yyjson_read_file(path, YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS | YYJSON_READ_ALLOW_INF_AND_NAN, NULL, &error); - if (!doc) + assert(!instance.state.configDoc); + { - if (error.code != YYJSON_READ_ERROR_FILE_OPEN) + yyjson_read_err error; + instance.state.configDoc = yyjson_read_file(path, YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS | YYJSON_READ_ALLOW_INF_AND_NAN, NULL, &error); + if (!instance.state.configDoc) { - fprintf(stderr, "Error: failed to parse JSON config file `%s` at pos %zu: %s\n", path, error.pos, error.msg); + if (error.code != YYJSON_READ_ERROR_FILE_OPEN) + { + fprintf(stderr, "Error: failed to parse JSON config file `%s` at pos %zu: %s\n", path, error.pos, error.msg); + exit(477); + } + return false; + } + } + + { + const char* error = NULL; + + yyjson_val* const root = yyjson_doc_get_root(instance.state.configDoc); + if (!yyjson_is_obj(root)) + error = "Invalid JSON config format. Root value must be an object"; + + if ( + error || + (error = ffOptionsParseLogoJsonConfig(&instance.config.logo, root)) || + (error = ffOptionsParseGeneralJsonConfig(&instance.config.general, root)) || + (error = ffOptionsParseDisplayJsonConfig(&instance.config.display, root)) || + (error = ffOptionsParseLibraryJsonConfig(&instance.config.library, root)) || + false + ) { + fprintf(stderr, "JsonConfig Error: %s\n", error); exit(477); } - return false; } - if (instance.state.configDoc) - yyjson_doc_free(instance.state.configDoc); // for `--load-config` - instance.state.configDoc = doc; return true; } @@ -779,27 +800,6 @@ static void parseArguments(FFdata* data, int argc, char** argv, void (*parser)(F static void run(FFdata* data) { - if (instance.state.configDoc) - { - const char* error = NULL; - - yyjson_val* const root = yyjson_doc_get_root(instance.state.configDoc); - if (!yyjson_is_obj(root)) - error = "Invalid JSON config format. Root value must be an object"; - - if ( - error || - (error = ffOptionsParseLogoJsonConfig(&instance.config.logo, root)) || - (error = ffOptionsParseGeneralJsonConfig(&instance.config.general, root)) || - (error = ffOptionsParseDisplayJsonConfig(&instance.config.display, root)) || - (error = ffOptionsParseLibraryJsonConfig(&instance.config.library, root)) || - false - ) { - fprintf(stderr, "JsonConfig Error: %s\n", error); - exit(477); - } - } - const bool useJsonConfig = data->structure.length == 0 && instance.state.configDoc; if (useJsonConfig)