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

157 lines
5.9 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"
2026-03-29 09:28:49 +08:00
bool ffPrintHost(FFHostOptions* options) {
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);
2026-03-29 09:28:49 +08:00
if (error) {
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
}
2026-03-29 09:28:49 +08:00
if (host.name.length == 0 && host.family.length == 0) {
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
}
2026-03-29 09:28:49 +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
2026-03-29 09:28:49 +08:00
if (host.name.length > 0) {
2024-01-05 20:38:29 +08:00
ffStrbufAppend(&output, &host.name);
2026-03-29 09:28:49 +08:00
} else {
2024-01-05 20:38:29 +08:00
ffStrbufAppend(&output, &host.family);
2026-03-29 09:28:49 +08:00
}
2023-03-10 11:26:53 +08:00
2026-03-29 09:28:49 +08:00
if (host.version.length > 0) {
2024-01-05 20:38:29 +08:00
ffStrbufAppendF(&output, " (%s)", host.version.chars);
2026-03-29 09:28:49 +08:00
}
2023-03-10 11:26:53 +08:00
ffStrbufPutTo(&output, stdout);
2026-03-29 09:28:49 +08:00
} 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[]) {
2026-03-29 09:28:49 +08:00
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
}
2026-03-29 09:28:49 +08:00
void ffParseHostJsonObject(FFHostOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2023-08-17 16:09:41 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2023-08-17 16:09:41 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
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
2026-03-29 09:28:49 +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
}
2026-04-09 16:20:59 +08:00
bool ffGenerateHostJsonResult(FF_A_UNUSED FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
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);
2026-03-29 09:28:49 +08:00
if (error) {
2023-09-05 15:57:18 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
goto exit;
}
2026-03-29 09:28:49 +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
2026-03-29 09:28:49 +08:00
void ffInitHostOptions(FFHostOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󰌢");
}
2026-03-29 09:28:49 +08:00
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[]) {
2026-03-30 10:27:23 +08:00
{ "Product family", "family" },
{ "Product name", "name" },
{ "Product version", "version" },
{ "Product sku", "sku" },
{ "Product vendor", "vendor" },
{ "Product serial number", "serial" },
{ "Product uuid", "uuid" },
}))
};