From fc4022d1736e2cce3810698b9fe8a7832625277b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 8 Jul 2019 19:04:13 +0200 Subject: [PATCH] Shell: Handle SIGWINCH to get a nice behavior when resizing. When resizing the terminal, we now clear the entire current line and reset the shell's LineEditor input state. This makes it look and feel kinda the same as xterm. Fixes #286. --- Shell/GlobalState.h | 1 + Shell/LineEditor.cpp | 8 +++++++- Shell/main.cpp | 36 ++++++++++-------------------------- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/Shell/GlobalState.h b/Shell/GlobalState.h index 4fd85330a6..b15702fc58 100644 --- a/Shell/GlobalState.h +++ b/Shell/GlobalState.h @@ -13,6 +13,7 @@ struct GlobalState { uid_t uid; struct termios termios; bool was_interrupted { false }; + bool was_resized { false }; }; extern GlobalState g; diff --git a/Shell/LineEditor.cpp b/Shell/LineEditor.cpp index 78b70794a4..b1bd1df8e8 100644 --- a/Shell/LineEditor.cpp +++ b/Shell/LineEditor.cpp @@ -50,10 +50,16 @@ String LineEditor::get_line() if (nread < 0) { if (errno == EINTR) { if (g.was_interrupted) { + g.was_interrupted = false; if (!m_buffer.is_empty()) printf("^C"); } - g.was_interrupted = false; + if (g.was_resized) { + g.was_resized = false; + printf("\033[2K\r"); + m_buffer.clear(); + return String::empty(); + } m_buffer.clear(); putchar('\n'); return String::empty(); diff --git a/Shell/main.cpp b/Shell/main.cpp index f16b3fed46..cc1ecae3a1 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -50,11 +50,6 @@ void did_receive_signal(int signum) g_got_signal = true; } -void handle_sigint(int) -{ - g.was_interrupted = true; -} - static int sh_exit(int, char**) { printf("Good-bye!\n"); @@ -561,11 +556,6 @@ void save_history() } } -void handle_sighup(int) -{ - save_history(); -} - int main(int argc, char** argv) { g.uid = getuid(); @@ -573,23 +563,17 @@ int main(int argc, char** argv) tcsetpgrp(0, getpgrp()); tcgetattr(0, &g.termios); - { - struct sigaction sa; - sa.sa_handler = handle_sigint; - sa.sa_flags = 0; - sa.sa_mask = 0; - int rc = sigaction(SIGINT, &sa, nullptr); - assert(rc == 0); - } + signal(SIGINT, [](int) { + g.was_interrupted = true; + }); - { - struct sigaction sa; - sa.sa_handler = handle_sighup; - sa.sa_flags = 0; - sa.sa_mask = 0; - int rc = sigaction(SIGHUP, &sa, nullptr); - assert(rc == 0); - } + signal(SIGHUP, [](int) { + save_history(); + }); + + signal(SIGWINCH, [](int) { + g.was_resized = true; + }); int rc = gethostname(g.hostname, sizeof(g.hostname)); if (rc < 0)