mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
Host: support JSON config
This commit is contained in:
parent
04180a7389
commit
9c066e9494
@ -278,7 +278,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/disk.c
|
||||
src/modules/font.c
|
||||
src/modules/gpu.c
|
||||
src/modules/host.c
|
||||
src/modules/host/host.c
|
||||
src/modules/icons.c
|
||||
src/modules/gamepad.c
|
||||
src/modules/kernel.c
|
||||
|
@ -74,7 +74,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
|
||||
ffInitTitleOptions(&instance->config.title);
|
||||
ffInitOSOptions(&instance->config.os);
|
||||
initModuleArg(&instance->config.host);
|
||||
ffInitHostOptions(&instance->config.host);
|
||||
initModuleArg(&instance->config.bios);
|
||||
initModuleArg(&instance->config.board);
|
||||
initModuleArg(&instance->config.brightness);
|
||||
@ -314,7 +314,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
|
||||
ffDestroyTitleOptions(&instance->config.title);
|
||||
ffDestroyOSOptions(&instance->config.os);
|
||||
destroyModuleArg(&instance->config.host);
|
||||
ffDestroyHostOptions(&instance->config.host);
|
||||
destroyModuleArg(&instance->config.bios);
|
||||
destroyModuleArg(&instance->config.board);
|
||||
destroyModuleArg(&instance->config.chassis);
|
||||
|
@ -1134,7 +1134,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
///////////////////////
|
||||
|
||||
else if(ffParseOSCommandOptions(&instance->config.os, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "host", &instance->config.host)) {}
|
||||
else if(ffParseHostCommandOptions(&instance->config.host, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "bios", &instance->config.bios)) {}
|
||||
else if(optionParseModuleArgs(key, value, "board", &instance->config.board)) {}
|
||||
else if(optionParseModuleArgs(key, value, "chassis", &instance->config.chassis)) {}
|
||||
@ -1385,7 +1385,7 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
else if(strcasecmp(line, "os") == 0)
|
||||
ffPrintOS(instance, &instance->config.os);
|
||||
else if(strcasecmp(line, "host") == 0)
|
||||
ffPrintHost(instance);
|
||||
ffPrintHost(instance, &instance->config.host);
|
||||
else if(strcasecmp(line, "bios") == 0)
|
||||
ffPrintBios(instance);
|
||||
else if(strcasecmp(line, "board") == 0)
|
||||
|
@ -128,7 +128,7 @@ typedef struct FFconfig
|
||||
|
||||
FFTitleOptions title;
|
||||
FFOSOptions os;
|
||||
FFModuleArgs host;
|
||||
FFHostOptions host;
|
||||
FFModuleArgs bios;
|
||||
FFModuleArgs board;
|
||||
FFModuleArgs brightness;
|
||||
@ -286,7 +286,6 @@ void ffPrepareWeather(FFinstance* instance);
|
||||
|
||||
void ffPrintCustom(FFinstance* instance, const char* key, const char* value);
|
||||
void ffPrintBreak(FFinstance* instance);
|
||||
void ffPrintHost(FFinstance* instance);
|
||||
void ffPrintBios(FFinstance* instance);
|
||||
void ffPrintBoard(FFinstance* instance);
|
||||
void ffPrintChassis(FFinstance* instance);
|
||||
|
@ -23,7 +23,7 @@ int main(int argc, char** argv)
|
||||
ffPrintTitle(&instance, &instance.config.title);
|
||||
ffPrintSeparator(&instance, &instance.config.separator);
|
||||
ffPrintOS(&instance, &instance.config.os);
|
||||
ffPrintHost(&instance);
|
||||
ffPrintHost(&instance, &instance.config.host);
|
||||
//ffPrintBios(&instance);
|
||||
//ffPrintBoard(&instance);
|
||||
//ffPrintChassis(&instance);
|
||||
|
@ -1,55 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/host/host.h"
|
||||
|
||||
#define FF_HOST_MODULE_NAME "Host"
|
||||
#define FF_HOST_NUM_FORMAT_ARGS 5
|
||||
|
||||
void ffPrintHost(FFinstance* instance)
|
||||
{
|
||||
const FFHostResult* host = ffDetectHost();
|
||||
|
||||
if(host->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_HOST_MODULE_NAME, 0, &instance->config.host, "%*s", host->error.length, host->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
if(host->productFamily.length == 0 && host->productName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_HOST_MODULE_NAME, 0, &instance->config.host, "neither product_family nor product_name is set by O.E.M.");
|
||||
return;
|
||||
}
|
||||
|
||||
if(instance->config.host.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_HOST_MODULE_NAME, 0, &instance->config.host.key);
|
||||
|
||||
FFstrbuf output;
|
||||
ffStrbufInit(&output);
|
||||
|
||||
if(host->productName.length > 0)
|
||||
ffStrbufAppend(&output, &host->productName);
|
||||
else
|
||||
ffStrbufAppend(&output, &host->productFamily);
|
||||
|
||||
if(host->productVersion.length > 0 && !ffStrbufIgnCaseEqualS(&host->productVersion, "none"))
|
||||
{
|
||||
ffStrbufAppendF(&output, " (%s)", host->productVersion.chars);
|
||||
}
|
||||
|
||||
ffStrbufPutTo(&output, stdout);
|
||||
|
||||
ffStrbufDestroy(&output);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_HOST_MODULE_NAME, 0, &instance->config.host, FF_HOST_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productFamily},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productVersion},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productSku},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->sysVendor}
|
||||
});
|
||||
}
|
||||
}
|
108
src/modules/host/host.c
Normal file
108
src/modules/host/host.c
Normal file
@ -0,0 +1,108 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/host/host.h"
|
||||
#include "modules/host/host.h"
|
||||
|
||||
#define FF_HOST_MODULE_NAME "Host"
|
||||
#define FF_HOST_NUM_FORMAT_ARGS 5
|
||||
|
||||
void ffPrintHost(FFinstance* instance, FFHostOptions* options)
|
||||
{
|
||||
const FFHostResult* host = ffDetectHost();
|
||||
|
||||
if(host->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs, "%*s", host->error.length, host->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
if(host->productFamily.length == 0 && host->productName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs, "neither product_family nor product_name is set by O.E.M.");
|
||||
return;
|
||||
}
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
|
||||
FFstrbuf output;
|
||||
ffStrbufInit(&output);
|
||||
|
||||
if(host->productName.length > 0)
|
||||
ffStrbufAppend(&output, &host->productName);
|
||||
else
|
||||
ffStrbufAppend(&output, &host->productFamily);
|
||||
|
||||
if(host->productVersion.length > 0 && !ffStrbufIgnCaseEqualS(&host->productVersion, "none"))
|
||||
{
|
||||
ffStrbufAppendF(&output, " (%s)", host->productVersion.chars);
|
||||
}
|
||||
|
||||
ffStrbufPutTo(&output, stdout);
|
||||
|
||||
ffStrbufDestroy(&output);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_HOST_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productFamily},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productVersion},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->productSku},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &host->sysVendor}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitHostOptions(FFHostOptions* options)
|
||||
{
|
||||
options->moduleName = FF_HOST_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseHostCommandOptions(FFHostOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_HOST_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyHostOptions(FFHostOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
bool ffParseHostJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module)
|
||||
{
|
||||
if (strcasecmp(type, FF_HOST_MODULE_NAME) != 0)
|
||||
return false;
|
||||
|
||||
FFHostOptions __attribute__((__cleanup__(ffDestroyHostOptions))) options;
|
||||
ffInitHostOptions(&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_HOST_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintHost(instance, &options);
|
||||
return true;
|
||||
}
|
||||
#endif
|
14
src/modules/host/host.h
Normal file
14
src/modules/host/host.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
#include "modules/host/option.h"
|
||||
|
||||
void ffPrintHost(FFinstance* instance, FFHostOptions* options);
|
||||
void ffInitHostOptions(FFHostOptions* options);
|
||||
bool ffParseHostCommandOptions(FFHostOptions* options, const char* key, const char* value);
|
||||
void ffDestroyHostOptions(FFHostOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/config.h"
|
||||
bool ffParseHostJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module);
|
||||
#endif
|
11
src/modules/host/option.h
Normal file
11
src/modules/host/option.h
Normal 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 FFHostOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFHostOptions;
|
@ -17,6 +17,7 @@ static inline bool parseModuleJsonObject(FFinstance* instance, const char* type,
|
||||
ffParseCommandJsonObject(instance, type, data, module) ||
|
||||
ffParseDateTimeJsonObject(instance, type, data, module) ||
|
||||
ffParseDisplayJsonObject(instance, type, data, module) ||
|
||||
ffParseHostJsonObject(instance, type, data, module) ||
|
||||
ffParseOSJsonObject(instance, type, data, module) ||
|
||||
ffParseSeparatorJsonObject(instance, type, data, module) ||
|
||||
false;
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "modules/command/command.h"
|
||||
#include "modules/datetime/datetime.h"
|
||||
#include "modules/display/display.h"
|
||||
#include "modules/host/host.h"
|
||||
#include "modules/separator/separator.h"
|
||||
#include "modules/title/title.h"
|
||||
#include "modules/jsonconfig/jsonconfig.h"
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "modules/command/option.h"
|
||||
#include "modules/datetime/option.h"
|
||||
#include "modules/display/option.h"
|
||||
#include "modules/host/option.h"
|
||||
#include "modules/os/option.h"
|
||||
#include "modules/separator/option.h"
|
||||
#include "modules/title/option.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user