Skip to content

Commit 5cf6c68

Browse files
committed
args: exit --loop-ms with 'q' and increase from 50 to 200 ms
1 parent 65e9e0d commit 5cf6c68

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/main.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include <getopt.h>
2727
#include <unistd.h>
28+
#include <termios.h>
29+
#include <stdlib.h>
2830

2931
#include <algorithm>
3032
#include <cerrno>
@@ -61,6 +63,32 @@
6163

6264
using namespace std::string_view_literals;
6365

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+
6492
// Print the version and some other infos, then exit successfully
6593
static void version()
6694
{
@@ -149,8 +177,8 @@ ADVANCED/CONFIG:
149177
--list-logos List available ASCII logos in --data-dir.
150178
151179
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.
154182
155183
EXAMPLES:
156184
1. Minimal output with default logo:
@@ -789,7 +817,7 @@ int main(int argc, char *argv[])
789817
return 1;
790818
config.loadConfigFile(configFile, colors);
791819

792-
is_live_mode = (config.loop_ms > 50);
820+
is_live_mode = (config.loop_ms >= 200);
793821

794822
if (config.source_path.empty() || config.source_path == "off")
795823
config.args_disable_source = true;
@@ -832,7 +860,6 @@ int main(int argc, char *argv[])
832860
if (!config.wrap_lines)
833861
{
834862
// https://en.cppreference.com/w/c/program/exit
835-
// if something goes wrong like a segfault, then re-enable the cursor again
836863
std::atexit(enable_cursor);
837864

838865
// hide cursor and disable line wrapping
@@ -841,10 +868,23 @@ int main(int argc, char *argv[])
841868

842869
if (is_live_mode)
843870
{
844-
const std::chrono::milliseconds sleep_ms {config.loop_ms};
871+
enable_raw_mode();
872+
const std::chrono::milliseconds sleep_ms{ config.loop_ms };
845873

846874
while (true)
847875
{
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+
848888
// clear screen and go to position 0, 0
849889
write(STDOUT_FILENO, "\33[H\33[2J", 7);
850890
fmt::print("\033[0;0H");

0 commit comments

Comments
 (0)