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

168 lines
5.2 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/stringUtils.h"
2023-03-10 11:26:53 +08:00
#include "detection/host/host.h"
#include "modules/host/host.h"
bool ffPrintHost(FFHostOptions* options)
2023-03-10 11:26:53 +08:00
{
bool success = false;
2023-06-12 22:53:12 +08:00
FFHostResult host;
2024-01-05 20:38:29 +08:00
ffStrbufInit(&host.family);
ffStrbufInit(&host.name);
ffStrbufInit(&host.version);
ffStrbufInit(&host.sku);
ffStrbufInit(&host.serial);
ffStrbufInit(&host.uuid);
ffStrbufInit(&host.vendor);
2023-03-10 11:26:53 +08:00
const char* error = ffDetectHost(&host);
2023-06-12 22:53:12 +08:00
if(error)
2023-03-10 11:26:53 +08:00
{
ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
2023-06-12 22:53:12 +08:00
goto exit;
2023-03-10 11:26:53 +08:00
}
if(host.name.length == 0 && host.family.length == 0)
2023-03-10 11:26:53 +08:00
{
ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "neither product_family nor product_name is set by O.E.M.");
2023-06-12 22:53:12 +08:00
goto exit;
2023-03-10 11:26:53 +08:00
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-03-10 11:26:53 +08:00
FF_STRBUF_AUTO_DESTROY output = ffStrbufCreate();
2023-03-10 11:26:53 +08:00
2024-01-05 20:38:29 +08:00
if(host.name.length > 0)
ffStrbufAppend(&output, &host.name);
2023-03-10 11:26:53 +08:00
else
2024-01-05 20:38:29 +08:00
ffStrbufAppend(&output, &host.family);
2023-03-10 11:26:53 +08:00
2024-01-05 20:38:29 +08:00
if(host.version.length > 0)
ffStrbufAppendF(&output, " (%s)", host.version.chars);
2023-03-10 11:26:53 +08:00
ffStrbufPutTo(&output, stdout);
}
else
{
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
FF_ARG(host.family, "family"),
FF_ARG(host.name, "name"),
FF_ARG(host.version, "version"),
FF_ARG(host.sku, "sku"),
FF_ARG(host.vendor, "vendor"),
FF_ARG(host.serial, "serial"),
FF_ARG(host.uuid, "uuid"),
}));
2023-03-10 11:26:53 +08:00
}
success = true;
2023-06-12 22:53:12 +08:00
exit:
2024-01-05 20:38:29 +08:00
ffStrbufDestroy(&host.family);
ffStrbufDestroy(&host.name);
ffStrbufDestroy(&host.version);
ffStrbufDestroy(&host.sku);
ffStrbufDestroy(&host.serial);
ffStrbufDestroy(&host.uuid);
ffStrbufDestroy(&host.vendor);
return success;
2023-03-10 11:26:53 +08:00
}
2023-08-17 10:56:54 +08:00
void ffParseHostJsonObject(FFHostOptions* options, yyjson_val* module)
2023-03-10 11:26:53 +08:00
{
yyjson_val *key, *val;
2023-08-17 16:09:41 +08:00
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key, val)
2023-03-10 11:26:53 +08:00
{
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-03-10 11:26:53 +08:00
ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-03-10 11:26:53 +08:00
}
}
2023-09-05 15:57:18 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateHostJsonConfig(FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2023-10-24 14:41:29 +08:00
}
bool ffGenerateHostJsonResult(FF_MAYBE_UNUSED FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-09-05 15:57:18 +08:00
{
bool success = false;
2023-09-05 15:57:18 +08:00
FFHostResult host;
2024-01-05 20:38:29 +08:00
ffStrbufInit(&host.family);
ffStrbufInit(&host.name);
ffStrbufInit(&host.version);
ffStrbufInit(&host.sku);
ffStrbufInit(&host.serial);
ffStrbufInit(&host.uuid);
ffStrbufInit(&host.vendor);
2023-09-05 15:57:18 +08:00
const char* error = ffDetectHost(&host);
2023-09-05 15:57:18 +08:00
if (error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
goto exit;
}
2024-01-05 20:38:29 +08:00
if (host.family.length == 0 && host.name.length == 0)
2023-09-05 15:57:18 +08:00
{
yyjson_mut_obj_add_str(doc, module, "error", "neither product_family nor product_name is set by O.E.M.");
goto exit;
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
2024-01-05 20:38:29 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "family", &host.family);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &host.name);
yyjson_mut_obj_add_strbuf(doc, obj, "version", &host.version);
yyjson_mut_obj_add_strbuf(doc, obj, "sku", &host.sku);
2024-01-06 17:07:36 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &host.vendor);
2024-01-05 20:38:29 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &host.serial);
yyjson_mut_obj_add_strbuf(doc, obj, "uuid", &host.uuid);
success = true;
2023-09-05 15:57:18 +08:00
exit:
2024-01-05 20:38:29 +08:00
ffStrbufDestroy(&host.family);
ffStrbufDestroy(&host.name);
ffStrbufDestroy(&host.version);
ffStrbufDestroy(&host.sku);
ffStrbufDestroy(&host.serial);
ffStrbufDestroy(&host.uuid);
ffStrbufDestroy(&host.vendor);
return success;
2023-09-05 15:57:18 +08:00
}
2023-10-07 13:40:40 +08:00
void ffInitHostOptions(FFHostOptions* options)
{
ffOptionInitModuleArg(&options->moduleArgs, "󰌢");
}
void ffDestroyHostOptions(FFHostOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffHostModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_HOST_MODULE_NAME,
.description = "Print product name of your computer",
.initOptions = (void*) ffInitHostOptions,
.destroyOptions = (void*) ffDestroyHostOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseHostJsonObject,
.printModule = (void*) ffPrintHost,
.generateJsonResult = (void*) ffGenerateHostJsonResult,
.generateJsonConfig = (void*) ffGenerateHostJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Product family", "family"},
{"Product name", "name"},
{"Product version", "version"},
{"Product sku", "sku"},
{"Product vendor", "vendor"},
{"Product serial number", "serial"},
{"Product uuid", "uuid"},
}))
};