@@ -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 )
@@ -321,78 +322,28 @@ def move_cursor(self, x, y):
321322 self .posxy = x , y
322323 self .flushoutput ()
323324
324- def sync_screen (self ):
325+ def sync_cursor (self ):
325326 """
326- Synchronize self.posxy, self.screen, self.width and self.height.
327- Assuming that the content of the screen doesn't change, only the width changes.
327+ Synchronize posxy after resizing.
328328 """
329- if not self .screen :
330- self .posxy = 0 , 0
331- return
329+ os .write (self .output_fd , b"\x1b [6n" )
330+ response = b""
331+ while True :
332+ ch = os .read (self .input_fd , 1 )
333+ response += ch
334+ if ch == b'R' :
335+ break
336+ m = re .match (rb"\x1b\[(\d+);(\d+)R" , response )
337+ if m :
338+ row , col = map (int , m .groups ())
339+ cur_x , cur_y = col - 1 , row - 1
340+ self .posxy = cur_x , cur_y + self .__offset
332341
333- px , py = self .posxy
334- old_height , old_width = self .height , self .width
335- new_height , new_width = self .getheightwidth ()
336-
337- vlines = []
338- x , y = 0 , 0
339- new_line = True
340- for i , line in enumerate (self .screen ):
341- l = wlen (line )
342- if i == py :
343- if new_line :
344- y = sum (wlen (g ) // new_width for g in vlines ) + len (vlines )
345- x = px
346- else :
347- y = sum (wlen (g ) // new_width for g in vlines [:- 1 ]) + len (vlines ) - 1
348- x = px + wlen (vlines [- 1 ])
349- if x >= new_width :
350- y += x // new_width
351- x %= new_width
352-
353- if new_line :
354- vlines .append (line )
355- new_line = False
356- else :
357- vlines [- 1 ] += line
358- if l != old_width :
359- new_line = True
360-
361- new_screen = []
362- for vline in vlines :
363- parts = []
364- last_end = 0
365- for match in ANSI_ESCAPE_SEQUENCE .finditer (vline ):
366- if match .start () > last_end :
367- parts .append ((vline [last_end :match .start ()], False ))
368- parts .append ((match .group (), True ))
369- last_end = match .end ()
370-
371- if last_end < len (vline ):
372- parts .append ((vline [last_end :], False ))
373-
374- result = ""
375- length = 0
376- for part , is_escape in parts :
377- if is_escape :
378- result += part
379- continue
380- for char in part :
381- lc = wlen (char )
382- if lc + length > new_width :
383- # save the current line
384- new_screen .append (result )
385- result = ""
386- length = 0
387- result += char
388- length += lc
389-
390- if result :
391- new_screen .append (result )
392-
393- self .posxy = x , y
394- self .screen = new_screen
395- self .height , self .width = new_height , new_width
342+ def sync_screen_size (self ):
343+ """
344+ Synchronize screen size after resizing.
345+ """
346+ self .height , self .width = self .getheightwidth ()
396347
397348 def prepare (self ):
398349 """
0 commit comments