mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-09 14:40:10 +01:00
Global: init --format json support (#527)
This commit is contained in:
+30
-85
@@ -21,21 +21,6 @@
|
||||
|
||||
#include "modules/modules.h"
|
||||
|
||||
typedef struct FFCustomValue
|
||||
{
|
||||
bool printKey;
|
||||
FFstrbuf key;
|
||||
FFstrbuf value;
|
||||
} FFCustomValue;
|
||||
|
||||
// Things only needed by fastfetch
|
||||
typedef struct FFdata
|
||||
{
|
||||
FFstrbuf structure;
|
||||
FFlist customValues; // List of FFCustomValue
|
||||
bool loadUserConfig;
|
||||
} FFdata;
|
||||
|
||||
static void constructAndPrintCommandHelpFormat(const char* name, const char* def, uint32_t numArgs, ...)
|
||||
{
|
||||
va_list argp;
|
||||
@@ -968,6 +953,30 @@ static void parseOption(FFdata* data, const char* key, const char* value)
|
||||
data->loadUserConfig = ffOptionParseBoolean(value);
|
||||
else if(ffStrEqualsIgnCase(key, "--processing-timeout"))
|
||||
instance.config.processingTimeout = ffOptionParseInt32(key, value);
|
||||
else if(ffStrEqualsIgnCase(key, "--format"))
|
||||
{
|
||||
switch (ffOptionParseEnum(key, value, (FFKeyValuePair[]) {
|
||||
{ "default", 0},
|
||||
{ "json", 1 },
|
||||
{},
|
||||
}))
|
||||
{
|
||||
case 0:
|
||||
if (instance.state.resultDoc)
|
||||
{
|
||||
yyjson_mut_doc_free(instance.state.resultDoc);
|
||||
instance.state.resultDoc = NULL;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (!instance.state.resultDoc)
|
||||
{
|
||||
instance.state.resultDoc = yyjson_mut_doc_new(NULL);
|
||||
yyjson_mut_doc_set_root(instance.state.resultDoc, yyjson_mut_arr(instance.state.resultDoc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__linux__) || defined(__FreeBSD__)
|
||||
else if(ffStrEqualsIgnCase(key, "--player-name"))
|
||||
@@ -1219,27 +1228,6 @@ static void parseArguments(FFdata* data, int argc, const char** argv)
|
||||
}
|
||||
}
|
||||
|
||||
static void parseStructureCommand(const char* line, FFlist* customValues)
|
||||
{
|
||||
// handle `--set` and `--set-keyless`
|
||||
FF_LIST_FOR_EACH(FFCustomValue, customValue, *customValues)
|
||||
{
|
||||
if (ffStrbufEqualS(&customValue->key, line))
|
||||
{
|
||||
__attribute__((__cleanup__(ffDestroyCustomOptions))) FFCustomOptions options;
|
||||
ffInitCustomOptions(&options);
|
||||
if (customValue->printKey)
|
||||
ffStrbufAppend(&options.moduleArgs.key, &customValue->key);
|
||||
ffStrbufAppend(&options.moduleArgs.outputFormat, &customValue->value);
|
||||
ffPrintCustom(&options);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(!ffParseModuleCommand(line))
|
||||
ffPrintErrorString(line, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "<no implementation provided>");
|
||||
}
|
||||
|
||||
int main(int argc, const char** argv)
|
||||
{
|
||||
ffInitInstance();
|
||||
@@ -1273,25 +1261,9 @@ int main(int argc, const char** argv)
|
||||
const bool useJsonConfig = data.structure.length == 0 && instance.state.configDoc;
|
||||
|
||||
if(useJsonConfig)
|
||||
ffPrintJsonConfig(true);
|
||||
ffPrintJsonConfig(true /* prepare */);
|
||||
else
|
||||
{
|
||||
//If we don't have a custom structure, use the default one
|
||||
if(data.structure.length == 0)
|
||||
ffStrbufAppendS(&data.structure, FASTFETCH_DATATEXT_STRUCTURE);
|
||||
|
||||
if(ffStrbufContainIgnCaseS(&data.structure, FF_CPUUSAGE_MODULE_NAME))
|
||||
ffPrepareCPUUsage();
|
||||
|
||||
if(instance.config.multithreading)
|
||||
{
|
||||
if(ffStrbufContainIgnCaseS(&data.structure, FF_PUBLICIP_MODULE_NAME))
|
||||
ffPreparePublicIp(&instance.config.publicIP);
|
||||
|
||||
if(ffStrbufContainIgnCaseS(&data.structure, FF_WEATHER_MODULE_NAME))
|
||||
ffPrepareWeather(&instance.config.weather);
|
||||
}
|
||||
}
|
||||
ffPrepareCommandOption(&data);
|
||||
|
||||
ffStart();
|
||||
|
||||
@@ -1300,40 +1272,13 @@ int main(int argc, const char** argv)
|
||||
#endif
|
||||
|
||||
if (useJsonConfig)
|
||||
{
|
||||
ffPrintJsonConfig(false);
|
||||
}
|
||||
else
|
||||
ffPrintCommandOption(&data);
|
||||
|
||||
if (instance.state.resultDoc)
|
||||
{
|
||||
//Parse the structure and call the modules
|
||||
uint32_t startIndex = 0;
|
||||
while (startIndex < data.structure.length)
|
||||
{
|
||||
uint32_t colonIndex = ffStrbufNextIndexC(&data.structure, startIndex, ':');
|
||||
data.structure.chars[colonIndex] = '\0';
|
||||
|
||||
uint64_t ms = 0;
|
||||
if(__builtin_expect(instance.config.stat, false))
|
||||
ms = ffTimeGetTick();
|
||||
|
||||
parseStructureCommand(data.structure.chars + startIndex, &data.customValues);
|
||||
|
||||
if(__builtin_expect(instance.config.stat, false))
|
||||
{
|
||||
char str[32];
|
||||
int len = snprintf(str, sizeof str, "%" PRIu64 "ms", ffTimeGetTick() - ms);
|
||||
if(instance.config.pipe)
|
||||
puts(str);
|
||||
else
|
||||
printf("\033[s\033[1A\033[9999999C\033[%dD%s\033[u", len, str); // Save; Up 1; Right 9999999; Left <len>; Print <str>; Load
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
if (!instance.config.noBuffer) fflush(stdout);
|
||||
#endif
|
||||
|
||||
startIndex = colonIndex + 1;
|
||||
}
|
||||
yyjson_mut_write_fp(stdout, instance.state.resultDoc, YYJSON_WRITE_ALLOW_INF_AND_NAN | YYJSON_WRITE_PRETTY_TWO_SPACES, NULL, NULL);
|
||||
}
|
||||
|
||||
ffFinish();
|
||||
|
||||
Reference in New Issue
Block a user