25
25
26
26
#include < getopt.h>
27
27
#include < unistd.h>
28
+ #include < termios.h>
29
+ #include < stdlib.h>
28
30
29
31
#include < algorithm>
30
32
#include < cerrno>
61
63
62
64
using namespace std ::string_view_literals;
63
65
66
+ struct termios orig_termios;
67
+
68
+ static void disable_raw_mode ()
69
+ {
70
+ tcsetattr (STDIN_FILENO, TCSAFLUSH, &orig_termios);
71
+ }
72
+
73
+ static void enable_raw_mode ()
74
+ {
75
+ tcgetattr (STDIN_FILENO, &orig_termios);
76
+ atexit (disable_raw_mode);
77
+
78
+ struct termios raw = orig_termios;
79
+ raw.c_lflag &= ~(ECHO | ICANON); // Disable echo and canonical mode
80
+ tcsetattr (STDIN_FILENO, TCSAFLUSH, &raw);
81
+ }
82
+
83
+ static int kbhit ()
84
+ {
85
+ struct timeval tv = {0L , 0L };
86
+ fd_set fds;
87
+ FD_ZERO (&fds);
88
+ FD_SET (STDIN_FILENO, &fds);
89
+ return select (STDIN_FILENO + 1 , &fds, NULL , NULL , &tv) > 0 ;
90
+ }
91
+
64
92
// Print the version and some other infos, then exit successfully
65
93
static void version ()
66
94
{
@@ -149,8 +177,8 @@ ADVANCED/CONFIG:
149
177
--list-logos List available ASCII logos in --data-dir.
150
178
151
179
LIVE MODE:
152
- --loop-ms <NUM> Run in live mode, updating every <NUM> milliseconds (min: 50 ).
153
- Use 0 to disable (default) .
180
+ --loop-ms <NUM> Run in live mode, updating every <NUM> milliseconds (min: 200 ).
181
+ Press 'q' to exit .
154
182
155
183
EXAMPLES:
156
184
1. Minimal output with default logo:
@@ -789,7 +817,7 @@ int main(int argc, char *argv[])
789
817
return 1 ;
790
818
config.loadConfigFile (configFile, colors);
791
819
792
- is_live_mode = (config.loop_ms > 50 );
820
+ is_live_mode = (config.loop_ms >= 200 );
793
821
794
822
if (config.source_path .empty () || config.source_path == " off" )
795
823
config.args_disable_source = true ;
@@ -832,7 +860,6 @@ int main(int argc, char *argv[])
832
860
if (!config.wrap_lines )
833
861
{
834
862
// https://en.cppreference.com/w/c/program/exit
835
- // if something goes wrong like a segfault, then re-enable the cursor again
836
863
std::atexit (enable_cursor);
837
864
838
865
// hide cursor and disable line wrapping
@@ -841,10 +868,23 @@ int main(int argc, char *argv[])
841
868
842
869
if (is_live_mode)
843
870
{
844
- const std::chrono::milliseconds sleep_ms {config.loop_ms };
871
+ enable_raw_mode ();
872
+ const std::chrono::milliseconds sleep_ms{ config.loop_ms };
845
873
846
874
while (true )
847
875
{
876
+ if (kbhit ())
877
+ {
878
+ char c;
879
+ read (STDIN_FILENO, &c, 1 );
880
+ if (c == ' q' || c == ' Q' )
881
+ {
882
+ info (" exiting...\n " );
883
+ disable_raw_mode ();
884
+ break ;
885
+ }
886
+ }
887
+
848
888
// clear screen and go to position 0, 0
849
889
write (STDOUT_FILENO, " \33 [H\33 [2J" , 7 );
850
890
fmt::print (" \033 [0;0H" );
0 commit comments