mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Global: simplify code of module matching
This commit is contained in:
+15
-304
@@ -3,319 +3,30 @@
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
static inline bool tryModule(const char* type, void* options)
|
||||
bool ffParseModuleCommand(const char* type)
|
||||
{
|
||||
FFModuleBaseInfo* baseInfo = (FFModuleBaseInfo*) options;
|
||||
if (ffStrEqualsIgnCase(type, baseInfo->name))
|
||||
if(!isalpha(type[0])) return false;
|
||||
|
||||
for (FFModuleBaseInfo** modules = ffModuleInfos[toupper(type[0]) - 'A']; *modules; ++modules)
|
||||
{
|
||||
baseInfo->printModule(options);
|
||||
return true;
|
||||
FFModuleBaseInfo* baseInfo = *modules;
|
||||
if (ffStrEqualsIgnCase(type, baseInfo->name))
|
||||
{
|
||||
baseInfo->printModule(baseInfo);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ffParseModuleCommand(const char* type)
|
||||
{
|
||||
FFconfig* cfg = &instance.config;
|
||||
switch (toupper(type[0]))
|
||||
{
|
||||
case 'B': {
|
||||
return
|
||||
tryModule(type, &cfg->battery) ||
|
||||
tryModule(type, &cfg->bios) ||
|
||||
tryModule(type, &cfg->bluetooth) ||
|
||||
tryModule(type, &cfg->board) ||
|
||||
tryModule(type, &cfg->break_) ||
|
||||
tryModule(type, &cfg->brightness) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'C': {
|
||||
return
|
||||
tryModule(type, &cfg->chassis) ||
|
||||
tryModule(type, &cfg->command) ||
|
||||
tryModule(type, &cfg->colors) ||
|
||||
tryModule(type, &cfg->cpu) ||
|
||||
tryModule(type, &cfg->cpuUsage) ||
|
||||
tryModule(type, &cfg->cursor) ||
|
||||
tryModule(type, &cfg->custom) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'D': {
|
||||
return
|
||||
tryModule(type, &cfg->dateTime) ||
|
||||
tryModule(type, &cfg->de) ||
|
||||
tryModule(type, &cfg->display) ||
|
||||
tryModule(type, &cfg->disk) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'F': {
|
||||
return
|
||||
tryModule(type, &cfg->font) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'G': {
|
||||
return
|
||||
tryModule(type, &cfg->gamepad) ||
|
||||
tryModule(type, &cfg->gpu) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'H': {
|
||||
return
|
||||
tryModule(type, &cfg->host) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'I': {
|
||||
return
|
||||
tryModule(type, &cfg->icons) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'K': {
|
||||
return
|
||||
tryModule(type, &cfg->kernel) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'L': {
|
||||
return
|
||||
tryModule(type, &cfg->lm) ||
|
||||
tryModule(type, &cfg->locale) ||
|
||||
tryModule(type, &cfg->localIP) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'M': {
|
||||
return
|
||||
tryModule(type, &cfg->media) ||
|
||||
tryModule(type, &cfg->memory) ||
|
||||
tryModule(type, &cfg->monitor) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'O': {
|
||||
return
|
||||
tryModule(type, &cfg->openCL) ||
|
||||
tryModule(type, &cfg->openGL) ||
|
||||
tryModule(type, &cfg->os) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'P': {
|
||||
return
|
||||
tryModule(type, &cfg->packages) ||
|
||||
tryModule(type, &cfg->player) ||
|
||||
tryModule(type, &cfg->powerAdapter) ||
|
||||
tryModule(type, &cfg->processes) ||
|
||||
tryModule(type, &cfg->publicIP) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'S': {
|
||||
return
|
||||
tryModule(type, &cfg->separator) ||
|
||||
tryModule(type, &cfg->shell) ||
|
||||
tryModule(type, &cfg->sound) ||
|
||||
tryModule(type, &cfg->swap) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'T': {
|
||||
return
|
||||
tryModule(type, &cfg->terminal) ||
|
||||
tryModule(type, &cfg->terminalFont) ||
|
||||
tryModule(type, &cfg->terminalSize) ||
|
||||
tryModule(type, &cfg->title) ||
|
||||
tryModule(type, &cfg->theme) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'U': {
|
||||
return
|
||||
tryModule(type, &cfg->uptime) ||
|
||||
tryModule(type, &cfg->users) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'V': {
|
||||
return
|
||||
tryModule(type, &cfg->vulkan) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'W': {
|
||||
return
|
||||
tryModule(type, &cfg->wallpaper) ||
|
||||
tryModule(type, &cfg->weather) ||
|
||||
tryModule(type, &cfg->wm) ||
|
||||
tryModule(type, &cfg->wifi) ||
|
||||
tryModule(type, &cfg->wmTheme) ||
|
||||
false;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool tryModuleCommandOptions(const char* key, const char* value, void* options)
|
||||
{
|
||||
FFModuleBaseInfo* baseInfo = (FFModuleBaseInfo*) options;
|
||||
return baseInfo->parseCommandOptions(options, key, value);
|
||||
}
|
||||
|
||||
bool ffParseModuleOptions(const char* key, const char* value)
|
||||
{
|
||||
if (!ffStrStartsWith(key, "--")) return false;
|
||||
FFconfig* cfg = &instance.config;
|
||||
if (!ffStrStartsWith(key, "--") || !isalpha(key[2])) return false;
|
||||
|
||||
switch (toupper(key[2]))
|
||||
for (FFModuleBaseInfo** modules = ffModuleInfos[toupper(key[2]) - 'A']; *modules; ++modules)
|
||||
{
|
||||
case 'B': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->battery) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->bios) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->bluetooth) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->board) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->break_) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->brightness) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'C': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->chassis) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->command) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->colors) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->cpu) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->cpuUsage) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->cursor) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->custom) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'D': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->dateTime) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->de) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->display) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->disk) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'F': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->font) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'G': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->gamepad) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->gpu) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'H': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->host) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'I': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->icons) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'K': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->kernel) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'L': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->lm) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->locale) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->localIP) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'M': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->media) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->memory) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->monitor) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'O': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->openCL) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->openGL) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->os) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'P': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->packages) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->player) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->powerAdapter) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->processes) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->publicIP) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'S': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->separator) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->shell) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->sound) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->swap) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'T': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->terminal) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->terminalFont) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->terminalSize) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->title) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->theme) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'U': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->uptime) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->users) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'V': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->vulkan) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'W': {
|
||||
return
|
||||
tryModuleCommandOptions(key, value, &cfg->wallpaper) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->weather) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->wm) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->wifi) ||
|
||||
tryModuleCommandOptions(key, value, &cfg->wmTheme) ||
|
||||
false;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
FFModuleBaseInfo* baseInfo = *modules;
|
||||
if (baseInfo->parseCommandOptions(baseInfo, key, value)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
+11
-155
@@ -70,167 +70,23 @@ const char* ffJsonConfigParseEnum(yyjson_val* val, int* result, FFKeyValuePair p
|
||||
return "Invalid enum value type; must be a string or integer";
|
||||
}
|
||||
|
||||
static inline bool tryModule(const char* type, yyjson_val* module, void* options)
|
||||
static bool parseModuleJsonObject(const char* type, yyjson_val* jsonVal)
|
||||
{
|
||||
FFModuleBaseInfo* baseInfo = (FFModuleBaseInfo*) options;
|
||||
if (ffStrEqualsIgnCase(type, baseInfo->name))
|
||||
if(!isalpha(type[0])) return false;
|
||||
|
||||
for (FFModuleBaseInfo** modules = ffModuleInfos[toupper(type[0]) - 'A']; *modules; ++modules)
|
||||
{
|
||||
if (module) baseInfo->parseJsonObject(options, module);
|
||||
baseInfo->printModule(options);
|
||||
return true;
|
||||
FFModuleBaseInfo* baseInfo = *modules;
|
||||
if (ffStrEqualsIgnCase(type, baseInfo->name))
|
||||
{
|
||||
if (jsonVal) baseInfo->parseJsonObject(baseInfo, jsonVal);
|
||||
baseInfo->printModule(baseInfo);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool parseModuleJsonObject(const char* type, yyjson_val* module)
|
||||
{
|
||||
FFconfig* cfg = &instance.config;
|
||||
switch (toupper(type[0]))
|
||||
{
|
||||
case 'B': {
|
||||
return
|
||||
tryModule(type, module, &cfg->battery) ||
|
||||
tryModule(type, module, &cfg->bios) ||
|
||||
tryModule(type, module, &cfg->bluetooth) ||
|
||||
tryModule(type, module, &cfg->board) ||
|
||||
tryModule(type, module, &cfg->break_) ||
|
||||
tryModule(type, module, &cfg->brightness) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'C': {
|
||||
return
|
||||
tryModule(type, module, &cfg->chassis) ||
|
||||
tryModule(type, module, &cfg->command) ||
|
||||
tryModule(type, module, &cfg->colors) ||
|
||||
tryModule(type, module, &cfg->cpu) ||
|
||||
tryModule(type, module, &cfg->cpuUsage) ||
|
||||
tryModule(type, module, &cfg->cursor) ||
|
||||
tryModule(type, module, &cfg->custom) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'D': {
|
||||
return
|
||||
tryModule(type, module, &cfg->dateTime) ||
|
||||
tryModule(type, module, &cfg->de) ||
|
||||
tryModule(type, module, &cfg->display) ||
|
||||
tryModule(type, module, &cfg->disk) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'F': {
|
||||
return
|
||||
tryModule(type, module, &cfg->font) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'G': {
|
||||
return
|
||||
tryModule(type, module, &cfg->gamepad) ||
|
||||
tryModule(type, module, &cfg->gpu) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'H': {
|
||||
return
|
||||
tryModule(type, module, &cfg->host) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'I': {
|
||||
return
|
||||
tryModule(type, module, &cfg->icons) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'K': {
|
||||
return
|
||||
tryModule(type, module, &cfg->kernel) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'L': {
|
||||
return
|
||||
tryModule(type, module, &cfg->lm) ||
|
||||
tryModule(type, module, &cfg->locale) ||
|
||||
tryModule(type, module, &cfg->localIP) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'M': {
|
||||
return
|
||||
tryModule(type, module, &cfg->media) ||
|
||||
tryModule(type, module, &cfg->memory) ||
|
||||
tryModule(type, module, &cfg->monitor) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'O': {
|
||||
return
|
||||
tryModule(type, module, &cfg->openCL) ||
|
||||
tryModule(type, module, &cfg->openGL) ||
|
||||
tryModule(type, module, &cfg->os) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'P': {
|
||||
return
|
||||
tryModule(type, module, &cfg->packages) ||
|
||||
tryModule(type, module, &cfg->player) ||
|
||||
tryModule(type, module, &cfg->powerAdapter) ||
|
||||
tryModule(type, module, &cfg->processes) ||
|
||||
tryModule(type, module, &cfg->publicIP) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'S': {
|
||||
return
|
||||
tryModule(type, module, &cfg->separator) ||
|
||||
tryModule(type, module, &cfg->shell) ||
|
||||
tryModule(type, module, &cfg->sound) ||
|
||||
tryModule(type, module, &cfg->swap) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'T': {
|
||||
return
|
||||
tryModule(type, module, &cfg->terminal) ||
|
||||
tryModule(type, module, &cfg->terminalFont) ||
|
||||
tryModule(type, module, &cfg->terminalSize) ||
|
||||
tryModule(type, module, &cfg->title) ||
|
||||
tryModule(type, module, &cfg->theme) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'U': {
|
||||
return
|
||||
tryModule(type, module, &cfg->uptime) ||
|
||||
tryModule(type, module, &cfg->users) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'V': {
|
||||
return
|
||||
tryModule(type, module, &cfg->vulkan) ||
|
||||
false;
|
||||
}
|
||||
|
||||
case 'W': {
|
||||
return
|
||||
tryModule(type, module, &cfg->wallpaper) ||
|
||||
tryModule(type, module, &cfg->weather) ||
|
||||
tryModule(type, module, &cfg->wm) ||
|
||||
tryModule(type, module, &cfg->wifi) ||
|
||||
tryModule(type, module, &cfg->wmTheme) ||
|
||||
false;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void prepareModuleJsonObject(const char* type, yyjson_val* module)
|
||||
{
|
||||
FFconfig* cfg = &instance.config;
|
||||
|
||||
Reference in New Issue
Block a user