From e68efb184bf2b2e20e7be30908ba95817d61f481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 11 Jun 2023 21:31:09 +0800 Subject: [PATCH] JsonConfig: support more non-module options --- src/common/jsonconfig.c | 180 ++++++++++++++++++++++++++++++++++++++++ src/common/jsonconfig.h | 3 + src/fastfetch.c | 21 ++--- 3 files changed, 194 insertions(+), 10 deletions(-) diff --git a/src/common/jsonconfig.c b/src/common/jsonconfig.c index 75f433dd0..efd192609 100644 --- a/src/common/jsonconfig.c +++ b/src/common/jsonconfig.c @@ -274,6 +274,186 @@ static const char* printJsonConfig(FFinstance* instance) return NULL; } +const char* ffParseGeneralJsonConfig(FFinstance* instance) +{ + FFconfig* config = &instance->config; + + yyjson_val* const root = yyjson_doc_get_root(instance->state.configDoc); + assert(root); + + if (!yyjson_is_obj(root)) + return "Invalid JSON config format. Root value must be an object"; + + yyjson_val* object = yyjson_obj_get(root, "general"); + if (!object) return NULL; + if (!yyjson_is_obj(object)) return "Property 'general' must be an object"; + + yyjson_val *key_, *val; + size_t idx, max; + yyjson_obj_foreach(object, idx, max, key_, val) + { + const char* key = yyjson_get_str(key_); + + if (strcasecmp(key, "allowSlowOperations") == 0) + config->allowSlowOperations = yyjson_get_bool(val); + else if (strcasecmp(key, "thread") == 0 || strcasecmp(key, "multithreading") == 0) + config->multithreading = yyjson_get_bool(val); + else if (strcasecmp(key, "stat") == 0) + config->stat = yyjson_get_bool(val); + else if (strcasecmp(key, "escapeBedrock") == 0) + config->escapeBedrock = yyjson_get_bool(val); + else if (strcasecmp(key, "pipe") == 0) + config->pipe = yyjson_get_bool(val); + else + return "Unknown general property"; + } + + return NULL; +} + +const char* ffParseDisplayJsonConfig(FFinstance* instance) +{ + FFconfig* config = &instance->config; + + yyjson_val* const root = yyjson_doc_get_root(instance->state.configDoc); + assert(root); + + if (!yyjson_is_obj(root)) + return "Invalid JSON config format. Root value must be an object"; + + yyjson_val* object = yyjson_obj_get(root, "display"); + if (!object) return NULL; + if (!yyjson_is_obj(object)) return "Property 'display' must be an object"; + + yyjson_val *key_, *val; + size_t idx, max; + yyjson_obj_foreach(object, idx, max, key_, val) + { + const char* key = yyjson_get_str(key_); + + if (strcasecmp(key, "showErrors") == 0) + config->showErrors = yyjson_get_bool(val); + else if (strcasecmp(key, "disableLinewrap") == 0) + config->disableLinewrap = yyjson_get_bool(val); + else if (strcasecmp(key, "hideCursor") == 0) + config->hideCursor = yyjson_get_bool(val); + else if (strcasecmp(key, "separator") == 0) + ffStrbufAppendS(&config->keyValueSeparator, yyjson_get_str(val)); + else if (strcasecmp(key, "color") == 0) + { + if (yyjson_is_str(val)) + { + ffOptionParseColor(yyjson_get_str(val), &config->colorKeys); + ffStrbufAppend(&config->colorTitle, &config->colorKeys); + } + else if (yyjson_is_obj(val)) + { + const char* colorKeys = yyjson_get_str(yyjson_obj_get(val, "keys")); + if (colorKeys) + ffOptionParseColor(colorKeys, &config->colorKeys); + const char* colorTitle = yyjson_get_str(yyjson_obj_get(val, "title")); + if (colorTitle) + ffOptionParseColor(colorTitle, &config->colorTitle); + } + else + return "display.color must be either a string or an object"; + } + else if (strcasecmp(key, "binaryPrefix") == 0) + { + int value; + const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) { + { "iec", FF_BINARY_PREFIX_TYPE_IEC }, + { "si", FF_BINARY_PREFIX_TYPE_SI }, + { "jedec", FF_BINARY_PREFIX_TYPE_JEDEC }, + {}, + }); + if (error) return error; + config->binaryPrefixType = (FFBinaryPrefixType) value; + } + else if (strcasecmp(key, "percentType") == 0) + config->percentType = (uint32_t) yyjson_get_uint(val); + else + return "Unknown display property"; + } + + return NULL; +} + +const char* ffParseLibraryJsonConfig(FFinstance* instance) +{ + FFconfig* config = &instance->config; + + yyjson_val* const root = yyjson_doc_get_root(instance->state.configDoc); + assert(root); + + if (!yyjson_is_obj(root)) + return "Invalid JSON config format. Root value must be an object"; + + yyjson_val* object = yyjson_obj_get(root, "library"); + if (!object) return NULL; + if (!yyjson_is_obj(object)) return "Property 'library' must be an object"; + + yyjson_val *key_, *val; + size_t idx, max; + yyjson_obj_foreach(object, idx, max, key_, val) + { + const char* key = yyjson_get_str(key_); + + if (strcasecmp(key, "pci") == 0) + ffStrbufAppendS(&config->libPCI, yyjson_get_str(val)); + else if (strcasecmp(key, "vulkan") == 0) + ffStrbufAppendS(&config->libVulkan, yyjson_get_str(val)); + else if (strcasecmp(key, "freetype") == 0) + ffStrbufAppendS(&config->libfreetype, yyjson_get_str(val)); + else if (strcasecmp(key, "wayland") == 0) + ffStrbufAppendS(&config->libWayland, yyjson_get_str(val)); + else if (strcasecmp(key, "xcbRandr") == 0) + ffStrbufAppendS(&config->libXcbRandr, yyjson_get_str(val)); + else if (strcasecmp(key, "xcb") == 0) + ffStrbufAppendS(&config->libXcb, yyjson_get_str(val)); + else if (strcasecmp(key, "Xrandr") == 0) + ffStrbufAppendS(&config->libXrandr, yyjson_get_str(val)); + else if (strcasecmp(key, "X11") == 0) + ffStrbufAppendS(&config->libX11, yyjson_get_str(val)); + else if (strcasecmp(key, "gio") == 0) + ffStrbufAppendS(&config->libGIO, yyjson_get_str(val)); + else if (strcasecmp(key, "DConf") == 0) + ffStrbufAppendS(&config->libDConf, yyjson_get_str(val)); + else if (strcasecmp(key, "dbus") == 0) + ffStrbufAppendS(&config->libDBus, yyjson_get_str(val)); + else if (strcasecmp(key, "XFConf") == 0) + ffStrbufAppendS(&config->libXFConf, yyjson_get_str(val)); + else if (strcasecmp(key, "sqlite") == 0 || strcasecmp(key, "sqlite3") == 0) + ffStrbufAppendS(&config->libSQLite3, yyjson_get_str(val)); + else if (strcasecmp(key, "rpm") == 0) + ffStrbufAppendS(&config->librpm, yyjson_get_str(val)); + else if (strcasecmp(key, "imagemagick") == 0) + ffStrbufAppendS(&config->libImageMagick, yyjson_get_str(val)); + else if (strcasecmp(key, "z") == 0) + ffStrbufAppendS(&config->libZ, yyjson_get_str(val)); + else if (strcasecmp(key, "chafa") == 0) + ffStrbufAppendS(&config->libChafa, yyjson_get_str(val)); + else if (strcasecmp(key, "egl") == 0) + ffStrbufAppendS(&config->libEGL, yyjson_get_str(val)); + else if (strcasecmp(key, "glx") == 0) + ffStrbufAppendS(&config->libGLX, yyjson_get_str(val)); + else if (strcasecmp(key, "osmesa") == 0) + ffStrbufAppendS(&config->libOSMesa, yyjson_get_str(val)); + else if (strcasecmp(key, "opencl") == 0) + ffStrbufAppendS(&config->libOpenCL, yyjson_get_str(val)); + else if (strcasecmp(key, "wlanapi") == 0) + ffStrbufAppendS(&config->libwlanapi, yyjson_get_str(val)); + else if (strcasecmp(key, "pulse") == 0) + ffStrbufAppendS(&config->libPulse, yyjson_get_str(val)); + else if (strcasecmp(key, "nm") == 0) + ffStrbufAppendS(&config->libnm, yyjson_get_str(val)); + else + return "Unknown library property"; + } + + return NULL; +} + void ffPrintJsonConfig(FFinstance* instance) { const char* error = printJsonConfig(instance); diff --git a/src/common/jsonconfig.h b/src/common/jsonconfig.h index 8a235ae2d..8a72408bc 100644 --- a/src/common/jsonconfig.h +++ b/src/common/jsonconfig.h @@ -5,3 +5,6 @@ bool ffJsonConfigParseModuleArgs(const char* key, yyjson_val* val, FFModuleArgs* moduleArgs); const char* ffJsonConfigParseEnum(yyjson_val* val, int* result, FFKeyValuePair pairs[]); void ffPrintJsonConfig(FFinstance* instance); +const char* ffParseGeneralJsonConfig(FFinstance* instance); +const char* ffParseDisplayJsonConfig(FFinstance* instance); +const char* ffParseLibraryJsonConfig(FFinstance* instance); diff --git a/src/fastfetch.c b/src/fastfetch.c index 208c907df..2b5e5600d 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -527,9 +527,15 @@ static bool parseJsoncFile(FFinstance* instance, const char* path) if (doc) { instance->state.configDoc = doc; - const char* error = ffParseLogoJsonConfig(instance); - if (error) - { + const char* error = NULL; + + if ( + (error = ffParseLogoJsonConfig(instance)) || + (error = ffParseGeneralJsonConfig(instance)) || + (error = ffParseDisplayJsonConfig(instance)) || + (error = ffParseLibraryJsonConfig(instance)) || + false + ) { fputs(error, stderr); exit(477); } @@ -935,6 +941,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con NULL ); } + else if(strcasecmp(key, "--percent-type") == 0) + instance->config.percentType = optionParseUInt32(key, value); /////////////////////// //Module args options// @@ -1049,13 +1057,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con goto error; } - ////////////////// - //Module options// - ////////////////// - - else if(strcasecmp(key, "--percent-type") == 0) - instance->config.percentType = optionParseUInt32(key, value); - ////////////////// //Unknown option// //////////////////