@@ -223,7 +223,7 @@ def change_encoding(self, encoding: str) -> None:
223223 """
224224 self .encoding = encoding
225225
226- def refresh (self , screen , c_xy ):
226+ def refresh (self , screen , c_xy , clear_to_end = False ):
227227 """
228228 Refresh the console screen.
229229
@@ -271,8 +271,9 @@ def refresh(self, screen, c_xy):
271271 self .posxy = 0 , old_offset
272272 for i in range (old_offset - offset ):
273273 self .__write_code (self ._ri )
274- oldscr .pop (- 1 )
275274 oldscr .insert (0 , "" )
275+ if len (oldscr ) > height :
276+ oldscr .pop (- 1 )
276277 elif old_offset < offset and self ._ind :
277278 self .__hide_cursor ()
278279 self .__write_code (self ._cup , self .height - 1 , 0 )
@@ -300,6 +301,12 @@ def refresh(self, screen, c_xy):
300301 self .__write_code (self ._el )
301302 y += 1
302303
304+ if clear_to_end :
305+ self .__move (wlen (newscr [- 1 ]), len (newscr ) - 1 + self .__offset )
306+ self .posxy = wlen (newscr [- 1 ]), len (newscr ) - 1 + self .__offset
307+ self .__write_code (b"\x1b [J" ) # clear to end of line
308+ self .flushoutput ()
309+
303310 self .__show_cursor ()
304311
305312 self .screen = screen .copy ()
@@ -321,6 +328,10 @@ def move_cursor(self, x, y):
321328 self .posxy = x , y
322329 self .flushoutput ()
323330
331+ def reset_cursor (self ) -> None :
332+ self .posxy = 0 , self .__offset
333+ self .__write_code (self ._cup , 0 , 0 )
334+
324335 def prepare (self ):
325336 """
326337 Prepare the console for input/output operations.
@@ -683,13 +694,18 @@ def __write_changed_line(self, y, oldline, newline, px_coord):
683694 self .__write (newline [x_pos ])
684695 self .posxy = character_width + 1 , y
685696
697+ if newline [- 1 ] != oldline [- 1 ]:
698+ self .__move (self .width , y )
699+ self .__write (newline [- 1 ])
700+ self .posxy = self .width - 1 , y
701+
686702 else :
687703 self .__hide_cursor ()
688704 self .__move (x_coord , y )
689705 if wlen (oldline ) > wlen (newline ):
690706 self .__write_code (self ._el )
691707 self .__write (newline [x_pos :])
692- self .posxy = wlen (newline ), y
708+ self .posxy = min ( wlen (newline ), self . width - 1 ), y
693709
694710 if "\x1b " in newline :
695711 # ANSI escape characters are present, so we can't assume
@@ -752,7 +768,6 @@ def __move_tall(self, x, y):
752768 self .__write_code (self ._cup , y - self .__offset , x )
753769
754770 def __sigwinch (self , signum , frame ):
755- self .height , self .width = self .getheightwidth ()
756771 self .event_queue .insert (Event ("resize" , None ))
757772
758773 def __hide_cursor (self ):
0 commit comments