mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Chassis: add JSON config support
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
+2
-2
@@ -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);
|
||||
|
||||
+3
-3
@@ -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)
|
||||
|
||||
+1
-2
@@ -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
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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;
|
||||
@@ -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) ||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user