diff --git a/CMakeLists.txt b/CMakeLists.txt index fc0451893..2b7ed8b72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -263,6 +263,7 @@ set(LIBFASTFETCH_SRC src/modules/cpuUsage.c src/modules/cursor.c src/modules/custom.c + src/modules/command.c src/modules/date.c src/modules/datetime.c src/modules/de.c diff --git a/src/common/init.c b/src/common/init.c index e94ea4e02..e2c32c3c0 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -350,6 +350,18 @@ static void defaultConfig(FFinstance* instance) ffStrbufInitA(&instance->config.playerName, 0); instance->config.percentType = 1; + + ffStrbufInitS(&instance->config.commandShell, + #ifdef _WIN32 + "cmd" + #elif defined(__FreeBSD__) + "csh" + #else + "bash" + #endif + ); + ffListInit(&instance->config.commandKeys, sizeof(FFstrbuf)); + ffListInit(&instance->config.commandTexts, sizeof(FFstrbuf)); } void ffInitInstance(FFinstance* instance) @@ -564,6 +576,14 @@ static void destroyConfig(FFinstance* instance) ffStrbufDestroy(&instance->config.weatherOutputFormat); ffStrbufDestroy(&instance->config.osFile); ffStrbufDestroy(&instance->config.playerName); + + ffStrbufDestroy(&instance->config.commandShell); + FF_LIST_FOR_EACH(FFstrbuf, item, instance->config.commandKeys) + ffStrbufDestroy(item); + ffListDestroy(&instance->config.commandKeys); + FF_LIST_FOR_EACH(FFstrbuf, item, instance->config.commandTexts) + ffStrbufDestroy(item); + ffListDestroy(&instance->config.commandTexts); } static void destroyState(FFinstance* instance) diff --git a/src/common/processing_linux.c b/src/common/processing_linux.c index d8bb4e0bd..a628aeb3d 100644 --- a/src/common/processing_linux.c +++ b/src/common/processing_linux.c @@ -31,8 +31,8 @@ const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]) //Parent close(pipes[1]); waitpid(childPid, NULL, 0); - ffAppendFDBuffer(pipes[0], buffer); + bool ok = ffAppendFDBuffer(pipes[0], buffer); close(pipes[0]); - return NULL; + return ok ? NULL : "ffAppendFDBuffer() failed"; } diff --git a/src/data/help.txt b/src/data/help.txt index 81b52dd7f..441c1c6cf 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -126,6 +126,9 @@ Module specific options: --player-name: The name of the player to use --gl : Set the OpenGL context creation library to use. Must be auto, egl, glx or osmesa. Default is auto --percent-type : Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only. Default is 1 + --command-shell : Set the shell program to execute the command text. Default is cmd for Windows, csh for FreeBSD, bash for others + --command-key : Set the module key to display, can be specified mulitple times + --command-text : Set the command text to be executed, can be specified mulitple times Parsing is not case sensitive. E.g. "--lib-PCI" is equal to "--Lib-Pci" If a value starts with a ?, it is optional. "true" will be used if not set. diff --git a/src/fastfetch.c b/src/fastfetch.c index a18e3256e..e732422c3 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1301,6 +1301,20 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con } else if(strcasecmp(key, "--percent-type") == 0) instance->config.percentType = optionParseUInt32(key, value); + else if(strcasecmp(key, "--command-shell") == 0) + optionParseString(key, value, &instance->config.commandShell); + else if(strcasecmp(key, "--command-key") == 0) + { + FFstrbuf* result = (FFstrbuf*) ffListAdd(&instance->config.commandKeys); + ffStrbufInit(result); + optionParseString(key, value, result); + } + else if(strcasecmp(key, "--command-text") == 0) + { + FFstrbuf* result = (FFstrbuf*) ffListAdd(&instance->config.commandTexts); + ffStrbufInit(result); + optionParseString(key, value, result); + } ////////////////// //Unknown option// @@ -1454,6 +1468,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintOpenCL(instance); else if(strcasecmp(line, "users") == 0) ffPrintUsers(instance); + else if(strcasecmp(line, "command") == 0) + ffPrintCommand(instance); else ffPrintErrorString(instance, line, 0, NULL, NULL, ""); } diff --git a/src/fastfetch.h b/src/fastfetch.h index 8ee5cd356..c793b3c0e 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -208,6 +208,10 @@ typedef struct FFconfig FFstrbuf playerName; uint32_t percentType; + + FFstrbuf commandShell; + FFlist commandKeys; + FFlist commandTexts; } FFconfig; typedef struct FFstate @@ -314,5 +318,6 @@ void ffPrintVulkan(FFinstance* instance); void ffPrintOpenGL(FFinstance* instance); void ffPrintOpenCL(FFinstance* instance); void ffPrintUsers(FFinstance* instance); +void ffPrintCommand(FFinstance* instance); #endif diff --git a/src/modules/command.c b/src/modules/command.c new file mode 100644 index 000000000..1b767013b --- /dev/null +++ b/src/modules/command.c @@ -0,0 +1,67 @@ +#include "fastfetch.h" + +#include "common/printing.h" +#include "common/processing.h" +#include "util/textModifier.h" + +#define FF_COMMAND_MODULE_NAME "Command" + +static void printError(FFinstance* instance, const char* key, const char* error) +{ + ffPrintLogoAndKey(instance, key, 0, NULL); + + if(instance->config.pipe) + fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stdout); + + fputs(error, stdout); + + if(!instance->config.pipe) + fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout); + + putchar('\n'); +} + +void ffPrintCommand(FFinstance* instance) +{ + FF_STRBUF_AUTO_DESTROY key; + ffStrbufInit(&key); + + if(!ffListShift(&instance->config.commandKeys, &key)) + ffStrbufInitS(&key, FF_COMMAND_MODULE_NAME); + + FF_STRBUF_AUTO_DESTROY text; + ffStrbufInit(&text); + if(!ffListShift(&instance->config.commandTexts, &text)) + { + printError(instance, key.chars, "No command text left"); + return; + } + + FF_STRBUF_AUTO_DESTROY result; + ffStrbufInit(&result); + const char* error = ffProcessAppendStdOut(&result, (char* const[]){ + instance->config.commandShell.chars, + #ifdef _WIN32 + "/c", + #else + "-c", + #endif + text.chars, + NULL + }); + + if(error) + { + printError(instance, key.chars, error); + return; + } + + if(!result.length) + { + printError(instance, key.chars, "No result printed"); + return; + } + + ffPrintLogoAndKey(instance, key.chars, 0, NULL); + puts(result.chars); +}