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

180 lines
4.8 KiB
C
Raw Normal View History

2023-03-07 19:27:49 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-03-07 19:27:49 +08:00
#include "common/processing.h"
#include "modules/command/command.h"
#include "util/stringUtils.h"
2023-03-07 19:27:49 +08:00
2023-10-07 13:40:40 +08:00
#define FF_COMMAND_NUM_FORMAT_ARGS 1
void ffPrintCommand(FFCommandOptions* options)
2023-03-07 19:27:49 +08:00
{
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
2023-03-07 19:27:49 +08:00
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
options->shell.chars,
#ifdef _WIN32
"/c",
#else
"-c",
#endif
options->text.chars,
NULL
});
if(error)
{
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
2023-03-07 19:27:49 +08:00
return;
}
if(!result.length)
{
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "No result printed");
2023-03-07 19:27:49 +08:00
return;
}
2023-10-07 13:40:40 +08:00
if (options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
ffStrbufPutTo(&result, stdout);
}
else
{
ffPrintFormat(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_COMMAND_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &result}
});
}
2023-03-07 19:27:49 +08:00
}
bool ffParseCommandCommandOptions(FFCommandOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_COMMAND_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
if(ffStrEqualsIgnCase(subKey, "shell"))
2023-03-07 19:27:49 +08:00
{
ffOptionParseString(key, value, &options->shell);
return true;
}
if(ffStrEqualsIgnCase(subKey, "text"))
2023-03-07 19:27:49 +08:00
{
ffOptionParseString(key, value, &options->text);
return true;
}
return false;
}
2023-08-17 10:56:54 +08:00
void ffParseCommandJsonObject(FFCommandOptions* options, yyjson_val* module)
2023-03-09 16:10:46 +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-03-09 16:10:46 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
if (ffStrEqualsIgnCase(key, "shell"))
{
ffStrbufSetS(&options->shell, yyjson_get_str(val));
continue;
}
if (ffStrEqualsIgnCase(key, "text"))
{
2023-08-17 16:09:41 +08:00
ffStrbufSetS(&options->text, yyjson_get_str(val));
continue;
}
2023-08-17 16:09:41 +08:00
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-03-09 16:10:46 +08:00
}
}
2023-08-30 14:13:39 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateCommandJsonConfig(FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyCommandOptions))) FFCommandOptions defaultOptions;
ffInitCommandOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
if (!ffStrbufEqual(&defaultOptions.shell, &options->shell))
yyjson_mut_obj_add_strbuf(doc, module, "shell", &options->shell);
if (!ffStrbufEqual(&defaultOptions.text, &options->text))
yyjson_mut_obj_add_strbuf(doc, module, "text", &options->text);
}
2023-10-23 10:14:22 +08:00
void ffGenerateCommandJsonResult(FF_MAYBE_UNUSED FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-08-30 14:13:39 +08:00
{
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
options->shell.chars,
#ifdef _WIN32
"/c",
#else
"-c",
#endif
options->text.chars,
NULL
});
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return;
}
if(!result.length)
{
yyjson_mut_obj_add_str(doc, module, "error", "No result printed");
return;
}
yyjson_mut_obj_add_strbuf(doc, module, "result", &result);
}
2023-10-07 13:40:40 +08:00
void ffPrintCommandHelpFormat(void)
{
ffPrintModuleFormatHelp(FF_COMMAND_MODULE_NAME, "{1}", FF_COMMAND_NUM_FORMAT_ARGS, (const char* []) {
"Command result"
});
}
2023-10-23 13:32:18 +08:00
void ffInitCommandOptions(FFCommandOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_COMMAND_MODULE_NAME,
ffParseCommandCommandOptions,
ffParseCommandJsonObject,
ffPrintCommand,
ffGenerateCommandJsonResult,
ffPrintCommandHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateCommandJsonConfig
);
2023-10-23 13:32:18 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
ffStrbufInitStatic(&options->shell,
#ifdef _WIN32
"cmd.exe"
#else
"/bin/sh"
#endif
);
ffStrbufInit(&options->text);
}
void ffDestroyCommandOptions(FFCommandOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
ffStrbufDestroy(&options->shell);
ffStrbufDestroy(&options->text);
}