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

113 lines
3.0 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
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;
}
ffPrintLogoAndKey(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-03-07 19:27:49 +08:00
ffStrbufPutTo(&result, stdout);
}
void ffInitCommandOptions(FFCommandOptions* options)
{
2023-08-17 10:56:54 +08:00
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_COMMAND_MODULE_NAME, ffParseCommandCommandOptions, ffParseCommandJsonObject, ffPrintCommand);
2023-03-07 19:27:49 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
ffStrbufInitStatic(&options->shell,
2023-03-07 19:27:49 +08:00
#ifdef _WIN32
"cmd.exe"
2023-03-07 19:27:49 +08:00
#else
"/bin/sh"
2023-03-07 19:27:49 +08:00
#endif
);
ffStrbufInit(&options->text);
}
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;
}
void ffDestroyCommandOptions(FFCommandOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
ffStrbufDestroy(&options->shell);
ffStrbufDestroy(&options->text);
}
2023-03-09 16:10:46 +08:00
2023-08-17 10:56:54 +08:00
void ffParseCommandJsonObject(FFCommandOptions* options, yyjson_val* module)
2023-03-09 16:10:46 +08:00
{
if (module)
2023-03-09 16:10:46 +08:00
{
2023-06-10 15:01:54 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
{
2023-06-10 15:01:54 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
2023-06-10 15:01:54 +08:00
continue;
2023-08-17 10:56:54 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-03-15 11:24:49 +08:00
if (ffStrEqualsIgnCase(key, "shell"))
{
2023-08-17 10:56:54 +08:00
ffStrbufSetS(&options->shell, yyjson_get_str(val));
continue;
}
2023-03-15 11:24:49 +08:00
if (ffStrEqualsIgnCase(key, "text"))
{
2023-08-17 10:56:54 +08:00
ffStrbufSetS(&options->text, yyjson_get_str(val));
continue;
}
2023-03-15 11:24:49 +08:00
2023-08-17 10:56:54 +08:00
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
}
2023-03-09 16:10:46 +08:00
}
}