Chassis: add JSON config support

This commit is contained in:
李通洲
2023-06-09 10:08:04 +08:00
committed by 李通洲
parent e3ec136cad
commit 0021219f42
12 changed files with 129 additions and 59 deletions
-50
View File
@@ -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);
}
+92
View File
@@ -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
+15
View File
@@ -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
+11
View File
@@ -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;
+1
View File
@@ -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) ||
+1
View File
@@ -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"
+1
View File
@@ -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"