From bab3f90e2bad590ff1eb4e0124b0174d04415e6c Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Sun, 6 Feb 2011 03:52:29 -0500 Subject: [PATCH] Better cursor-handling to save explicit moves on output --- terminal.hpp | 3 ++- terminaldisplay.cpp | 13 +++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/terminal.hpp b/terminal.hpp index dd7e2b0..744be1f 100644 --- a/terminal.hpp +++ b/terminal.hpp @@ -17,11 +17,12 @@ namespace Terminal { bool initialized; Framebuffer last_frame; std::string current_rendition_string; + int cursor_x, cursor_y; public: Display( int width, int height ) : initialized( false ), last_frame( width, height ), - current_rendition_string() + current_rendition_string(), cursor_x( -1 ), cursor_y( -1 ) {} std::string new_frame( Framebuffer &f ); diff --git a/terminaldisplay.cpp b/terminaldisplay.cpp index 5459ef6..8209861 100644 --- a/terminaldisplay.cpp +++ b/terminaldisplay.cpp @@ -7,7 +7,6 @@ std::string Display::new_frame( Framebuffer &f ) f.back_color_erase(); std::string screen; - bool cursor_was_moved = false; /* has window title changed? */ if ( (!initialized) @@ -41,12 +40,10 @@ std::string Display::new_frame( Framebuffer &f ) /* clear screen */ screen.append( "\033[0m\033[H\033[2J" ); initialized = false; - cursor_was_moved = true; + cursor_x = cursor_y = 0; current_rendition_string = "\033[0m"; } - int cursor_x = -1, cursor_y = -1; - /* iterate for every cell */ for ( int y = 0; y < f.ds.get_height(); y++ ) { for ( int x = 0; x < f.ds.get_width(); /* let charwidth handle advance */ ) { @@ -62,7 +59,6 @@ std::string Display::new_frame( Framebuffer &f ) char curmove[ 32 ]; snprintf( curmove, 32, "\033[%d;%dH", y + 1, x + 1 ); screen.append( curmove ); - cursor_was_moved = true; } cursor_x = x; @@ -118,13 +114,14 @@ std::string Display::new_frame( Framebuffer &f ) /* has cursor location changed? */ if ( (!initialized) - || (f.ds.get_cursor_row() != last_frame.ds.get_cursor_row()) - || (f.ds.get_cursor_col() != last_frame.ds.get_cursor_col()) - || cursor_was_moved ) { + || (f.ds.get_cursor_row() != cursor_y) + || (f.ds.get_cursor_col() != cursor_x) ) { char curmove[ 32 ]; snprintf( curmove, 32, "\033[%d;%dH", f.ds.get_cursor_row() + 1, f.ds.get_cursor_col() + 1 ); screen.append( curmove ); + cursor_x = f.ds.get_cursor_col(); + cursor_y = f.ds.get_cursor_row(); } /* has cursor visibility changed? */