diff --git a/CMakeLists.txt b/CMakeLists.txt index 65353bd63..0b561110f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -389,6 +389,7 @@ set(LIBFASTFETCH_SRC src/detection/chassis/chassis.c src/detection/cpu/cpu.c src/detection/cpuusage/cpuusage.c + src/detection/command/command.c src/detection/disk/disk.c src/detection/diskio/diskio.c src/detection/displayserver/displayserver.c diff --git a/doc/json_schema.json b/doc/json_schema.json index 3db2dfb64..32a992b93 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -2101,6 +2101,16 @@ "description": "Set the command text to be executed", "type": "string" }, + "useStdErr": { + "description": "Set if stderr should be used instead of stdout for command output", + "type": "boolean", + "default": false + }, + "parallel": { + "description": "Set if the command should be executed in parallel with other commands\nMight improve performance when using multiple commands, but may cause issues with some commands", + "type": "boolean", + "default": false + }, "key": { "$ref": "#/$defs/key" }, diff --git a/presets/examples/25.jsonc b/presets/examples/25.jsonc index 5d0ce75bb..0fa0cf471 100644 --- a/presets/examples/25.jsonc +++ b/presets/examples/25.jsonc @@ -174,7 +174,8 @@ "keyIcon": "", // Custom icon override "key": "│{#red}│ {icon} Rust │{$4}│{#keys}│{$2}", "text": "rustc --version", - "format": "rustc {~6,13}" // Print 6th to 13th characters (version number) + "format": "rustc {~6,13}", // Print 6th to 13th characters (version number) + "parallel": true // Enable parallel execution for performance }, { "type": "command", @@ -194,28 +195,32 @@ "keyIcon": "", "key": "│{#red}│ {icon} Clang │{$4}│{#keys}│{$2}", "text": "clang --version | findstr version", // Finds the line with "version" - "format": "clang {~-6}" // Prints the last 6 characters (version number) + "format": "clang {~-6}", // Prints the last 6 characters (version number) + "parallel": true }, { "type": "command", "keyIcon": "", "key": "│{#red}│ {icon} NodeJS │{$4}│{#keys}│{$2}", "text": "node --version", - "format": "node {~1}" // {~1} removes first character (v) + "format": "node {~1}", // {~1} removes first character (v) + "parallel": true }, { "type": "command", "keyIcon": "", "key": "│{#red}│ {icon} Go │{$4}│{#keys}│{$2}", "text": "go version | cut -d' ' -f3", - "format": "go {~2}" // {~2} removes first 2 characters (go) + "format": "go {~2}", // {~2} removes first 2 characters (go) + "parallel": true }, { "type": "command", "keyIcon": "", "key": "│{#red}│ {icon} Zig │{$4}│{#keys}│{$2}", "text": "zig version", - "format": "zig {}" + "format": "zig {}", + "parallel": true }, { "type": "editor", @@ -226,7 +231,8 @@ "keyIcon": "󰊢", "key": "│{#red}│ {icon} Git │{$4}│{#keys}│{$2}", "text": "git version", - "format": "git {~12}" + "format": "git {~12}", + "parallel": true }, { "type": "font", diff --git a/src/common/jsonconfig.c b/src/common/jsonconfig.c index 7cd73d9a4..208710628 100644 --- a/src/common/jsonconfig.c +++ b/src/common/jsonconfig.c @@ -158,6 +158,16 @@ static void prepareModuleJsonObject(const char* type, yyjson_val* module) ffPrepareCPUUsage(); break; } + case 'c': case 'C': { + if (ffStrEqualsIgnCase(type, FF_COMMAND_MODULE_NAME)) + { + __attribute__((__cleanup__(ffDestroyCommandOptions))) FFCommandOptions options; + ffInitCommandOptions(&options); + if (module) ffCommandModuleInfo.parseJsonObject(&options, module); + ffPrepareCommand(&options); + } + break; + } case 'd': case 'D': { if (ffStrEqualsIgnCase(type, FF_DISKIO_MODULE_NAME)) { diff --git a/src/detection/command/command.c b/src/detection/command/command.c new file mode 100644 index 000000000..2a6f08481 --- /dev/null +++ b/src/detection/command/command.c @@ -0,0 +1,60 @@ +#include "common/processing.h" +#include "util/FFstrbuf.h" +#include "detection/command/command.h" + +typedef struct FFCommandResultBundle +{ + FFProcessHandle handle; + const char* error; +} FFCommandResultBundle; + +// FIFO, non-thread-safe list of running commands +static FFlist commandQueue = { + .elementSize = sizeof(FFCommandResultBundle), +}; + +static const char* spawnProcess(FFCommandOptions* options, FFProcessHandle* handle) +{ + if (options->text.length == 0) + return "No command text specified"; + + return ffProcessSpawn(options->param.length ? (char* const[]){ + options->shell.chars, + options->param.chars, + options->text.chars, + NULL + } : (char* const[]){ + options->shell.chars, + options->text.chars, + NULL + }, options->useStdErr, handle); +} + +bool ffPrepareCommand(FFCommandOptions* options) +{ + if (!options->parallel) return false; + + FFCommandResultBundle* bundle = ffListAdd(&commandQueue); + bundle->error = spawnProcess(options, &bundle->handle); + + return true; +} + +const char* ffDetectCommand(FFCommandOptions* options, FFstrbuf* result) +{ + FFCommandResultBundle bundle; + if (!options->parallel) + bundle.error = spawnProcess(options, &bundle.handle); + else + ffListShift(&commandQueue, &bundle); + + if (bundle.error) + return bundle.error; + + bundle.error = ffProcessReadOutput(&bundle.handle, result); + if (bundle.error) + return bundle.error; + + ffStrbufTrimRightSpace(result); + return NULL; +} diff --git a/src/detection/command/command.h b/src/detection/command/command.h new file mode 100644 index 000000000..73acc0abc --- /dev/null +++ b/src/detection/command/command.h @@ -0,0 +1,6 @@ +#pragma once + +#include "fastfetch.h" +#include "modules/command/option.h" + +const char* ffDetectCommand(FFCommandOptions* options, FFstrbuf* result); diff --git a/src/modules/command/command.c b/src/modules/command/command.c index a46d77115..0ba430a3a 100644 --- a/src/modules/command/command.c +++ b/src/modules/command/command.c @@ -1,30 +1,20 @@ #include "common/printing.h" #include "common/jsonconfig.h" -#include "common/processing.h" #include "modules/command/command.h" -#include "util/stringUtils.h" +#include "detection/command/command.h" bool ffPrintCommand(FFCommandOptions* options) { FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); - const char* error = ffProcessAppendStdOut(&result, options->param.length ? (char* const[]){ - options->shell.chars, - options->param.chars, - options->text.chars, - NULL - } : (char* const[]){ - options->shell.chars, - options->text.chars, - NULL - }); + const char* error = ffDetectCommand(options, &result); - if(error) + if (error) { ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); return false; } - if(!result.length) + if (!result.length) { ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No result generated"); return false; @@ -72,6 +62,18 @@ void ffParseCommandJsonObject(FFCommandOptions* options, yyjson_val* module) continue; } + if (unsafe_yyjson_equals_str(key, "useStdErr")) + { + options->useStdErr = yyjson_get_bool(val); + continue; + } + + if (unsafe_yyjson_equals_str(key, "parallel")) + { + options->parallel = yyjson_get_bool(val); + continue; + } + ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key)); } } @@ -81,25 +83,16 @@ void ffGenerateCommandJsonConfig(FFCommandOptions* options, yyjson_mut_doc* doc, ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); yyjson_mut_obj_add_strbuf(doc, module, "shell", &options->shell); - yyjson_mut_obj_add_strbuf(doc, module, "param", &options->param); - yyjson_mut_obj_add_strbuf(doc, module, "text", &options->text); + yyjson_mut_obj_add_bool(doc, module, "useStdErr", options->useStdErr); + yyjson_mut_obj_add_bool(doc, module, "parallel", options->parallel); } bool ffGenerateCommandJsonResult(FF_MAYBE_UNUSED FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); - const char* error = ffProcessAppendStdOut(&result, options->param.length ? (char* const[]){ - options->shell.chars, - options->param.chars, - options->text.chars, - NULL - } : (char* const[]){ - options->shell.chars, - options->text.chars, - NULL - }); + const char* error = ffDetectCommand(options, &result); if(error) { @@ -137,6 +130,8 @@ void ffInitCommandOptions(FFCommandOptions* options) #endif ); ffStrbufInit(&options->text); + options->useStdErr = false; + options->parallel = false; } void ffDestroyCommandOptions(FFCommandOptions* options) diff --git a/src/modules/command/command.h b/src/modules/command/command.h index 204553747..e0e143e01 100644 --- a/src/modules/command/command.h +++ b/src/modules/command/command.h @@ -4,6 +4,8 @@ #define FF_COMMAND_MODULE_NAME "Command" +bool ffPrepareCommand(FFCommandOptions* options); + bool ffPrintCommand(FFCommandOptions* options); void ffInitCommandOptions(FFCommandOptions* options); void ffDestroyCommandOptions(FFCommandOptions* options); diff --git a/src/modules/command/option.h b/src/modules/command/option.h index 62fbeb981..e85a65f15 100644 --- a/src/modules/command/option.h +++ b/src/modules/command/option.h @@ -9,6 +9,8 @@ typedef struct FFCommandOptions FFstrbuf shell; FFstrbuf param; FFstrbuf text; + bool useStdErr; + bool parallel; } FFCommandOptions; static_assert(sizeof(FFCommandOptions) <= FF_OPTION_MAX_SIZE, "FFCommandOptions size exceeds maximum allowed size");