Better cursor-handling to save explicit moves on output

This commit is contained in:
Keith Winstein
2011-02-06 03:52:29 -05:00
parent 9ee3ef57bb
commit bab3f90e2b
2 changed files with 7 additions and 9 deletions
+2 -1
View File
@@ -17,11 +17,12 @@ namespace Terminal {
bool initialized; bool initialized;
Framebuffer last_frame; Framebuffer last_frame;
std::string current_rendition_string; std::string current_rendition_string;
int cursor_x, cursor_y;
public: public:
Display( int width, int height ) Display( int width, int height )
: initialized( false ), last_frame( width, 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 ); std::string new_frame( Framebuffer &f );
+5 -8
View File
@@ -7,7 +7,6 @@ std::string Display::new_frame( Framebuffer &f )
f.back_color_erase(); f.back_color_erase();
std::string screen; std::string screen;
bool cursor_was_moved = false;
/* has window title changed? */ /* has window title changed? */
if ( (!initialized) if ( (!initialized)
@@ -41,12 +40,10 @@ std::string Display::new_frame( Framebuffer &f )
/* clear screen */ /* clear screen */
screen.append( "\033[0m\033[H\033[2J" ); screen.append( "\033[0m\033[H\033[2J" );
initialized = false; initialized = false;
cursor_was_moved = true; cursor_x = cursor_y = 0;
current_rendition_string = "\033[0m"; current_rendition_string = "\033[0m";
} }
int cursor_x = -1, cursor_y = -1;
/* iterate for every cell */ /* iterate for every cell */
for ( int y = 0; y < f.ds.get_height(); y++ ) { for ( int y = 0; y < f.ds.get_height(); y++ ) {
for ( int x = 0; x < f.ds.get_width(); /* let charwidth handle advance */ ) { 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 ]; char curmove[ 32 ];
snprintf( curmove, 32, "\033[%d;%dH", y + 1, x + 1 ); snprintf( curmove, 32, "\033[%d;%dH", y + 1, x + 1 );
screen.append( curmove ); screen.append( curmove );
cursor_was_moved = true;
} }
cursor_x = x; cursor_x = x;
@@ -118,13 +114,14 @@ std::string Display::new_frame( Framebuffer &f )
/* has cursor location changed? */ /* has cursor location changed? */
if ( (!initialized) if ( (!initialized)
|| (f.ds.get_cursor_row() != last_frame.ds.get_cursor_row()) || (f.ds.get_cursor_row() != cursor_y)
|| (f.ds.get_cursor_col() != last_frame.ds.get_cursor_col()) || (f.ds.get_cursor_col() != cursor_x) ) {
|| cursor_was_moved ) {
char curmove[ 32 ]; char curmove[ 32 ];
snprintf( curmove, 32, "\033[%d;%dH", f.ds.get_cursor_row() + 1, snprintf( curmove, 32, "\033[%d;%dH", f.ds.get_cursor_row() + 1,
f.ds.get_cursor_col() + 1 ); f.ds.get_cursor_col() + 1 );
screen.append( curmove ); screen.append( curmove );
cursor_x = f.ds.get_cursor_col();
cursor_y = f.ds.get_cursor_row();
} }
/* has cursor visibility changed? */ /* has cursor visibility changed? */