From 09d36ccc9b3d424ed3f49986bb8a84693d4adfb9 Mon Sep 17 00:00:00 2001 From: dangooddd Date: Tue, 12 Nov 2024 19:58:10 +0300 Subject: [PATCH 1/4] Clear terminal output of interactive screen if alternate screen not in use --- include/ftxui/component/screen_interactive.hpp | 1 + src/ftxui/component/screen_interactive.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/ftxui/component/screen_interactive.hpp b/include/ftxui/component/screen_interactive.hpp index 6c79913..999fbbc 100644 --- a/include/ftxui/component/screen_interactive.hpp +++ b/include/ftxui/component/screen_interactive.hpp @@ -121,6 +121,7 @@ class ScreenInteractive : public Screen { bool mouse_captured = false; bool previous_frame_resized_ = false; + bool validated_ = false; bool frame_valid_ = false; bool force_handle_ctrl_c_ = true; diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index b5643ed..b49b8b8 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -858,6 +858,14 @@ void ScreenInteractive::Draw(Component component) { ResetCursorPosition(); std::cout << ResetPosition(/*clear=*/resized); + // clear terminal output if non-fullscreen interactive screen dimx decreases +#if !defined(_WIN32) + if ((dimx < dimx_) && validated_ && !use_alternative_screen_) { + std::cout << "\033[J"; + std::cout << "\033[H"; + } +#endif + // Resize the screen if needed. if (resized) { dimx_ = dimx; From 766be63c138fca485db87a30acae1527948f1a35 Mon Sep 17 00:00:00 2001 From: dangooddd Date: Tue, 12 Nov 2024 20:34:33 +0300 Subject: [PATCH 2/4] Set validated_ on first iteration on ScreenInteractive loop --- src/ftxui/component/screen_interactive.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index b49b8b8..bba13bb 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -932,6 +932,7 @@ void ScreenInteractive::Draw(Component component) { std::cout << ToString() << set_cursor_position; Flush(); Clear(); + validated_ = true; frame_valid_ = true; } From faf7d7e5602e542bc51f2cb3e8ec4b0410610a2e Mon Sep 17 00:00:00 2001 From: dangooddd Date: Wed, 13 Nov 2024 10:38:40 +0300 Subject: [PATCH 3/4] Add more comments (clear on resize) --- src/ftxui/component/screen_interactive.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index bba13bb..19754bf 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -858,11 +858,13 @@ void ScreenInteractive::Draw(Component component) { ResetCursorPosition(); std::cout << ResetPosition(/*clear=*/resized); - // clear terminal output if non-fullscreen interactive screen dimx decreases + // clear terminal output if screen dimx decreases + // only on primary screen + // only on POSIX systems (linux/macos) #if !defined(_WIN32) if ((dimx < dimx_) && validated_ && !use_alternative_screen_) { - std::cout << "\033[J"; - std::cout << "\033[H"; + std::cout << "\033[J"; // clear + std::cout << "\033[H"; // move cursor to home position } #endif From 3b01a5a0ad082cc3e89ce999dd50e1bf7ef28743 Mon Sep 17 00:00:00 2001 From: dangooddd Date: Thu, 14 Nov 2024 12:25:39 +0300 Subject: [PATCH 4/4] Remove unused variable, allow clear on resize for Windows --- include/ftxui/component/screen_interactive.hpp | 1 - src/ftxui/component/screen_interactive.cpp | 12 ++++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/include/ftxui/component/screen_interactive.hpp b/include/ftxui/component/screen_interactive.hpp index 999fbbc..6c79913 100644 --- a/include/ftxui/component/screen_interactive.hpp +++ b/include/ftxui/component/screen_interactive.hpp @@ -121,7 +121,6 @@ class ScreenInteractive : public Screen { bool mouse_captured = false; bool previous_frame_resized_ = false; - bool validated_ = false; bool frame_valid_ = false; bool force_handle_ctrl_c_ = true; diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index 19754bf..45a4fff 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -858,15 +858,12 @@ void ScreenInteractive::Draw(Component component) { ResetCursorPosition(); std::cout << ResetPosition(/*clear=*/resized); - // clear terminal output if screen dimx decreases - // only on primary screen - // only on POSIX systems (linux/macos) -#if !defined(_WIN32) - if ((dimx < dimx_) && validated_ && !use_alternative_screen_) { - std::cout << "\033[J"; // clear + // If the terminal width decrease, the terminal emulator will start wrapping + // lines and make the display dirty. We should clear it completely. + if ((dimx < dimx_) && !use_alternative_screen_) { + std::cout << "\033[J"; // clear terminal output std::cout << "\033[H"; // move cursor to home position } -#endif // Resize the screen if needed. if (resized) { @@ -934,7 +931,6 @@ void ScreenInteractive::Draw(Component component) { std::cout << ToString() << set_cursor_position; Flush(); Clear(); - validated_ = true; frame_valid_ = true; }