Files
fastfetch/src/modules/de/de.c
T

88 lines
2.3 KiB
C
Raw Normal View History

2023-06-07 10:39:53 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-06-07 10:39:53 +08:00
#include "detection/displayserver/displayserver.h"
#include "modules/de/de.h"
#include "util/stringUtils.h"
2023-06-07 10:39:53 +08:00
#define FF_DE_NUM_FORMAT_ARGS 3
void ffPrintDE(FFDEOptions* options)
2023-06-07 10:39:53 +08:00
{
const FFDisplayServerResult* result = ffConnectDisplayServer();
2023-06-07 10:39:53 +08:00
if(result->dePrettyName.length == 0)
{
ffPrintError(FF_DE_MODULE_NAME, 0, &options->moduleArgs, "No DE found");
2023-06-07 10:39:53 +08:00
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_DE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-06-07 10:39:53 +08:00
ffStrbufWriteTo(&result->dePrettyName, stdout);
if(result->deVersion.length > 0)
{
putchar(' ');
ffStrbufWriteTo(&result->deVersion, stdout);
}
putchar('\n');
}
else
{
ffPrintFormat(FF_DE_MODULE_NAME, 0, &options->moduleArgs, FF_DE_NUM_FORMAT_ARGS, (FFformatarg[]){
2023-06-07 10:39:53 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &result->deProcessName},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->dePrettyName},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->deVersion}
});
}
}
void ffInitDEOptions(FFDEOptions* options)
{
options->moduleName = FF_DE_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseDECommandOptions(FFDEOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_DE_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyDEOptions(FFDEOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
void ffParseDEJsonObject(yyjson_val* module)
2023-06-07 10:39:53 +08:00
{
FFDEOptions __attribute__((__cleanup__(ffDestroyDEOptions))) options;
ffInitDEOptions(&options);
if (module)
{
2023-06-10 15:01:54 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-06-07 10:39:53 +08:00
{
2023-06-10 15:01:54 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
2023-06-10 15:01:54 +08:00
continue;
2023-06-07 10:39:53 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
ffPrintError(FF_DE_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
2023-06-07 10:39:53 +08:00
}
}
ffPrintDE(&options);
2023-06-07 10:39:53 +08:00
}