From 786d538276f7fbc5de55123f7c8cf618a5792279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 1 Jun 2023 11:37:52 +0800 Subject: [PATCH] Shell: add JSON config support --- CMakeLists.txt | 2 +- src/common/init.c | 5 +- .../terminalshell/terminalshell_linux.c | 2 +- .../terminalshell/terminalshell_windows.c | 2 +- src/fastfetch.c | 8 +- src/fastfetch.h | 4 +- src/flashfetch.c | 2 +- src/modules/jsonconfig/jsonconfig.c | 1 + src/modules/modules.h | 1 + src/modules/options.h | 1 + src/modules/shell.c | 43 -------- src/modules/shell/option.h | 13 +++ src/modules/shell/shell.c | 99 +++++++++++++++++++ src/modules/shell/shell.h | 15 +++ 14 files changed, 140 insertions(+), 58 deletions(-) delete mode 100644 src/modules/shell.c create mode 100644 src/modules/shell/option.h create mode 100644 src/modules/shell/shell.c create mode 100644 src/modules/shell/shell.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f947d1c40..c85e70f42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -297,7 +297,7 @@ set(LIBFASTFETCH_SRC src/modules/publicip.c src/modules/display/display.c src/modules/separator/separator.c - src/modules/shell.c + src/modules/shell/shell.c src/modules/sound/sound.c src/modules/swap/swap.c src/modules/media.c diff --git a/src/common/init.c b/src/common/init.c index 20b5f0945..cb2352155 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -70,7 +70,7 @@ static void defaultConfig(FFinstance* instance) ffInitUptimeOptions(&instance->config.uptime); initModuleArg(&instance->config.processes); ffInitPackagesOptions(&instance->config.packages); - initModuleArg(&instance->config.shell); + ffInitShellOptions(&instance->config.shell); ffInitDisplayOptions(&instance->config.display); initModuleArg(&instance->config.de); initModuleArg(&instance->config.wm); @@ -133,7 +133,6 @@ static void defaultConfig(FFinstance* instance) ffStrbufInit(&instance->config.libwlanapi); ffStrbufInit(&instance->config.libnm); - instance->config.shellVersion = true; instance->config.terminalVersion = true; instance->config.publicIpTimeout = 0; @@ -292,7 +291,7 @@ static void destroyConfig(FFinstance* instance) ffDestroyUptimeOptions(&instance->config.uptime); destroyModuleArg(&instance->config.processes); ffDestroyPackagesOptions(&instance->config.packages); - destroyModuleArg(&instance->config.shell); + ffDestroyShellOptions(&instance->config.shell); ffDestroyDisplayOptions(&instance->config.display); destroyModuleArg(&instance->config.de); destroyModuleArg(&instance->config.wm); diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index 45bf0091e..931ffdee2 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -353,7 +353,7 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) getUserShellFromEnv(instance, &result); ffStrbufClear(&result.shellVersion); - if(instance->config.shellVersion) + if(instance->config.shell.version) { getShellVersion(&result.shellExe, result.shellExeName, &result.shellVersion); diff --git a/src/detection/terminalshell/terminalshell_windows.c b/src/detection/terminalshell/terminalshell_windows.c index 9af70f8ca..6b5c9b9c1 100644 --- a/src/detection/terminalshell/terminalshell_windows.c +++ b/src/detection/terminalshell/terminalshell_windows.c @@ -152,7 +152,7 @@ static uint32_t getShellInfo(const FFinstance* instance, FFTerminalShellResult* } ffStrbufClear(&result->shellVersion); - if(instance->config.shellVersion) + if(instance->config.shell.version) fftsGetShellVersion(&result->shellExe, result->shellPrettyName.chars, &result->shellVersion); result->shellPid = pid; diff --git a/src/fastfetch.c b/src/fastfetch.c index 46189fcb2..da2526d74 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -985,7 +985,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con else if(ffParseUptimeCommandOptions(&instance->config.uptime, key, value)) {} else if(optionParseModuleArgs(key, value, "processes", &instance->config.processes)) {} else if(ffParsePackagesCommandOptions(&instance->config.packages, key, value)) {} - else if(optionParseModuleArgs(key, value, "shell", &instance->config.shell)) {} + else if(ffParseShellCommandOptions(&instance->config.shell, key, value)) {} else if(ffParseDisplayCommandOptions(&instance->config.display, key, value)) {} else if(ffParseBrightnessCommandOptions(&instance->config.brightness, key, value)) {} else if(optionParseModuleArgs(key, value, "de", &instance->config.de)) {} @@ -1088,8 +1088,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con //Module options// ////////////////// - else if(strcasecmp(key, "--shell-version") == 0) - instance->config.shellVersion = optionParseBoolean(value); else if(strcasecmp(key, "--terminal-version") == 0) instance->config.terminalVersion = optionParseBoolean(value); else if(strcasecmp(key, "--player-name") == 0) @@ -1192,8 +1190,8 @@ static void parseStructureCommand(FFinstance* instance, const char* line) ffPrintUptime(instance, &instance->config.uptime); else if(strcasecmp(line, FF_PACKAGES_MODULE_NAME) == 0) ffPrintPackages(instance, &instance->config.packages); - else if(strcasecmp(line, "shell") == 0) - ffPrintShell(instance); + else if(strcasecmp(line, FF_SHELL_MODULE_NAME) == 0) + ffPrintShell(instance, &instance->config.shell); else if(strcasecmp(line, FF_DISPLAY_MODULE_NAME) == 0) ffPrintDisplay(instance, &instance->config.display); else if(strcasecmp(line, "desktopenvironment") == 0 || strcasecmp(line, "de") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 94aa5d436..1a3498521 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -68,7 +68,7 @@ typedef struct FFconfig FFUptimeOptions uptime; FFModuleArgs processes; FFPackagesOptions packages; - FFModuleArgs shell; + FFShellOptions shell; FFDisplayOptions display; FFModuleArgs de; FFModuleArgs wallpaper; @@ -132,7 +132,6 @@ typedef struct FFconfig FFstrbuf libwlanapi; FFstrbuf libnm; - bool shellVersion; bool terminalVersion; FFstrbuf publicIpUrl; @@ -201,7 +200,6 @@ void ffPrepareWeather(FFinstance* instance); void ffPrintChassis(FFinstance* instance); void ffPrintProcesses(FFinstance* instance); -void ffPrintShell(FFinstance* instance); void ffPrintDesktopEnvironment(FFinstance* instance); void ffPrintWM(FFinstance* instance); void ffPrintTheme(FFinstance* instance); diff --git a/src/flashfetch.c b/src/flashfetch.c index be0016643..49b48fc71 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -31,7 +31,7 @@ int main(int argc, char** argv) //ffPrintProcesses(&instance); ffPrintUptime(&instance, &instance.config.uptime); ffPrintPackages(&instance, &instance.config.packages); - ffPrintShell(&instance); + ffPrintShell(&instance, &instance.config.shell); ffPrintDisplay(&instance, &instance.config.display); // ffPrintBrightness(&instance); ffPrintDesktopEnvironment(&instance); diff --git a/src/modules/jsonconfig/jsonconfig.c b/src/modules/jsonconfig/jsonconfig.c index fdf5cdc18..3b15bacd3 100644 --- a/src/modules/jsonconfig/jsonconfig.c +++ b/src/modules/jsonconfig/jsonconfig.c @@ -109,6 +109,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o case 'S': { return tryModule(instance, type, module, FF_SEPARATOR_MODULE_NAME, ffParseSeparatorJsonObject) || + tryModule(instance, type, module, FF_SHELL_MODULE_NAME, ffParseShellJsonObject) || tryModule(instance, type, module, FF_SOUND_MODULE_NAME, ffParseSoundJsonObject) || tryModule(instance, type, module, FF_SWAP_MODULE_NAME, ffParseSwapJsonObject) || false; diff --git a/src/modules/modules.h b/src/modules/modules.h index 3357c1f1d..08c4a8598 100644 --- a/src/modules/modules.h +++ b/src/modules/modules.h @@ -28,6 +28,7 @@ #include "modules/os/os.h" #include "modules/packages/packages.h" #include "modules/separator/separator.h" +#include "modules/shell/shell.h" #include "modules/sound/sound.h" #include "modules/swap/swap.h" #include "modules/title/title.h" diff --git a/src/modules/options.h b/src/modules/options.h index fe13e1cc1..522036be8 100644 --- a/src/modules/options.h +++ b/src/modules/options.h @@ -26,6 +26,7 @@ #include "modules/os/option.h" #include "modules/packages/option.h" #include "modules/separator/option.h" +#include "modules/shell/option.h" #include "modules/sound/option.h" #include "modules/swap/option.h" #include "modules/title/option.h" diff --git a/src/modules/shell.c b/src/modules/shell.c deleted file mode 100644 index 607f5ec69..000000000 --- a/src/modules/shell.c +++ /dev/null @@ -1,43 +0,0 @@ -#include "fastfetch.h" -#include "common/printing.h" -#include "detection/terminalshell/terminalshell.h" - -#define FF_SHELL_MODULE_NAME "Shell" -#define FF_SHELL_NUM_FORMAT_ARGS 7 - -void ffPrintShell(FFinstance* instance) -{ - const FFTerminalShellResult* result = ffDetectTerminalShell(instance); - - if(result->shellProcessName.length == 0) - { - ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shell, "Couldn't detect shell"); - return; - } - - if(instance->config.shell.outputFormat.length == 0) - { - ffPrintLogoAndKey(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shell.key); - ffStrbufWriteTo(&result->shellPrettyName, stdout); - - if(result->shellVersion.length > 0) - { - putchar(' '); - ffStrbufWriteTo(&result->shellVersion, stdout); - } - - putchar('\n'); - } - else - { - ffPrintFormat(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shell, FF_SHELL_NUM_FORMAT_ARGS, (FFformatarg[]) { - {FF_FORMAT_ARG_TYPE_STRBUF, &result->shellProcessName}, - {FF_FORMAT_ARG_TYPE_STRBUF, &result->shellExe}, - {FF_FORMAT_ARG_TYPE_STRING, result->shellExeName}, - {FF_FORMAT_ARG_TYPE_STRBUF, &result->shellVersion}, - {FF_FORMAT_ARG_TYPE_STRBUF, &result->userShellExe}, - {FF_FORMAT_ARG_TYPE_STRING, result->userShellExeName}, - {FF_FORMAT_ARG_TYPE_STRBUF, &result->userShellVersion} - }); - } -} diff --git a/src/modules/shell/option.h b/src/modules/shell/option.h new file mode 100644 index 000000000..dd6223a36 --- /dev/null +++ b/src/modules/shell/option.h @@ -0,0 +1,13 @@ +#pragma once + +// This file will be included in "fastfetch.h", do NOT put unnecessary things here + +#include "common/option.h" + +typedef struct FFShellOptions +{ + const char* moduleName; + FFModuleArgs moduleArgs; + + bool version; +} FFShellOptions; diff --git a/src/modules/shell/shell.c b/src/modules/shell/shell.c new file mode 100644 index 000000000..ca5a22cab --- /dev/null +++ b/src/modules/shell/shell.c @@ -0,0 +1,99 @@ +#include "fastfetch.h" +#include "common/printing.h" +#include "detection/terminalshell/terminalshell.h" +#include "modules/shell/shell.h" + +#define FF_SHELL_NUM_FORMAT_ARGS 7 + +void ffPrintShell(FFinstance* instance, FFShellOptions* options) +{ + const FFTerminalShellResult* result = ffDetectTerminalShell(instance); + + if(result->shellProcessName.length == 0) + { + ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, "Couldn't detect shell"); + return; + } + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(instance, FF_SHELL_MODULE_NAME, 0, &options->moduleArgs.key); + ffStrbufWriteTo(&result->shellPrettyName, stdout); + + if(result->shellVersion.length > 0) + { + putchar(' '); + ffStrbufWriteTo(&result->shellVersion, stdout); + } + + putchar('\n'); + } + else + { + ffPrintFormat(instance, FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_SHELL_NUM_FORMAT_ARGS, (FFformatarg[]) { + {FF_FORMAT_ARG_TYPE_STRBUF, &result->shellProcessName}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->shellExe}, + {FF_FORMAT_ARG_TYPE_STRING, result->shellExeName}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->shellVersion}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->userShellExe}, + {FF_FORMAT_ARG_TYPE_STRING, result->userShellExeName}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result->userShellVersion} + }); + } +} + +void ffInitShellOptions(FFShellOptions* options) +{ + options->moduleName = FF_SHELL_MODULE_NAME; + ffOptionInitModuleArg(&options->moduleArgs); + + options->version = true; +} + +bool ffParseShellCommandOptions(FFShellOptions* options, const char* key, const char* value) +{ + const char* subKey = ffOptionTestPrefix(key, FF_SHELL_MODULE_NAME); + if (!subKey) return false; + if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) + return true; + + if (strcasecmp(subKey, "version") == 0) + { + options->version = ffOptionParseBoolean(value); + return true; + } + + return false; +} + +void ffDestroyShellOptions(FFShellOptions* options) +{ + ffOptionDestroyModuleArg(&options->moduleArgs); +} + +#ifdef FF_HAVE_JSONC +void ffParseShellJsonObject(FFinstance* instance, json_object* module) +{ + FFShellOptions __attribute__((__cleanup__(ffDestroyShellOptions))) options; + ffInitShellOptions(&options); + + if (module) + { + json_object_object_foreach(module, key, val) + { + if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs)) + continue; + + if (strcasecmp(key, "version") == 0) + { + options.version = (bool) json_object_get_boolean(val); + continue; + } + + ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key); + } + } + + ffPrintShell(instance, &options); +} +#endif diff --git a/src/modules/shell/shell.h b/src/modules/shell/shell.h new file mode 100644 index 000000000..210d17dbf --- /dev/null +++ b/src/modules/shell/shell.h @@ -0,0 +1,15 @@ +#pragma once + +#include "fastfetch.h" + +#define FF_SHELL_MODULE_NAME "Shell" + +void ffPrintShell(FFinstance* instance, FFShellOptions* options); +void ffInitShellOptions(FFShellOptions* options); +bool ffParseShellCommandOptions(FFShellOptions* options, const char* key, const char* value); +void ffDestroyShellOptions(FFShellOptions* options); + +#ifdef FF_HAVE_JSONC +#include "common/jsonconfig.h" +void ffParseShellJsonObject(FFinstance* instance, json_object* module); +#endif