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

122 lines
3.9 KiB
C
Raw Normal View History

2023-03-20 14:57:46 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-03-20 14:57:46 +08:00
#include "detection/cursor/cursor.h"
#include "modules/cursor/cursor.h"
#include "util/stringUtils.h"
2023-03-20 14:57:46 +08:00
void ffPrintCursor(FFCursorOptions* options)
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
if(result.error.length)
ffPrintError(FF_CURSOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", result.error.chars);
2023-03-20 14:57:46 +08:00
else
{
ffStrbufRemoveIgnCaseEndS(&result.theme, "cursors");
ffStrbufRemoveIgnCaseEndS(&result.theme, "cursor");
ffStrbufTrimRight(&result.theme, '_');
ffStrbufTrimRight(&result.theme, '-');
if(result.theme.length == 0)
ffStrbufAppendS(&result.theme, "default");
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);
2023-08-26 05:43:15 -04:00
if(result.size.length > 0 && !ffStrbufEqualS(&result.size, "0"))
2023-03-20 14:57:46 +08:00
printf(" (%spx)", result.size.chars);
putchar('\n');
}
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[]) {
FF_FORMAT_ARG(result.theme, "theme"),
FF_FORMAT_ARG(result.size, "size"),
}));
2023-03-20 14:57:46 +08:00
}
}
ffStrbufDestroy(&result.error);
ffStrbufDestroy(&result.theme);
ffStrbufDestroy(&result.size);
}
2023-08-17 10:56:54 +08:00
void ffParseCursorJsonObject(FFCursorOptions* options, yyjson_val* module)
2023-03-20 14:57:46 +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-20 14:57:46 +08:00
{
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
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
2023-10-24 14:41:29 +08:00
void ffGenerateCursorJsonConfig(FFCursorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyCursorOptions))) FFCursorOptions defaultOptions;
ffInitCursorOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
}
2023-10-23 10:14:22 +08:00
void ffGenerateCursorJsonResult(FF_MAYBE_UNUSED FFCursorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-08-31 10:38:11 +08:00
{
FFCursorResult result;
ffStrbufInit(&result.error);
ffStrbufInit(&result.theme);
ffStrbufInit(&result.size);
ffDetectCursor(&result);
if (result.error.length)
{
yyjson_mut_obj_add_strbuf(doc, module, "error", &result.error);
}
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);
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);
2023-08-31 10:38:11 +08:00
}
2023-10-07 13:40:40 +08:00
2024-12-11 10:47:02 +08:00
static FFModuleBaseInfo ffModuleInfo = {
.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[]) {
{"Cursor theme", "theme"},
{"Cursor size", "size"},
})),
};
2023-10-23 13:32:18 +08:00
void ffInitCursorOptions(FFCursorOptions* options)
{
2024-12-11 10:47:02 +08:00
options->moduleInfo = ffModuleInfo;
2024-07-23 23:02:35 +08:00
ffOptionInitModuleArg(&options->moduleArgs, "󰆿");
2023-10-23 13:32:18 +08:00
}
void ffDestroyCursorOptions(FFCursorOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}