mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 16:26:39 +01:00
Board: support JSON config
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/board/board.h"
|
||||
|
||||
#define FF_BOARD_MODULE_NAME "Board"
|
||||
#define FF_BOARD_NUM_FORMAT_ARGS 3
|
||||
|
||||
void ffPrintBoard(FFinstance* instance)
|
||||
{
|
||||
FFBoardResult result;
|
||||
ffDetectBoard(&result);
|
||||
|
||||
if(result.error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_BOARD_MODULE_NAME, 0, &instance->config.board, "%*s", result.error.length, result.error.chars);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if(result.boardName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_BOARD_MODULE_NAME, 0, &instance->config.board, "board_name is not set.");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if(instance->config.board.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_BOARD_MODULE_NAME, 0, &instance->config.board.key);
|
||||
puts(result.boardName.chars);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_BOARD_MODULE_NAME, 0, &instance->config.board, FF_BOARD_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result.boardName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result.boardVendor},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result.boardVersion},
|
||||
});
|
||||
}
|
||||
|
||||
exit:
|
||||
ffStrbufDestroy(&result.boardName);
|
||||
ffStrbufDestroy(&result.boardVendor);
|
||||
ffStrbufDestroy(&result.boardVersion);
|
||||
ffStrbufDestroy(&result.error);
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/board/board.h"
|
||||
#include "modules/board/board.h"
|
||||
|
||||
#define FF_BOARD_MODULE_NAME "Board"
|
||||
#define FF_BOARD_NUM_FORMAT_ARGS 3
|
||||
|
||||
void ffPrintBoard(FFinstance* instance, FFBoardOptions* options)
|
||||
{
|
||||
FFBoardResult result;
|
||||
ffDetectBoard(&result);
|
||||
|
||||
if(result.error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, "%*s", result.error.length, result.error.chars);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if(result.boardName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, "board_name is not set.");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_BOARD_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
puts(result.boardName.chars);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, FF_BOARD_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result.boardName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result.boardVendor},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result.boardVersion},
|
||||
});
|
||||
}
|
||||
|
||||
exit:
|
||||
ffStrbufDestroy(&result.boardName);
|
||||
ffStrbufDestroy(&result.boardVendor);
|
||||
ffStrbufDestroy(&result.boardVersion);
|
||||
ffStrbufDestroy(&result.error);
|
||||
}
|
||||
|
||||
void ffInitBoardOptions(FFBoardOptions* options)
|
||||
{
|
||||
options->moduleName = FF_BOARD_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseBoardCommandOptions(FFBoardOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_BOARD_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyBoardOptions(FFBoardOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
bool ffParseBoardJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module)
|
||||
{
|
||||
if (strcasecmp(type, FF_BOARD_MODULE_NAME) != 0)
|
||||
return false;
|
||||
|
||||
FFBoardOptions __attribute__((__cleanup__(ffDestroyBoardOptions))) options;
|
||||
ffInitBoardOptions(&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_BOARD_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintBoard(instance, &options);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
void ffPrintBoard(FFinstance* instance, FFBoardOptions* options);
|
||||
void ffInitBoardOptions(FFBoardOptions* options);
|
||||
bool ffParseBoardCommandOptions(FFBoardOptions* options, const char* key, const char* value);
|
||||
void ffDestroyBoardOptions(FFBoardOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/config.h"
|
||||
bool ffParseBoardJsonObject(FFinstance* instance, const char* type, JSONCData* data, 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 FFBoardOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFBoardOptions;
|
||||
@@ -16,6 +16,7 @@ static inline bool parseModuleJsonObject(FFinstance* instance, const char* type,
|
||||
ffParseBatteryJsonObject(instance, type, data, module) ||
|
||||
ffParseBiosJsonObject(instance, type, data, module) ||
|
||||
ffParseBluetoothJsonObject(instance, type, data, module) ||
|
||||
ffParseBoardJsonObject(instance, type, data, module) ||
|
||||
ffParseCommandJsonObject(instance, type, data, module) ||
|
||||
ffParseDateTimeJsonObject(instance, type, data, module) ||
|
||||
ffParseDisplayJsonObject(instance, type, data, module) ||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "modules/battery/battery.h"
|
||||
#include "modules/bios/bios.h"
|
||||
#include "modules/bluetooth/bluetooth.h"
|
||||
#include "modules/board/board.h"
|
||||
#include "modules/command/command.h"
|
||||
#include "modules/datetime/datetime.h"
|
||||
#include "modules/display/display.h"
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "modules/battery/option.h"
|
||||
#include "modules/bios/option.h"
|
||||
#include "modules/bluetooth/option.h"
|
||||
#include "modules/board/option.h"
|
||||
#include "modules/command/option.h"
|
||||
#include "modules/datetime/option.h"
|
||||
#include "modules/display/option.h"
|
||||
|
||||
Reference in New Issue
Block a user