Files
fastfetch/src/modules/board/board.c
T

97 lines
2.7 KiB
C
Raw Normal View History

2023-03-13 18:54:57 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-03-13 18:54:57 +08:00
#include "detection/board/board.h"
#include "modules/board/board.h"
#include "util/stringUtils.h"
2023-03-13 18:54:57 +08:00
#define FF_BOARD_NUM_FORMAT_ARGS 3
void ffPrintBoard(FFBoardOptions* options)
2023-03-13 18:54:57 +08:00
{
FFBoardResult result;
2023-06-22 22:32:14 +08:00
ffStrbufInit(&result.name);
ffStrbufInit(&result.vendor);
ffStrbufInit(&result.version);
2023-06-19 21:27:47 +08:00
const char* error = ffDetectBoard(&result);
2023-03-13 18:54:57 +08:00
2023-06-19 21:27:47 +08:00
if(error)
2023-03-13 18:54:57 +08:00
{
ffPrintError(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
2023-03-13 18:54:57 +08:00
goto exit;
}
2023-06-22 22:32:14 +08:00
if(result.name.length == 0)
2023-03-13 18:54:57 +08:00
{
ffPrintError(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, "board_name is not set.");
2023-03-13 18:54:57 +08:00
goto exit;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-06-22 22:32:14 +08:00
ffStrbufWriteTo(&result.name, stdout);
if (result.version.length)
printf(" (%s)", result.version.chars);
2023-06-19 21:17:07 +08:00
putchar('\n');
2023-03-13 18:54:57 +08:00
}
else
{
ffPrintFormat(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, FF_BOARD_NUM_FORMAT_ARGS, (FFformatarg[]) {
2023-06-22 22:32:14 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &result.name},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.vendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.version},
2023-03-13 18:54:57 +08:00
});
}
exit:
2023-06-22 22:32:14 +08:00
ffStrbufDestroy(&result.name);
ffStrbufDestroy(&result.vendor);
ffStrbufDestroy(&result.version);
2023-03-13 18:54:57 +08:00
}
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);
}
void ffParseBoardJsonObject(yyjson_val* module)
2023-03-13 18:54:57 +08:00
{
FFBoardOptions __attribute__((__cleanup__(ffDestroyBoardOptions))) options;
ffInitBoardOptions(&options);
if (module)
2023-03-13 18:54:57 +08:00
{
2023-06-10 15:01:54 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
{
2023-06-10 15:01:54 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
2023-06-10 15:01:54 +08:00
continue;
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
2023-03-15 11:24:49 +08:00
ffPrintError(FF_BOARD_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
2023-03-13 18:54:57 +08:00
}
ffPrintBoard(&options);
2023-03-13 18:54:57 +08:00
}