diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cf008a8b..9f4e26ad0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -268,7 +268,7 @@ set(LIBFASTFETCH_SRC src/modules/board/board.c src/modules/brightness/brightness.c src/modules/break/break.c - src/modules/chassis.c + src/modules/chassis/chassis.c src/modules/colors/colors.c src/modules/cpu/cpu.c src/modules/cpuusage/cpuusage.c diff --git a/src/common/init.c b/src/common/init.c index 7497b979d..e800a685d 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -62,7 +62,7 @@ static void defaultConfig(FFinstance* instance) ffInitBiosOptions(&instance->config.bios); ffInitBoardOptions(&instance->config.board); ffInitBrightnessOptions(&instance->config.brightness); - initModuleArg(&instance->config.chassis); + ffInitChassisOptions(&instance->config.chassis); ffInitCommandOptions(&instance->config.command); ffInitCustomOptions(&instance->config.custom); ffInitKernelOptions(&instance->config.kernel); @@ -273,7 +273,7 @@ static void destroyConfig(FFinstance* instance) ffDestroyBiosOptions(&instance->config.bios); ffDestroyBoardOptions(&instance->config.board); ffDestroyBrightnessOptions(&instance->config.brightness); - destroyModuleArg(&instance->config.chassis); + ffDestroyChassisOptions(&instance->config.chassis); ffDestroyCommandOptions(&instance->config.command); ffDestroyCustomOptions(&instance->config.custom); ffDestroyKernelOptions(&instance->config.kernel); diff --git a/src/fastfetch.c b/src/fastfetch.c index a59ff5468..345852989 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -978,7 +978,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con else if(ffParseOSCommandOptions(&instance->config.os, key, value)) {} else if(ffParseBiosCommandOptions(&instance->config.bios, key, value)) {} else if(ffParseBoardCommandOptions(&instance->config.board, key, value)) {} - else if(optionParseModuleArgs(key, value, "chassis", &instance->config.chassis)) {} + else if(ffParseChassisCommandOptions(&instance->config.chassis, key, value)) {} else if(ffParseCommandCommandOptions(&instance->config.command, key, value)) {} else if(ffParseCustomCommandOptions(&instance->config.custom, key, value)) {} else if(ffParseKernelCommandOptions(&instance->config.kernel, key, value)) {} @@ -1158,8 +1158,8 @@ static void parseStructureCommand(FFinstance* instance, const char* line) ffPrintBoard(instance, &instance->config.board); else if(strcasecmp(line, FF_BRIGHTNESS_MODULE_NAME) == 0) ffPrintBrightness(instance, &instance->config.brightness); - else if(strcasecmp(line, "chassis") == 0) - ffPrintChassis(instance); + else if(strcasecmp(line, FF_CHASSIS_MODULE_NAME) == 0) + ffPrintChassis(instance, &instance->config.chassis); else if(strcasecmp(line, FF_KERNEL_MODULE_NAME) == 0) ffPrintKernel(instance, &instance->config.kernel); else if(strcasecmp(line, "processes") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 557e2121a..2a6b5b350 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -53,7 +53,7 @@ typedef struct FFconfig FFBiosOptions bios; FFBoardOptions board; FFBrightnessOptions brightness; - FFModuleArgs chassis; + FFChassisOptions chassis; FFCommandOptions command; FFKernelOptions kernel; FFUptimeOptions uptime; @@ -174,7 +174,6 @@ void ffLogoBuiltinListAutocompletion(); //Printing -void ffPrintChassis(FFinstance* instance); void ffPrintProcesses(FFinstance* instance); #endif diff --git a/src/flashfetch.c b/src/flashfetch.c index 0aad4532c..e6358c03f 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -26,7 +26,7 @@ int main(int argc, char** argv) ffPrintHost(&instance, &instance.config.host); //ffPrintBios(&instance, &instance.config.bios); //ffPrintBoard(&instance, &instance.config.board); - //ffPrintChassis(&instance); + //ffPrintChassis(&instance, &instance.config.chassis); ffPrintKernel(&instance, &instance.config.kernel); //ffPrintProcesses(&instance); ffPrintUptime(&instance, &instance.config.uptime); diff --git a/src/modules/chassis.c b/src/modules/chassis.c deleted file mode 100644 index 4932ecc69..000000000 --- a/src/modules/chassis.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "fastfetch.h" -#include "common/printing.h" -#include "detection/chassis/chassis.h" - -#define FF_CHASSIS_MODULE_NAME "Chassis" -#define FF_CHASSIS_NUM_FORMAT_ARGS 3 - -void ffPrintChassis(FFinstance* instance) -{ - FFChassisResult result; - ffDetectChassis(&result); - - if(result.error.length > 0) - { - ffPrintError(instance, FF_CHASSIS_MODULE_NAME, 0, &instance->config.chassis, "%*s", result.error.length, result.error.chars); - goto exit; - } - - if(result.chassisType.length == 0) - { - ffPrintError(instance, FF_CHASSIS_MODULE_NAME, 0, &instance->config.chassis, "chassis_type is not set by O.E.M."); - return; - } - - if(instance->config.chassis.outputFormat.length == 0) - { - ffPrintLogoAndKey(instance, FF_CHASSIS_MODULE_NAME, 0, &instance->config.chassis.key); - - FF_STRBUF_AUTO_DESTROY output = ffStrbufCreateCopy(&result.chassisType); - - if(result.chassisVersion.length > 0) - ffStrbufAppendF(&output, " (%s)", result.chassisVersion.chars); - - ffStrbufPutTo(&output, stdout); - } - else - { - ffPrintFormat(instance, FF_CHASSIS_MODULE_NAME, 0, &instance->config.chassis, FF_CHASSIS_NUM_FORMAT_ARGS, (FFformatarg[]) { - {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisType}, - {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisVendor}, - {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisVersion}, - }); - } - -exit: - ffStrbufDestroy(&result.chassisType); - ffStrbufDestroy(&result.chassisVendor); - ffStrbufDestroy(&result.chassisVersion); - ffStrbufDestroy(&result.error); -} diff --git a/src/modules/chassis/chassis.c b/src/modules/chassis/chassis.c new file mode 100644 index 000000000..72beac3c0 --- /dev/null +++ b/src/modules/chassis/chassis.c @@ -0,0 +1,92 @@ +#include "fastfetch.h" +#include "common/printing.h" +#include "detection/chassis/chassis.h" +#include "modules/chassis/chassis.h" + +#define FF_CHASSIS_NUM_FORMAT_ARGS 3 + +void ffPrintChassis(FFinstance* instance, FFChassisOptions* options) +{ + FFChassisResult result; + ffDetectChassis(&result); + + if(result.error.length > 0) + { + ffPrintError(instance, FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, "%*s", result.error.length, result.error.chars); + goto exit; + } + + if(result.chassisType.length == 0) + { + ffPrintError(instance, FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, "chassis_type is not set by O.E.M."); + return; + } + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(instance, FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs.key); + + FF_STRBUF_AUTO_DESTROY output = ffStrbufCreateCopy(&result.chassisType); + + if(result.chassisVersion.length > 0) + ffStrbufAppendF(&output, " (%s)", result.chassisVersion.chars); + + ffStrbufPutTo(&output, stdout); + } + else + { + ffPrintFormat(instance, FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_CHASSIS_NUM_FORMAT_ARGS, (FFformatarg[]) { + {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisType}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisVendor}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.chassisVersion}, + }); + } + +exit: + ffStrbufDestroy(&result.chassisType); + ffStrbufDestroy(&result.chassisVendor); + ffStrbufDestroy(&result.chassisVersion); + ffStrbufDestroy(&result.error); +} + +void ffInitChassisOptions(FFChassisOptions* options) +{ + options->moduleName = FF_CHASSIS_MODULE_NAME; + ffOptionInitModuleArg(&options->moduleArgs); +} + +bool ffParseChassisCommandOptions(FFChassisOptions* options, const char* key, const char* value) +{ + const char* subKey = ffOptionTestPrefix(key, FF_CHASSIS_MODULE_NAME); + if (!subKey) return false; + if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) + return true; + + return false; +} + +void ffDestroyChassisOptions(FFChassisOptions* options) +{ + ffOptionDestroyModuleArg(&options->moduleArgs); +} + +#ifdef FF_HAVE_JSONC +void ffParseChassisJsonObject(FFinstance* instance, json_object* module) +{ + FFChassisOptions __attribute__((__cleanup__(ffDestroyChassisOptions))) options; + ffInitChassisOptions(&options); + + if (module) + { + json_object_object_foreach(module, key, val) + { + if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs)) + continue; + + ffPrintError(instance, FF_CHASSIS_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key); + } + } + + ffPrintChassis(instance, &options); +} +#endif diff --git a/src/modules/chassis/chassis.h b/src/modules/chassis/chassis.h new file mode 100644 index 000000000..2eb9af900 --- /dev/null +++ b/src/modules/chassis/chassis.h @@ -0,0 +1,15 @@ +#pragma once + +#include "fastfetch.h" + +#define FF_CHASSIS_MODULE_NAME "Chassis" + +void ffPrintChassis(FFinstance* instance, FFChassisOptions* options); +void ffInitChassisOptions(FFChassisOptions* options); +bool ffParseChassisCommandOptions(FFChassisOptions* options, const char* key, const char* value); +void ffDestroyChassisOptions(FFChassisOptions* options); + +#ifdef FF_HAVE_JSONC +#include "common/jsonconfig.h" +void ffParseChassisJsonObject(FFinstance* instance, json_object* module); +#endif diff --git a/src/modules/chassis/option.h b/src/modules/chassis/option.h new file mode 100644 index 000000000..939fd165d --- /dev/null +++ b/src/modules/chassis/option.h @@ -0,0 +1,11 @@ +#pragma once + +// This file will be included in "fastfetch.h", do NOT put unnecessary things here + +#include "common/option.h" + +typedef struct FFChassisOptions +{ + const char* moduleName; + FFModuleArgs moduleArgs; +} FFChassisOptions; diff --git a/src/modules/jsonconfig/jsonconfig.c b/src/modules/jsonconfig/jsonconfig.c index aca52158d..fa17afb29 100644 --- a/src/modules/jsonconfig/jsonconfig.c +++ b/src/modules/jsonconfig/jsonconfig.c @@ -39,6 +39,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o case 'C': { return + tryModule(instance, type, module, FF_CHASSIS_MODULE_NAME, ffParseChassisJsonObject) || tryModule(instance, type, module, FF_CPU_MODULE_NAME, ffParseCPUJsonObject) || tryModule(instance, type, module, FF_CPUUSAGE_MODULE_NAME, ffParseCPUUsageJsonObject) || tryModule(instance, type, module, FF_COMMAND_MODULE_NAME, ffParseCommandJsonObject) || diff --git a/src/modules/modules.h b/src/modules/modules.h index 3c9b98c3c..fac24537f 100644 --- a/src/modules/modules.h +++ b/src/modules/modules.h @@ -8,6 +8,7 @@ #include "modules/brightness/brightness.h" #include "modules/board/board.h" #include "modules/break/break.h" +#include "modules/chassis/chassis.h" #include "modules/cpu/cpu.h" #include "modules/cpuusage/cpuusage.h" #include "modules/command/command.h" diff --git a/src/modules/options.h b/src/modules/options.h index 392a6f1fa..62b53f4da 100644 --- a/src/modules/options.h +++ b/src/modules/options.h @@ -7,6 +7,7 @@ #include "modules/bluetooth/option.h" #include "modules/board/option.h" #include "modules/brightness/option.h" +#include "modules/chassis/option.h" #include "modules/cpu/option.h" #include "modules/cpuusage/option.h" #include "modules/cursor/option.h"