From 302b8a7b1b075c48f219b1fa1ea6cedb7a1d4bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 5 Nov 2025 10:00:31 +0800 Subject: [PATCH] CommandLine: adds dynamic output refresh support with interval option --- src/common/init.c | 13 ++++++++++--- src/data/help.json | 9 +++++++++ src/fastfetch.c | 33 +++++++++++++++++++++++++++++---- src/fastfetch.h | 1 + src/flashfetch.c | 2 ++ src/logo/logo.c | 3 +++ 6 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/common/init.c b/src/common/init.c index 9735c96ed..8e27ebaf1 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -30,6 +30,7 @@ static void initState(FFstate* state) ffPlatformInit(&state->platform); state->configDoc = NULL; state->resultDoc = NULL; + state->dynamicInterval = 0; { // don't enable bright color if the terminal is in light mode @@ -74,6 +75,9 @@ static void resetConsole(void) #if defined(_WIN32) fflush(stdout); #endif + + if(instance.state.dynamicInterval > 0) + fputs("\033[?1049l", stdout); // Disable alternate buffer } #ifdef _WIN32 @@ -130,14 +134,17 @@ void ffStart(void) if(ffDisableLinewrap) fputs("\033[?7l", stdout); + if(instance.state.dynamicInterval > 0) + { + fputs("\033[?1049h\033[H", stdout); // Enable alternate buffer + fflush(stdout); + } + ffLogoPrint(); } void ffFinish(void) { - if(instance.config.logo.printRemaining) - ffLogoPrintRemaining(); - resetConsole(); } diff --git a/src/data/help.json b/src/data/help.json index 745570ff2..df10972cc 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -74,6 +74,15 @@ }, "desc": "Enable or disable JSON output", "remark": "Shortcut for `--format json`" + }, + { + "long": "dynamic-interval", + "desc": "Keep fastfetch open and update the output every milliseconds", + "remark": "0 (default) to disable the behavior; don't work with --json", + "arg": { + "type": "num", + "default": 0 + } } ], "Config": [ diff --git a/src/fastfetch.c b/src/fastfetch.c index b3845246e..6a742a5e7 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -3,6 +3,7 @@ #include "common/init.h" #include "common/io/io.h" #include "common/jsonconfig.h" +#include "common/time.h" #include "detection/version/version.h" #include "logo/logo.h" #include "util/stringUtils.h" @@ -670,6 +671,8 @@ static void parseCommand(FFdata* data, char* key, char* value) {}, })); } + else if(ffStrEqualsIgnCase(key, "--dynamic-interval")) + instance.state.dynamicInterval = ffOptionParseUInt32(key, value); // seconds to milliseconds else return; @@ -768,15 +771,31 @@ static void run(FFdata* data) if (!instance.config.display.noBuffer) fflush(stdout); #endif - if (useJsonConfig) - ffPrintJsonConfig(false, instance.state.resultDoc); - else - ffPrintCommandOption(data, instance.state.resultDoc); + while (true) + { + if (useJsonConfig) + ffPrintJsonConfig(false, instance.state.resultDoc); + else + ffPrintCommandOption(data, instance.state.resultDoc); + + if (instance.state.dynamicInterval > 0) + { + fflush(stdout); + ffTimeSleep(instance.state.dynamicInterval); + fputs("\e[H", stdout); + } + else + break; + } if (instance.state.resultDoc) yyjson_mut_write_fp(stdout, instance.state.resultDoc, YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES | YYJSON_WRITE_NEWLINE_AT_END, NULL, NULL); else + { + if (instance.config.logo.printRemaining) + ffLogoPrintRemaining(); ffFinish(); + } } static void writeConfigFile(FFdata* data) @@ -835,6 +854,12 @@ int main(int argc, char** argv) }; parseArguments(&data, argc, argv, parseCommand); + if(instance.state.dynamicInterval && instance.state.resultDoc) + { + fprintf(stderr, "Error: --dynamic-interval cannot be used with --json\n"); + exit(400); + } + if(!data.configLoaded && !getenv("NO_CONFIG")) parseConfigFiles(); parseArguments(&data, argc, argv, (void*) parseOption); diff --git a/src/fastfetch.h b/src/fastfetch.h index f38bb864d..bb5197d6c 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -46,6 +46,7 @@ typedef struct FFstate yyjson_mut_doc* resultDoc; FFstrbuf genConfigPath; bool fullConfig; + uint32_t dynamicInterval; } FFstate; typedef struct FFinstance diff --git a/src/flashfetch.c b/src/flashfetch.c index f4256cff6..e4815686c 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -1,6 +1,7 @@ #include "fastfetch.h" #include "common/init.h" +#include "logo/logo.h" #include "modules/modules.h" // A dirty replicate of neofetch @@ -136,6 +137,7 @@ int main(void) ffPrintColors(&options); } + ffLogoPrintRemaining(); ffFinish(); ffDestroyInstance(); return 0; diff --git a/src/logo/logo.c b/src/logo/logo.c index 3081dc2c4..02ff36c9c 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -728,6 +728,9 @@ void ffLogoPrintLine(void) if(instance.state.logoWidth > 0) printf("\033[%uC", instance.state.logoWidth); + if (instance.state.dynamicInterval > 0) + fputs("\033[K", stdout); + ++instance.state.keysHeight; }