diff --git a/src/common/jsonconfig.c b/src/common/jsonconfig.c index 8e1ecdaaf..2c6b8c8d0 100644 --- a/src/common/jsonconfig.c +++ b/src/common/jsonconfig.c @@ -231,7 +231,36 @@ static bool parseModuleJsonObject(const char* type, yyjson_val* module) } } -static const char* printJsonConfig(void) +static void prepareModuleJsonObject(const char* type, yyjson_val* module) +{ + FFconfig* cfg = &instance.config; + switch (type[0]) + { + case 'b': case 'B': { + if (ffStrEqualsIgnCase(type, FF_CPUUSAGE_MODULE_NAME)) + ffPrepareCPUUsage(); + break; + } + case 'p': case 'P': { + if (ffStrEqualsIgnCase(type, FF_PUBLICIP_MODULE_NAME)) + { + if (module) ffParsePublicIpJsonObject(&cfg->publicIP, module); + ffPreparePublicIp(&cfg->publicIP); + } + break; + } + case 'w': case 'W': { + if (ffStrEqualsIgnCase(type, FF_WEATHER_MODULE_NAME)) + { + if (module) ffParseWeatherJsonObject(&cfg->weather, module); + ffPrepareWeather(&cfg->weather); + } + break; + } + } +} + +static const char* printJsonConfig(bool prepare) { yyjson_val* const root = yyjson_doc_get_root(instance.state.configDoc); assert(root); @@ -248,7 +277,7 @@ static const char* printJsonConfig(void) yyjson_arr_foreach(modules, idx, max, item) { uint64_t ms = 0; - if(__builtin_expect(instance.config.stat, false)) + if(!prepare && instance.config.stat) ms = ffTimeGetTick(); yyjson_val* module = item; @@ -265,10 +294,12 @@ static const char* printJsonConfig(void) else return "modules must be an array of strings or objects"; - if(!parseModuleJsonObject(type, module)) + if(prepare) + prepareModuleJsonObject(type, module); + else if(!parseModuleJsonObject(type, module)) return "Unknown module type"; - if(__builtin_expect(instance.config.stat, false)) + if(!prepare && instance.config.stat) { char str[32]; int len = snprintf(str, sizeof str, "%" PRIu64 "ms", ffTimeGetTick() - ms); @@ -548,9 +579,9 @@ const char* ffParseLibraryJsonConfig(void) return NULL; } -void ffPrintJsonConfig(void) +void ffPrintJsonConfig(bool prepare) { - const char* error = printJsonConfig(); + const char* error = printJsonConfig(prepare); if (error) ffPrintErrorString("JsonConfig", 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "%s", error); } diff --git a/src/common/jsonconfig.h b/src/common/jsonconfig.h index 7cf6a96e0..e701cae86 100644 --- a/src/common/jsonconfig.h +++ b/src/common/jsonconfig.h @@ -4,7 +4,7 @@ bool ffJsonConfigParseModuleArgs(const char* key, yyjson_val* val, FFModuleArgs* moduleArgs); const char* ffJsonConfigParseEnum(yyjson_val* val, int* result, FFKeyValuePair pairs[]); -void ffPrintJsonConfig(); -const char* ffParseGeneralJsonConfig(); -const char* ffParseDisplayJsonConfig(); -const char* ffParseLibraryJsonConfig(); +void ffPrintJsonConfig(bool prepare); +const char* ffParseGeneralJsonConfig(void); +const char* ffParseDisplayJsonConfig(void); +const char* ffParseLibraryJsonConfig(void); diff --git a/src/detection/cpuusage/cpuusage.c b/src/detection/cpuusage/cpuusage.c index f12cec257..563c6fae5 100644 --- a/src/detection/cpuusage/cpuusage.c +++ b/src/detection/cpuusage/cpuusage.c @@ -35,6 +35,8 @@ const char* ffGetCpuUsageResult(double* result) if(inUseAll2 != inUseAll1) { *result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100; + inUseAll1 = inUseAll2; + totalAll1 = totalAll2; return NULL; } else diff --git a/src/fastfetch.c b/src/fastfetch.c index 6387b9738..0e9589160 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -564,7 +564,7 @@ static bool parseJsoncFile(const char* path) { 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); + fprintf(stderr, "Error: failed to parse JSON config file `%s` at pos %zu: %s\n", path, error.pos, error.msg); exit(477); } return false; @@ -1256,7 +1256,11 @@ int main(int argc, const char** argv) } } - if(data.structure.length > 0 || !instance.state.configDoc) + const bool useJsonConfig = data.structure.length == 0 && instance.state.configDoc; + + if(useJsonConfig) + ffPrintJsonConfig(true); + else { //If we don't have a custom structure, use the default one if(data.structure.length == 0) @@ -1281,9 +1285,9 @@ int main(int argc, const char** argv) if (!instance.config.noBuffer) fflush(stdout); #endif - if (data.structure.length == 0 && instance.state.configDoc) + if (useJsonConfig) { - ffPrintJsonConfig(); + ffPrintJsonConfig(false); } else { diff --git a/src/modules/publicip/publicip.c b/src/modules/publicip/publicip.c index 3dbbc287c..6097b54a3 100644 --- a/src/modules/publicip/publicip.c +++ b/src/modules/publicip/publicip.c @@ -19,6 +19,12 @@ static inline void wrapYyjsonFree(yyjson_doc** doc) void ffPreparePublicIp(FFPublicIpOptions* options) { + if (status != -1) + { + fputs("Error: " FF_PUBLICIP_DISPLAY_NAME " can only be used once due to internal limitations\n", stderr); + exit(1); + } + if (options->url.length == 0) status = ffNetworkingSendHttpRequest(&state, "ipinfo.io", "/json", NULL); else diff --git a/src/modules/weather/weather.c b/src/modules/weather/weather.c index 8ed45a624..68d18d550 100644 --- a/src/modules/weather/weather.c +++ b/src/modules/weather/weather.c @@ -11,6 +11,12 @@ static int status = -1; void ffPrepareWeather(FFWeatherOptions* options) { + if (status != -1) + { + fputs("Error: " FF_WEATHER_MODULE_NAME " can only be used once due to internal limitations\n", stderr); + exit(1); + } + FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/"); if (options->location.length) ffStrbufAppend(&path, &options->location);