@@ -800,11 +800,12 @@ def _reflow_primary(self, *, rows: int, cols: int) -> None:
800800
801801 combined = hist_thawed + self ._rows [: last_active + 1 ]
802802 cur_abs_idx = hist_len + self ._cur_row
803- cur_col = self ._cur_col
803+ eff_cur_col = self ._cur_col + ( 1 if self . _pending_wrap else 0 )
804804
805805 new_all : list [_Row ] = []
806806 new_cur_abs = 0
807807 new_cur_col = 0
808+ new_pending_wrap = False
808809 cursor_placed = False
809810
810811 idx = 0
@@ -816,83 +817,103 @@ def _reflow_primary(self, *, rows: int, cols: int) -> None:
816817 group_end = idx
817818 idx += 1
818819
819- # Extract logical cells across [group_start .. group_end].
820- cells : list [tuple [str , int , int , _CellExt | None , bool , bool ]] = []
820+ if group_start <= cur_abs_idx < group_end and eff_cur_col >= len (
821+ combined [cur_abs_idx ].chars
822+ ):
823+ cur_abs_idx += 1
824+ eff_cur_col = 0
825+
826+ # Extract real logical cells across [group_start .. group_end].
827+ cells : list [tuple [str , int , int , _CellExt | None , int ]] = []
828+ cur_after_extra : int | None = None
829+ carry_cur = False
821830 for r_i in range (group_start , group_end + 1 ):
822831 src = combined [r_i ]
823832 is_last = r_i == group_end
824833 src_t_used = src .text_used if isinstance (src , _Row ) else len (src .text )
825834 row_limit = len (src .chars ) if not is_last else src_t_used
826- limit = max (row_limit , cur_col + 1 ) if r_i == cur_abs_idx else row_limit
827- for c_i in range (limit ):
835+ if r_i == cur_abs_idx and eff_cur_col >= row_limit :
836+ cur_after_extra = eff_cur_col - row_limit
837+ for c_i in range (row_limit ):
828838 word = src .attr_at (c_i )
829839 rb = raw_width_bits (word )
830- is_cur = r_i == cur_abs_idx and c_i == cur_col
831- is_dummy = is_last and c_i >= row_limit
840+ is_cur = r_i == cur_abs_idx and c_i == eff_cur_col
832841 if rb == int (WidthRole .SPACER_HEAD ):
833842 if is_cur :
834- cells . append (( " " , 0 , 1 , None , True , True ))
843+ carry_cur = True
835844 continue
836845 if rb == int (WidthRole .SPACER_TAIL ):
837846 if is_cur and cells :
838- p_ch , p_w , p_cw , p_ext , _ , p_dum = cells [- 1 ]
839- cells [- 1 ] = (p_ch , p_w , p_cw , p_ext , True , p_dum )
847+ p_ch , p_w , p_cw , p_ext , _ = cells [- 1 ]
848+ cells [- 1 ] = (p_ch , p_w , p_cw , p_ext , 1 )
840849 continue
841850 cw = 2 if rb == int (WidthRole .WIDE ) else 1
842851 base_word = word & _WIDTH_CLEAR_MASK
852+ cur_off = 0 if (is_cur or carry_cur ) else - 1
853+ carry_cur = False
843854 cells .append (
844855 (
845856 src .chars [c_i ],
846857 base_word ,
847858 cw ,
848859 src .ext_at (c_i ),
849- is_cur ,
850- is_dummy ,
860+ cur_off ,
851861 )
852862 )
853863
854864 if not cells :
855865 empty_row = _Row (cols )
866+ new_all .append (empty_row )
856867 if group_start <= cur_abs_idx <= group_end and not cursor_placed :
857- new_cur_abs = len (new_all )
858- new_cur_col = 0
868+ extra = 0 if cur_after_extra is None else cur_after_extra
869+ new_cur_abs = len (new_all ) - 1
870+ new_cur_col = min (cols - 1 , extra )
871+ new_pending_wrap = False
859872 cursor_placed = True
860- new_all .append (empty_row )
861873 continue
862874
863875 cur_out = _Row (cols )
864876 new_all .append (cur_out )
865877 out_col = 0
866- for ch , base_w , cw , ce , is_cur , is_dummy in cells :
878+ for ch , base_w , cw , ce , cur_off in cells :
867879 eff_cw = cw
868880 eff_ch = ch
869881 if eff_cw == 2 and cols == 1 :
870882 eff_ch = "\ufffd "
871883 eff_cw = 1
872884
873885 if out_col + eff_cw > cols :
874- if not is_dummy and out_col < cols :
886+ if out_col < cols :
875887 cur_out .write_spacer_head (out_col , 0 , None )
876- if not is_dummy :
877- cur_out .wrapped = True
888+ cur_out .wrapped = True
878889 cur_out = _Row (cols )
879890 new_all .append (cur_out )
880891 out_col = 0
881892
882- if is_cur and not cursor_placed :
893+ if cur_off >= 0 and not cursor_placed :
883894 new_cur_abs = len (new_all ) - 1
884- new_cur_col = out_col
895+ new_cur_col = min (cols - 1 , out_col + cur_off )
896+ new_pending_wrap = False
885897 cursor_placed = True
886898
887- if is_dummy :
888- out_col += eff_cw
889- elif eff_cw == 2 :
899+ if eff_cw == 2 :
890900 cur_out .write_wide (out_col , eff_ch , base_w , ce )
891901 out_col += 2
892902 else :
893903 cur_out .write_narrow (out_col , eff_ch , base_w , ce )
894904 out_col += 1
895905
906+ if (cur_after_extra is not None or carry_cur ) and not cursor_placed :
907+ extra = 0 if cur_after_extra is None else cur_after_extra
908+ new_cur_abs = len (new_all ) - 1
909+ if extra == 0 and out_col == cols :
910+ new_cur_col = cols - 1
911+ new_pending_wrap = True
912+ else :
913+ new_cur_col = min (cols - 1 , out_col + extra )
914+ new_pending_wrap = False
915+ cursor_placed = True
916+
896917 if not new_all :
897918 new_all .append (_Row (cols ))
898919
@@ -911,7 +932,7 @@ def _reflow_primary(self, *, rows: int, cols: int) -> None:
911932 self ._cols = cols
912933 self ._cur_row = max (0 , min (rows - 1 , new_cur_abs - hist_cut ))
913934 self ._cur_col = max (0 , min (cols - 1 , new_cur_col ))
914- self ._pending_wrap = False
935+ self ._pending_wrap = new_pending_wrap
915936
916937 def resize (self , * , rows : int , cols : int , reflow : bool = True ) -> None :
917938 """Resize screen to ``(rows, cols)``, optionally reflowing soft wraps."""
@@ -920,9 +941,13 @@ def resize(self, *, rows: int, cols: int, reflow: bool = True) -> None:
920941 raise ValueError (msg )
921942
922943 old_cols = self ._cols
944+ if rows == len (self ._rows ) and cols == old_cols :
945+ return
946+
923947 if reflow and cols != old_cols :
924948 self ._reflow_primary (rows = rows , cols = cols )
925949 else :
950+ old_cur_col = self ._cur_col
926951 if cols != old_cols :
927952 for r in self ._rows :
928953 r .resize (cols )
@@ -948,7 +973,7 @@ def resize(self, *, rows: int, cols: int, reflow: bool = True) -> None:
948973
949974 self ._cur_row = min (self ._cur_row , rows - 1 )
950975 self ._cur_col = min (self ._cur_col , cols - 1 )
951- self ._pending_wrap = False
976+ self ._pending_wrap = self . _pending_wrap and old_cur_col < cols
952977
953978 self .scroll_region = ScrollRegion (
954979 top = 0 ,
0 commit comments