Clear terminal on resize. (#99)

This commit is contained in:
Arthur Sonzogni 2021-05-17 00:44:37 +02:00 committed by GitHub
parent e520ac59f9
commit 30a85c4c5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 11 deletions

View File

@ -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();

View File

@ -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<std::vector<Pixel>>(dimy, std::vector<Pixel>(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.

View File

@ -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();
}