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

111 lines
5.0 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-06-01 11:37:52 +08:00
#include "detection/terminalshell/terminalshell.h"
#include "modules/shell/shell.h"
2026-03-29 09:28:49 +08:00
bool ffPrintShell(FFShellOptions* options) {
const FFShellResult* result = ffDetectShell();
2023-06-01 11:37:52 +08:00
2026-03-29 09:28:49 +08:00
if (result->processName.length == 0) {
ffPrintError(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Couldn't detect shell");
return false;
2023-06-01 11:37:52 +08:00
}
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
ffPrintLogoAndKey(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
ffStrbufWriteTo(&result->prettyName, stdout);
2023-06-01 11:37:52 +08:00
2026-03-29 09:28:49 +08:00
if (result->version.length > 0) {
2023-06-01 11:37:52 +08:00
putchar(' ');
ffStrbufWriteTo(&result->version, stdout);
2023-06-01 11:37:52 +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_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(result->processName, "process-name"),
FF_ARG(result->exe, "exe"),
FF_ARG(result->exeName, "exe-name"),
FF_ARG(result->version, "version"),
FF_ARG(result->pid, "pid"),
FF_ARG(result->prettyName, "pretty-name"),
FF_ARG(result->exePath, "exe-path"),
FF_ARG(result->tty, "tty"),
}));
2023-06-01 11:37:52 +08:00
}
return true;
2023-06-01 11:37:52 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseShellJsonObject(FFShellOptions* 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-06-01 11:37:52 +08:00
ffPrintError(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-06-01 11:37:52 +08:00
}
}
2023-09-08 16:33:19 +08:00
2026-03-29 09:28:49 +08:00
void ffGenerateShellJsonConfig(FFShellOptions* 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 ffGenerateShellJsonResult(FF_A_UNUSED FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
const FFShellResult* result = ffDetectShell();
2023-09-08 16:33:19 +08:00
2026-03-29 09:28:49 +08:00
if (result->processName.length == 0) {
2023-09-08 16:33:19 +08:00
yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect shell");
return false;
2023-09-08 16:33:19 +08:00
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result->exe);
yyjson_mut_obj_add_strcpy(doc, obj, "exeName", result->exeName);
2024-01-19 10:50:02 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "exePath", &result->exePath);
yyjson_mut_obj_add_uint(doc, obj, "pid", result->pid);
yyjson_mut_obj_add_uint(doc, obj, "ppid", result->ppid);
yyjson_mut_obj_add_strbuf(doc, obj, "processName", &result->processName);
2024-01-18 14:07:29 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &result->prettyName);
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result->version);
2026-03-29 09:28:49 +08:00
if (result->tty >= 0) {
2024-01-19 15:42:48 +08:00
yyjson_mut_obj_add_int(doc, obj, "tty", result->tty);
2026-03-29 09:28:49 +08:00
} else {
2024-01-19 15:42:48 +08:00
yyjson_mut_obj_add_null(doc, obj, "tty");
2026-03-29 09:28:49 +08:00
}
return true;
2023-09-08 16:33:19 +08:00
}
2023-10-07 13:40:40 +08:00
2026-03-29 09:28:49 +08:00
void ffInitShellOptions(FFShellOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "");
}
2026-03-29 09:28:49 +08:00
void ffDestroyShellOptions(FFShellOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffShellModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_SHELL_MODULE_NAME,
2026-04-16 18:27:11 +08:00
.description = "Print the current shell name and version",
.initOptions = (void*) ffInitShellOptions,
.destroyOptions = (void*) ffDestroyShellOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseShellJsonObject,
.printModule = (void*) ffPrintShell,
.generateJsonResult = (void*) ffGenerateShellJsonResult,
.generateJsonConfig = (void*) ffGenerateShellJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Shell process name", "process-name" },
{ "The first argument of the command line when running the shell", "exe" },
{ "Shell base name of arg0", "exe-name" },
{ "Shell version", "version" },
{ "Shell pid", "pid" },
{ "Shell pretty name", "pretty-name" },
{ "Shell full exe path", "exe-path" },
{ "Shell tty used", "tty" },
}))
};