From a87e70c96e2105626e2a1db7575d30e58cfe744c Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sun, 30 Jun 2019 23:53:56 +0200 Subject: [PATCH] Handle terminal resize. This is related to bug: https://github.com/ArthurSonzogni/FTXUI/issues/3 --- src/ftxui/component/screen_interactive.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index 6df16c3..ca5ef5c 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -16,6 +16,9 @@ namespace ftxui { static const char* HIDE_CURSOR = "\e[?25l"; static const char* SHOW_CURSOR = "\e[?25h"; +static const char* DISABLE_LINE_WRAP = "\e[7l"; +static const char* ENABLE_LINE_WRAP = "\e[7h"; + std::stack> on_exit_functions; void OnExit(int signal) { while (!on_exit_functions.empty()) { @@ -27,6 +30,11 @@ void OnExit(int signal) { quick_exit(SIGINT); } +std::function on_resize = []{}; +void OnResize(int signal) { + on_resize(); +} + ScreenInteractive::ScreenInteractive(int dimx, int dimy, Dimension dimension) @@ -96,13 +104,21 @@ void ScreenInteractive::Loop(Component* component) { tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new); // Hide the cursor and show it at exit. - std::cout << HIDE_CURSOR << std::flush; - on_exit_functions.push([&]{ + std::cout << HIDE_CURSOR; + std::cout << DISABLE_LINE_WRAP; + std::cout << std::flush; + on_exit_functions.push([&] { std::cout << reset_cursor_position; std::cout << SHOW_CURSOR; + std::cout << ENABLE_LINE_WRAP; std::cout << std::flush; }); + // Handle resize. + on_resize = [&] { PostEvent(Event::Special({0})); }; + auto old_sigwinch_handler = std::signal(SIGWINCH, OnResize); + on_exit_functions.push([&] { std::signal(SIGWINCH, old_sigwinch_handler); }); + std::thread read_char([this] { while (!quit_) { auto event = Event::GetEvent([] { return (char)getchar(); }); @@ -117,7 +133,6 @@ void ScreenInteractive::Loop(Component* component) { Clear(); EventLoop(component); } - read_char.join(); OnExit(0); }