Bios: support JSON config

This commit is contained in:
李通洲
2023-03-10 14:48:09 +08:00
parent 76da3d811c
commit ae08966d40
12 changed files with 133 additions and 54 deletions
+99
View File
@@ -0,0 +1,99 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/bios/bios.h"
#include "modules/bios/bios.h"
#define FF_BIOS_MODULE_NAME "Bios"
#define FF_BIOS_NUM_FORMAT_ARGS 4
void ffPrintBios(FFinstance* instance, FFBiosOptions* options)
{
FFBiosResult result;
ffDetectBios(&result);
if(result.error.length > 0)
{
ffPrintError(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, "%*s", result.error.length, result.error.chars);
goto exit;
}
if(result.biosRelease.length == 0)
{
ffPrintError(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, "bios_release is not set.");
goto exit;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs.key);
puts(result.biosRelease.chars);
}
else
{
ffPrintFormat(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, FF_BIOS_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &result.biosDate},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.biosRelease},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.biosVendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.biosVersion},
});
}
exit:
ffStrbufDestroy(&result.biosDate);
ffStrbufDestroy(&result.biosRelease);
ffStrbufDestroy(&result.biosVendor);
ffStrbufDestroy(&result.biosVersion);
ffStrbufDestroy(&result.error);
}
void ffInitBiosOptions(FFBiosOptions* options)
{
options->moduleName = FF_BIOS_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseBiosCommandOptions(FFBiosOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_BIOS_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyBiosOptions(FFBiosOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
bool ffParseBiosJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module)
{
if (strcasecmp(type, FF_BIOS_MODULE_NAME) != 0)
return false;
FFBiosOptions __attribute__((__cleanup__(ffDestroyBiosOptions))) options;
ffInitBiosOptions(&options);
if (module)
{
struct lh_entry* entry;
lh_foreach(data->ffjson_object_get_object(module), entry)
{
const char* key = (const char *)lh_entry_k(entry);
if (strcasecmp(key, "type") == 0)
continue;
json_object* val = (struct json_object *)lh_entry_v(entry);
if (ffJsonConfigParseModuleArgs(data, key, val, &options.moduleArgs))
continue;
ffPrintError(instance, FF_BIOS_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintBios(instance, &options);
return true;
}
#endif
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include "fastfetch.h"
void ffPrintBios(FFinstance* instance, FFBiosOptions* options);
void ffInitBiosOptions(FFBiosOptions* options);
bool ffParseBiosCommandOptions(FFBiosOptions* options, const char* key, const char* value);
void ffDestroyBiosOptions(FFBiosOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/config.h"
bool ffParseBiosJsonObject(FFinstance* instance, const char* type, JSONCData* data, 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 FFBiosOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
} FFBiosOptions;