Command: adds parallel and useStdErr options

Fixes #2045
This commit is contained in:
李通洲
2025-11-10 16:02:53 +08:00
parent 7aaad5e03e
commit ea92ad3785
9 changed files with 124 additions and 32 deletions
+1
View File
@@ -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
+10
View File
@@ -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"
},
+12 -6
View File
@@ -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",
+10
View File
@@ -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))
{
+60
View File
@@ -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;
}
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#include "fastfetch.h"
#include "modules/command/option.h"
const char* ffDetectCommand(FFCommandOptions* options, FFstrbuf* result);
+21 -26
View File
@@ -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)
+2
View File
@@ -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);
+2
View File
@@ -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");