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

140 lines
4.5 KiB
C
Raw Normal View History

2023-06-01 11:37:52 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-06-01 11:37:52 +08:00
#include "detection/terminalshell/terminalshell.h"
#include "modules/shell/shell.h"
#include "util/stringUtils.h"
2023-06-01 11:37:52 +08:00
2023-09-13 14:47:13 +08:00
#define FF_SHELL_NUM_FORMAT_ARGS 6
2023-06-01 11:37:52 +08:00
void ffPrintShell(FFShellOptions* options)
2023-06-01 11:37:52 +08:00
{
const FFShellResult* result = ffDetectShell();
2023-06-01 11:37:52 +08:00
if(result->processName.length == 0)
2023-06-01 11:37:52 +08:00
{
ffPrintError(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, "Couldn't detect shell");
2023-06-01 11:37:52 +08:00
return;
}
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
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');
}
else
{
ffPrintFormat(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_SHELL_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &result->processName},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->exe},
{FF_FORMAT_ARG_TYPE_STRING, result->exeName},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->version},
{FF_FORMAT_ARG_TYPE_UINT, &result->pid},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->prettyName},
2024-01-19 10:50:02 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &result->exePath},
2024-01-19 15:42:48 +08:00
{FF_FORMAT_ARG_TYPE_INT, &result->tty},
2023-06-01 11:37:52 +08:00
});
}
}
bool ffParseShellCommandOptions(FFShellOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_SHELL_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
2023-08-17 10:56:54 +08:00
void ffParseShellJsonObject(FFShellOptions* options, yyjson_val* module)
2023-06-01 11:37:52 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-06-01 11:37:52 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
2023-06-10 15:01:54 +08:00
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-06-01 11:37:52 +08:00
2023-08-17 16:09:41 +08:00
ffPrintError(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-06-01 11:37:52 +08:00
}
}
2023-09-08 16:33:19 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateShellJsonConfig(FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyShellOptions))) FFShellOptions defaultOptions;
ffInitShellOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
}
2023-10-23 10:14:22 +08:00
void ffGenerateShellJsonResult(FF_MAYBE_UNUSED FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-09-08 16:33:19 +08:00
{
const FFShellResult* result = ffDetectShell();
2023-09-08 16:33:19 +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;
}
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);
2024-01-19 15:42:48 +08:00
if (result->tty >= 0)
yyjson_mut_obj_add_int(doc, obj, "tty", result->tty);
else
yyjson_mut_obj_add_null(doc, obj, "tty");
2023-09-08 16:33:19 +08:00
}
2023-10-07 13:40:40 +08:00
void ffPrintShellHelpFormat(void)
{
ffPrintModuleFormatHelp(FF_SHELL_MODULE_NAME, "{3} {4}", FF_SHELL_NUM_FORMAT_ARGS, (const char* []) {
"Shell process name",
2024-01-19 10:50:02 +08:00
"The first argument of the command line when running the shell",
"Shell base name of arg0",
2023-10-07 13:40:40 +08:00
"Shell version",
"Shell pid",
2024-01-19 10:50:02 +08:00
"Shell pretty name",
"Shell full exe path",
2023-10-07 13:40:40 +08:00
});
}
2023-10-23 13:32:18 +08:00
void ffInitShellOptions(FFShellOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_SHELL_MODULE_NAME,
"Print current shell name and version",
ffParseShellCommandOptions,
ffParseShellJsonObject,
ffPrintShell,
ffGenerateShellJsonResult,
ffPrintShellHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateShellJsonConfig
);
2023-10-23 13:32:18 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
void ffDestroyShellOptions(FFShellOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}