Skip to content

Commit 302b8a7

Browse files
committed
CommandLine: adds dynamic output refresh support with interval option
1 parent dbb9d8a commit 302b8a7

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

src/common/init.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static void initState(FFstate* state)
3030
ffPlatformInit(&state->platform);
3131
state->configDoc = NULL;
3232
state->resultDoc = NULL;
33+
state->dynamicInterval = 0;
3334

3435
{
3536
// don't enable bright color if the terminal is in light mode
@@ -74,6 +75,9 @@ static void resetConsole(void)
7475
#if defined(_WIN32)
7576
fflush(stdout);
7677
#endif
78+
79+
if(instance.state.dynamicInterval > 0)
80+
fputs("\033[?1049l", stdout); // Disable alternate buffer
7781
}
7882

7983
#ifdef _WIN32
@@ -130,14 +134,17 @@ void ffStart(void)
130134
if(ffDisableLinewrap)
131135
fputs("\033[?7l", stdout);
132136

137+
if(instance.state.dynamicInterval > 0)
138+
{
139+
fputs("\033[?1049h\033[H", stdout); // Enable alternate buffer
140+
fflush(stdout);
141+
}
142+
133143
ffLogoPrint();
134144
}
135145

136146
void ffFinish(void)
137147
{
138-
if(instance.config.logo.printRemaining)
139-
ffLogoPrintRemaining();
140-
141148
resetConsole();
142149
}
143150

src/data/help.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@
7474
},
7575
"desc": "Enable or disable JSON output",
7676
"remark": "Shortcut for `--format json`"
77+
},
78+
{
79+
"long": "dynamic-interval",
80+
"desc": "Keep fastfetch open and update the output every <num> milliseconds",
81+
"remark": "0 (default) to disable the behavior; don't work with --json",
82+
"arg": {
83+
"type": "num",
84+
"default": 0
85+
}
7786
}
7887
],
7988
"Config": [

src/fastfetch.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "common/init.h"
44
#include "common/io/io.h"
55
#include "common/jsonconfig.h"
6+
#include "common/time.h"
67
#include "detection/version/version.h"
78
#include "logo/logo.h"
89
#include "util/stringUtils.h"
@@ -670,6 +671,8 @@ static void parseCommand(FFdata* data, char* key, char* value)
670671
{},
671672
}));
672673
}
674+
else if(ffStrEqualsIgnCase(key, "--dynamic-interval"))
675+
instance.state.dynamicInterval = ffOptionParseUInt32(key, value); // seconds to milliseconds
673676
else
674677
return;
675678

@@ -768,15 +771,31 @@ static void run(FFdata* data)
768771
if (!instance.config.display.noBuffer) fflush(stdout);
769772
#endif
770773

771-
if (useJsonConfig)
772-
ffPrintJsonConfig(false, instance.state.resultDoc);
773-
else
774-
ffPrintCommandOption(data, instance.state.resultDoc);
774+
while (true)
775+
{
776+
if (useJsonConfig)
777+
ffPrintJsonConfig(false, instance.state.resultDoc);
778+
else
779+
ffPrintCommandOption(data, instance.state.resultDoc);
780+
781+
if (instance.state.dynamicInterval > 0)
782+
{
783+
fflush(stdout);
784+
ffTimeSleep(instance.state.dynamicInterval);
785+
fputs("\e[H", stdout);
786+
}
787+
else
788+
break;
789+
}
775790

776791
if (instance.state.resultDoc)
777792
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);
778793
else
794+
{
795+
if (instance.config.logo.printRemaining)
796+
ffLogoPrintRemaining();
779797
ffFinish();
798+
}
780799
}
781800

782801
static void writeConfigFile(FFdata* data)
@@ -835,6 +854,12 @@ int main(int argc, char** argv)
835854
};
836855

837856
parseArguments(&data, argc, argv, parseCommand);
857+
if(instance.state.dynamicInterval && instance.state.resultDoc)
858+
{
859+
fprintf(stderr, "Error: --dynamic-interval cannot be used with --json\n");
860+
exit(400);
861+
}
862+
838863
if(!data.configLoaded && !getenv("NO_CONFIG"))
839864
parseConfigFiles();
840865
parseArguments(&data, argc, argv, (void*) parseOption);

src/fastfetch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ typedef struct FFstate
4646
yyjson_mut_doc* resultDoc;
4747
FFstrbuf genConfigPath;
4848
bool fullConfig;
49+
uint32_t dynamicInterval;
4950
} FFstate;
5051

5152
typedef struct FFinstance

src/flashfetch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "fastfetch.h"
22

33
#include "common/init.h"
4+
#include "logo/logo.h"
45
#include "modules/modules.h"
56

67
// A dirty replicate of neofetch
@@ -136,6 +137,7 @@ int main(void)
136137
ffPrintColors(&options);
137138
}
138139

140+
ffLogoPrintRemaining();
139141
ffFinish();
140142
ffDestroyInstance();
141143
return 0;

src/logo/logo.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,9 @@ void ffLogoPrintLine(void)
728728
if(instance.state.logoWidth > 0)
729729
printf("\033[%uC", instance.state.logoWidth);
730730

731+
if (instance.state.dynamicInterval > 0)
732+
fputs("\033[K", stdout);
733+
731734
++instance.state.keysHeight;
732735
}
733736

0 commit comments

Comments
 (0)