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

116 lines
4.1 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
2026-05-28 15:58:08 +08:00
#include "common/strutil.h"
2023-03-20 14:57:46 +08:00
#include "detection/cursor/cursor.h"
#include "modules/cursor/cursor.h"
2026-03-29 09:28:49 +08:00
bool ffPrintCursor(FFCursorOptions* options) {
bool success = false;
2023-03-20 14:57:46 +08:00
FFCursorResult result;
ffStrbufInit(&result.error);
ffStrbufInit(&result.theme);
ffStrbufInit(&result.size);
ffDetectCursor(&result);
2023-03-20 14:57:46 +08:00
2026-03-29 09:28:49 +08:00
if (result.error.length) {
ffPrintError(FF_CURSOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", result.error.chars);
2026-03-29 09:28:49 +08:00
} else {
2023-03-20 14:57:46 +08:00
ffStrbufRemoveIgnCaseEndS(&result.theme, "cursors");
ffStrbufRemoveIgnCaseEndS(&result.theme, "cursor");
ffStrbufTrimRight(&result.theme, '_');
ffStrbufTrimRight(&result.theme, '-');
2026-03-29 09:28:49 +08:00
if (result.theme.length == 0) {
2023-03-20 14:57:46 +08:00
ffStrbufAppendS(&result.theme, "default");
2026-03-29 09:28:49 +08:00
}
2023-03-20 14:57:46 +08:00
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
ffPrintLogoAndKey(FF_CURSOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-03-20 14:57:46 +08:00
ffStrbufWriteTo(&result.theme, stdout);
2026-03-29 09:28:49 +08:00
if (result.size.length > 0 && !ffStrbufEqualS(&result.size, "0")) {
2023-03-20 14:57:46 +08:00
printf(" (%spx)", result.size.chars);
2026-03-29 09:28:49 +08:00
}
2023-03-20 14:57:46 +08:00
putchar('\n');
2026-03-29 09:28:49 +08:00
} else {
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(FF_CURSOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(result.theme, "theme"),
FF_ARG(result.size, "size"),
}));
2023-03-20 14:57:46 +08:00
}
success = true;
2023-03-20 14:57:46 +08:00
}
ffStrbufDestroy(&result.error);
ffStrbufDestroy(&result.theme);
ffStrbufDestroy(&result.size);
return success;
2023-03-20 14:57:46 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseCursorJsonObject(FFCursorOptions* 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-20 14:57:46 +08:00
ffPrintError(FF_CURSOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-03-20 14:57:46 +08:00
}
}
2023-08-31 10:38:11 +08:00
2026-03-29 09:28:49 +08:00
void ffGenerateCursorJsonConfig(FFCursorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2023-10-24 14:41:29 +08:00
}
bool ffGenerateCursorJsonResult([[maybe_unused]] FFCursorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
bool success = false;
2023-08-31 10:38:11 +08:00
FFCursorResult result;
ffStrbufInit(&result.error);
ffStrbufInit(&result.theme);
ffStrbufInit(&result.size);
ffDetectCursor(&result);
2026-03-29 09:28:49 +08:00
if (result.error.length) {
yyjson_mut_obj_add_strbuf(doc, module, "error", &result.error);
2026-03-29 09:28:49 +08:00
} else {
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "theme", &result.theme);
yyjson_mut_obj_add_strbuf(doc, obj, "size", &result.size);
success = true;
2023-08-31 10:38:11 +08:00
}
2023-09-08 20:03:26 +08:00
ffStrbufDestroy(&result.error);
ffStrbufDestroy(&result.theme);
ffStrbufDestroy(&result.size);
return success;
2023-08-31 10:38:11 +08:00
}
2023-10-07 13:40:40 +08:00
2026-03-29 09:28:49 +08:00
void ffInitCursorOptions(FFCursorOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󰆿");
}
2026-03-29 09:28:49 +08:00
void ffDestroyCursorOptions(FFCursorOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffCursorModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_CURSOR_MODULE_NAME,
.description = "Print cursor style name",
.initOptions = (void*) ffInitCursorOptions,
.destroyOptions = (void*) ffDestroyCursorOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseCursorJsonObject,
.printModule = (void*) ffPrintCursor,
.generateJsonResult = (void*) ffGenerateCursorJsonResult,
.generateJsonConfig = (void*) ffGenerateCursorJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Cursor theme", "theme" },
{ "Cursor size", "size" },
2024-12-11 10:47:02 +08:00
})),
};