CommandLine: adds dynamic output refresh support with interval option

This commit is contained in:
李通洲
2025-11-05 10:00:31 +08:00
parent dbb9d8a660
commit 302b8a7b1b
6 changed files with 54 additions and 7 deletions
+10 -3
View File
@@ -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();
}
+9
View File
@@ -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 <num> milliseconds",
"remark": "0 (default) to disable the behavior; don't work with --json",
"arg": {
"type": "num",
"default": 0
}
}
],
"Config": [
+29 -4
View File
@@ -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);
+1
View File
@@ -46,6 +46,7 @@ typedef struct FFstate
yyjson_mut_doc* resultDoc;
FFstrbuf genConfigPath;
bool fullConfig;
uint32_t dynamicInterval;
} FFstate;
typedef struct FFinstance
+2
View File
@@ -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;
+3
View File
@@ -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;
}