Improve command input and adding resize handling.

This commit is contained in:
2026-08-23 13:16:08 +01:00
parent f4e3a190b8
commit 6f1c87400a
5 changed files with 162 additions and 60 deletions
+12 -1
View File
@@ -3,11 +3,17 @@
namespace bed::internal::io {
termios IO::orig_termios{};
bool IO::cleaned = false;
volatile std::atomic_bool IO::resized(false);
IO::IO() {
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1)
throw fatal_error("Can't get terminal state.", 1);
atexit(cleanup);
struct sigaction sa{};
sa.sa_handler = handle_sigwinch;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGWINCH, &sa, nullptr) == -1)
throw fatal_error("Can't install SIGWINCH handler.", 1);
struct termios raw = orig_termios;
raw.c_iflag &= ~(BRKINT | ISTRIP | IXON);
raw.c_cflag |= (CS8);
@@ -18,6 +24,7 @@ IO::IO() {
throw fatal_error("Can't set terminal state.", 1);
std::string os = "\x1b[?2004h";
write_all(STDOUT_FILENO, os.c_str(), os.size());
atexit(cleanup);
}
IO::~IO() {
@@ -73,6 +80,10 @@ void IO::cleanup() {
cleaned = true;
}
void IO::handle_sigwinch(int) {
resized.store(true);
}
void IO::move_cursor(uint16_t row, uint16_t col) {
char buf[32];
int n = snprintf(buf, sizeof(buf), "\x1b[%u;%uH", row, col);