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

135 lines
4.1 KiB
C
Raw Normal View History

2026-01-05 16:19:47 +08:00
#include "util/printing.h"
#include "util/jsonconfig.h"
#include "util/stringUtils.h"
2024-05-22 16:13:36 +08:00
#include "detection/libc/libc.h"
#include "detection/editor/editor.h"
#include "modules/editor/editor.h"
bool ffPrintEditor(FFEditorOptions* options)
2024-05-22 16:13:36 +08:00
{
FFEditorResult result = {
2024-07-24 14:17:47 +08:00
.type = "Unknown",
2024-05-22 16:13:36 +08:00
.name = ffStrbufCreate(),
.path = ffStrbufCreate(),
.exe = ffStrbufCreate(),
2024-05-22 16:13:36 +08:00
.version = ffStrbufCreate(),
};
const char* error = ffDetectEditor(&result);
if (error)
{
ffPrintError(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2024-05-22 16:13:36 +08:00
}
2024-05-27 16:41:37 +08:00
if (options->moduleArgs.outputFormat.length == 0)
2024-05-22 16:13:36 +08:00
{
2024-05-27 16:41:37 +08:00
ffPrintLogoAndKey(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
if (result.exe.length)
{
ffStrbufWriteTo(&result.exe, stdout);
if (result.version.length)
2024-07-08 09:12:18 +08:00
printf(" %s", result.version.chars);
2024-05-27 16:41:37 +08:00
}
else
{
ffStrbufWriteTo(&result.name, stdout);
}
putchar('\n');
2024-05-22 16:13:36 +08:00
}
else
{
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
FF_FORMAT_ARG(result.type, "type"),
FF_FORMAT_ARG(result.name, "name"),
FF_FORMAT_ARG(result.exe, "exe-name"),
FF_FORMAT_ARG(result.path, "path"),
FF_FORMAT_ARG(result.version, "version"),
2024-05-27 16:41:37 +08:00
}));
2024-05-22 16:13:36 +08:00
}
ffStrbufDestroy(&result.name);
ffStrbufDestroy(&result.path);
ffStrbufDestroy(&result.exe);
2024-05-22 16:13:36 +08:00
ffStrbufDestroy(&result.version);
return true;
2024-05-22 16:13:36 +08:00
}
void ffParseEditorJsonObject(FFEditorOptions* options, yyjson_val* module)
{
yyjson_val *key, *val;
2024-05-22 16:13:36 +08:00
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key, val)
2024-05-22 16:13:36 +08:00
{
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
ffPrintError(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2024-05-22 16:13:36 +08:00
}
}
void ffGenerateEditorJsonConfig(FFEditorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2024-05-22 16:13:36 +08:00
}
bool ffGenerateEditorJsonResult(FF_MAYBE_UNUSED FFEditorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2024-05-22 16:13:36 +08:00
{
FFEditorResult result = {
.name = ffStrbufCreate(),
.path = ffStrbufCreate(),
.version = ffStrbufCreate(),
};
const char* error = ffDetectEditor(&result);
if (error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2024-05-22 16:13:36 +08:00
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
2024-07-24 14:17:47 +08:00
yyjson_mut_obj_add_str(doc, obj, "type", result.type);
2024-05-22 16:13:36 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name);
yyjson_mut_obj_add_strbuf(doc, obj, "path", &result.path);
yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe);
2024-05-22 16:13:36 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
ffStrbufDestroy(&result.name);
ffStrbufDestroy(&result.path);
ffStrbufDestroy(&result.exe);
2024-05-22 16:13:36 +08:00
ffStrbufDestroy(&result.version);
return true;
2024-05-22 16:13:36 +08:00
}
void ffInitEditorOptions(FFEditorOptions* options)
{
ffOptionInitModuleArg(&options->moduleArgs, "󱞎");
}
void ffDestroyEditorOptions(FFEditorOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffEditorModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_EDITOR_MODULE_NAME,
.description = "Print information of the default editor ($VISUAL or $EDITOR)",
.initOptions = (void*) ffInitEditorOptions,
.destroyOptions = (void*) ffDestroyEditorOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseEditorJsonObject,
.printModule = (void*) ffPrintEditor,
.generateJsonResult = (void*) ffGenerateEditorJsonResult,
.generateJsonConfig = (void*) ffGenerateEditorJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Type (Visual / Editor)", "type"},
{"Name", "name"},
{"Exe name of real path", "exe-name"},
{"Full path of real path", "path"},
2024-12-11 10:47:02 +08:00
{"Version", "version"},
}))
};