Skip to content

Commit 77e0d9d

Browse files
committed
JsonConfig: prepare for module weather and public ip
1 parent 55b9e27 commit 77e0d9d

File tree

6 files changed

+63
-14
lines changed

6 files changed

+63
-14
lines changed

src/common/jsonconfig.c

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,36 @@ static bool parseModuleJsonObject(const char* type, yyjson_val* module)
231231
}
232232
}
233233

234-
static const char* printJsonConfig(void)
234+
static void prepareModuleJsonObject(const char* type, yyjson_val* module)
235+
{
236+
FFconfig* cfg = &instance.config;
237+
switch (type[0])
238+
{
239+
case 'b': case 'B': {
240+
if (ffStrEqualsIgnCase(type, FF_CPUUSAGE_MODULE_NAME))
241+
ffPrepareCPUUsage();
242+
break;
243+
}
244+
case 'p': case 'P': {
245+
if (ffStrEqualsIgnCase(type, FF_PUBLICIP_MODULE_NAME))
246+
{
247+
if (module) ffParsePublicIpJsonObject(&cfg->publicIP, module);
248+
ffPreparePublicIp(&cfg->publicIP);
249+
}
250+
break;
251+
}
252+
case 'w': case 'W': {
253+
if (ffStrEqualsIgnCase(type, FF_WEATHER_MODULE_NAME))
254+
{
255+
if (module) ffParseWeatherJsonObject(&cfg->weather, module);
256+
ffPrepareWeather(&cfg->weather);
257+
}
258+
break;
259+
}
260+
}
261+
}
262+
263+
static const char* printJsonConfig(bool prepare)
235264
{
236265
yyjson_val* const root = yyjson_doc_get_root(instance.state.configDoc);
237266
assert(root);
@@ -248,7 +277,7 @@ static const char* printJsonConfig(void)
248277
yyjson_arr_foreach(modules, idx, max, item)
249278
{
250279
uint64_t ms = 0;
251-
if(__builtin_expect(instance.config.stat, false))
280+
if(!prepare && instance.config.stat)
252281
ms = ffTimeGetTick();
253282

254283
yyjson_val* module = item;
@@ -265,10 +294,12 @@ static const char* printJsonConfig(void)
265294
else
266295
return "modules must be an array of strings or objects";
267296

268-
if(!parseModuleJsonObject(type, module))
297+
if(prepare)
298+
prepareModuleJsonObject(type, module);
299+
else if(!parseModuleJsonObject(type, module))
269300
return "Unknown module type";
270301

271-
if(__builtin_expect(instance.config.stat, false))
302+
if(!prepare && instance.config.stat)
272303
{
273304
char str[32];
274305
int len = snprintf(str, sizeof str, "%" PRIu64 "ms", ffTimeGetTick() - ms);
@@ -548,9 +579,9 @@ const char* ffParseLibraryJsonConfig(void)
548579
return NULL;
549580
}
550581

551-
void ffPrintJsonConfig(void)
582+
void ffPrintJsonConfig(bool prepare)
552583
{
553-
const char* error = printJsonConfig();
584+
const char* error = printJsonConfig(prepare);
554585
if (error)
555586
ffPrintErrorString("JsonConfig", 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "%s", error);
556587
}

src/common/jsonconfig.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
bool ffJsonConfigParseModuleArgs(const char* key, yyjson_val* val, FFModuleArgs* moduleArgs);
66
const char* ffJsonConfigParseEnum(yyjson_val* val, int* result, FFKeyValuePair pairs[]);
7-
void ffPrintJsonConfig();
8-
const char* ffParseGeneralJsonConfig();
9-
const char* ffParseDisplayJsonConfig();
10-
const char* ffParseLibraryJsonConfig();
7+
void ffPrintJsonConfig(bool prepare);
8+
const char* ffParseGeneralJsonConfig(void);
9+
const char* ffParseDisplayJsonConfig(void);
10+
const char* ffParseLibraryJsonConfig(void);

src/detection/cpuusage/cpuusage.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const char* ffGetCpuUsageResult(double* result)
3535
if(inUseAll2 != inUseAll1)
3636
{
3737
*result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
38+
inUseAll1 = inUseAll2;
39+
totalAll1 = totalAll2;
3840
return NULL;
3941
}
4042
else

src/fastfetch.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ static bool parseJsoncFile(const char* path)
564564
{
565565
if (error.code != YYJSON_READ_ERROR_FILE_OPEN)
566566
{
567-
fprintf(stderr, "ERROR: failed to parse JSON config file `%s` at pos %zu: %s\n", path, error.pos, error.msg);
567+
fprintf(stderr, "Error: failed to parse JSON config file `%s` at pos %zu: %s\n", path, error.pos, error.msg);
568568
exit(477);
569569
}
570570
return false;
@@ -1256,7 +1256,11 @@ int main(int argc, const char** argv)
12561256
}
12571257
}
12581258

1259-
if(data.structure.length > 0 || !instance.state.configDoc)
1259+
const bool useJsonConfig = data.structure.length == 0 && instance.state.configDoc;
1260+
1261+
if(useJsonConfig)
1262+
ffPrintJsonConfig(true);
1263+
else
12601264
{
12611265
//If we don't have a custom structure, use the default one
12621266
if(data.structure.length == 0)
@@ -1281,9 +1285,9 @@ int main(int argc, const char** argv)
12811285
if (!instance.config.noBuffer) fflush(stdout);
12821286
#endif
12831287

1284-
if (data.structure.length == 0 && instance.state.configDoc)
1288+
if (useJsonConfig)
12851289
{
1286-
ffPrintJsonConfig();
1290+
ffPrintJsonConfig(false);
12871291
}
12881292
else
12891293
{

src/modules/publicip/publicip.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ static inline void wrapYyjsonFree(yyjson_doc** doc)
1919

2020
void ffPreparePublicIp(FFPublicIpOptions* options)
2121
{
22+
if (status != -1)
23+
{
24+
fputs("Error: " FF_PUBLICIP_DISPLAY_NAME " can only be used once due to internal limitations\n", stderr);
25+
exit(1);
26+
}
27+
2228
if (options->url.length == 0)
2329
status = ffNetworkingSendHttpRequest(&state, "ipinfo.io", "/json", NULL);
2430
else

src/modules/weather/weather.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ static int status = -1;
1111

1212
void ffPrepareWeather(FFWeatherOptions* options)
1313
{
14+
if (status != -1)
15+
{
16+
fputs("Error: " FF_WEATHER_MODULE_NAME " can only be used once due to internal limitations\n", stderr);
17+
exit(1);
18+
}
19+
1420
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/");
1521
if (options->location.length)
1622
ffStrbufAppend(&path, &options->location);

0 commit comments

Comments
 (0)