From 30a85c4c5ba96882336adc7af12da50b900b0836 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Mon, 17 May 2021 00:44:37 +0200 Subject: [PATCH] Clear terminal on resize. (#99) --- include/ftxui/screen/screen.hpp | 2 +- src/ftxui/component/screen_interactive.cpp | 15 +++++++++------ src/ftxui/screen/screen.cpp | 16 ++++++++++++---- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/ftxui/screen/screen.hpp b/include/ftxui/screen/screen.hpp index 0c75202..91cbce9 100644 --- a/include/ftxui/screen/screen.hpp +++ b/include/ftxui/screen/screen.hpp @@ -63,7 +63,7 @@ class Screen { int dimy() { return dimy_; } // Move the terminal cursor n-lines up with n = dimy(). - std::string ResetPosition(); + std::string ResetPosition(bool clear = false); // Fill with space. void Clear(); diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index 786f40a..0b45c6e 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -382,11 +382,6 @@ void ScreenInteractive::Loop(Component component) { // The main loop. while (!quit_) { if (!event_receiver_->HasPending()) { - std::cout << reset_cursor_position << ResetPosition(); - static int i = -2; - if (i % 10 == 0) - std::cout << DeviceStatusReport(DSRMode::kCursor); - ++i; Draw(component); std::cout << ToString() << set_cursor_position; Flush(); @@ -442,8 +437,11 @@ void ScreenInteractive::Draw(Component component) { break; } + bool resized = (dimx != dimx_) || (dimy != dimy_); + std::cout << reset_cursor_position << ResetPosition(/*clear=*/resized); + // Resize the screen if needed. - if (dimx != dimx_ || dimy != dimy_) { + if (resized) { dimx_ = dimx; dimy_ = dimy; pixels_ = std::vector>(dimy, std::vector(dimx)); @@ -451,6 +449,11 @@ void ScreenInteractive::Draw(Component component) { cursor_.y = dimy_ - 1; } + static int i = -2; + if (i % 10 == 0) + std::cout << DeviceStatusReport(DSRMode::kCursor); + ++i; + Render(*this, document); // Set cursor position for user using tools to insert CJK characters. diff --git a/src/ftxui/screen/screen.cpp b/src/ftxui/screen/screen.cpp index 110fd6b..8e51444 100644 --- a/src/ftxui/screen/screen.cpp +++ b/src/ftxui/screen/screen.cpp @@ -36,6 +36,7 @@ static const wchar_t* INVERTED_RESET = L"\x1B[27m"; static const char* MOVE_LEFT = "\r"; static const char* MOVE_UP = "\x1B[1A"; +static const char* CLEAR_LINE = "\x1B[2K"; bool In(const Box& stencil, int x, int y) { return stencil.x_min <= x && x <= stencil.x_max && // @@ -219,11 +220,18 @@ Pixel& Screen::PixelAt(int x, int y) { /// /// @return The string to print in order to reset the cursor position to the /// beginning. -std::string Screen::ResetPosition() { +std::string Screen::ResetPosition(bool clear) { std::stringstream ss; - ss << MOVE_LEFT; - for (int y = 1; y < dimy_; ++y) { - ss << MOVE_UP; + if (clear) { + ss << MOVE_LEFT << CLEAR_LINE; + for (int y = 1; y < dimy_; ++y) { + ss << MOVE_UP << CLEAR_LINE; + } + } else { + ss << MOVE_LEFT; + for (int y = 1; y < dimy_; ++y) { + ss << MOVE_UP; + } } return ss.str(); }