Handle terminal resize.

This is related to bug:
https://github.com/ArthurSonzogni/FTXUI/issues/3
This commit is contained in:
ArthurSonzogni 2019-06-30 23:53:56 +02:00
parent b8a81bae9e
commit a87e70c96e

View File

@ -16,6 +16,9 @@ namespace ftxui {
static const char* HIDE_CURSOR = "\e[?25l"; static const char* HIDE_CURSOR = "\e[?25l";
static const char* SHOW_CURSOR = "\e[?25h"; 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<std::function<void()>> on_exit_functions; std::stack<std::function<void()>> on_exit_functions;
void OnExit(int signal) { void OnExit(int signal) {
while (!on_exit_functions.empty()) { while (!on_exit_functions.empty()) {
@ -27,6 +30,11 @@ void OnExit(int signal) {
quick_exit(SIGINT); quick_exit(SIGINT);
} }
std::function<void()> on_resize = []{};
void OnResize(int signal) {
on_resize();
}
ScreenInteractive::ScreenInteractive(int dimx, ScreenInteractive::ScreenInteractive(int dimx,
int dimy, int dimy,
Dimension dimension) Dimension dimension)
@ -96,13 +104,21 @@ void ScreenInteractive::Loop(Component* component) {
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new); tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new);
// Hide the cursor and show it at exit. // Hide the cursor and show it at exit.
std::cout << HIDE_CURSOR << std::flush; std::cout << HIDE_CURSOR;
std::cout << DISABLE_LINE_WRAP;
std::cout << std::flush;
on_exit_functions.push([&] { on_exit_functions.push([&] {
std::cout << reset_cursor_position; std::cout << reset_cursor_position;
std::cout << SHOW_CURSOR; std::cout << SHOW_CURSOR;
std::cout << ENABLE_LINE_WRAP;
std::cout << std::flush; 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] { std::thread read_char([this] {
while (!quit_) { while (!quit_) {
auto event = Event::GetEvent([] { return (char)getchar(); }); auto event = Event::GetEvent([] { return (char)getchar(); });
@ -117,7 +133,6 @@ void ScreenInteractive::Loop(Component* component) {
Clear(); Clear();
EventLoop(component); EventLoop(component);
} }
read_char.join(); read_char.join();
OnExit(0); OnExit(0);
} }