@@ -918,6 +918,14 @@ impl Grid {
918918 } ;
919919 }
920920 if new_rows != self . height {
921+ let mut new_cursor_y = self . cursor . y ;
922+ let mut saved_cursor_y_coordinates =
923+ self . saved_cursor_position . as_ref ( ) . map ( |saved_cursor| saved_cursor. y ) ;
924+
925+ let new_cursor_x = self . cursor . x ;
926+ let mut saved_cursor_x_coordinates =
927+ self . saved_cursor_position . as_ref ( ) . map ( |saved_cursor| saved_cursor. x ) ;
928+
921929 let current_viewport_row_count = self . viewport . len ( ) ;
922930 match current_viewport_row_count. cmp ( & new_rows) {
923931 Ordering :: Less => {
@@ -930,23 +938,22 @@ impl Grid {
930938 new_columns,
931939 ) ;
932940 let rows_pulled = self . viewport . len ( ) - current_viewport_row_count;
933- self . cursor . y += rows_pulled;
934- if let Some ( saved_cursor_position ) = self . saved_cursor_position . as_mut ( ) {
935- saved_cursor_position . y += rows_pulled
941+ new_cursor_y += rows_pulled;
942+ if let Some ( saved_cursor_y_coordinates ) = saved_cursor_y_coordinates . as_mut ( ) {
943+ * saved_cursor_y_coordinates += rows_pulled;
936944 } ;
937945 } ,
938946 Ordering :: Greater => {
939947 let row_count_to_transfer = current_viewport_row_count - new_rows;
940- if row_count_to_transfer > self . cursor . y {
941- self . cursor . y = 0 ;
942- if let Some ( saved_cursor_position ) = self . saved_cursor_position . as_mut ( ) {
943- saved_cursor_position . y = 0
948+ if row_count_to_transfer > new_cursor_y {
949+ new_cursor_y = 0 ;
950+ if let Some ( saved_cursor_y_coordinates ) = saved_cursor_y_coordinates . as_mut ( ) {
951+ * saved_cursor_y_coordinates = 0
944952 } ;
945953 } else {
946- self . cursor . y -= row_count_to_transfer;
947- if let Some ( saved_cursor_position) = self . saved_cursor_position . as_mut ( ) {
948- saved_cursor_position. y = saved_cursor_position
949- . y
954+ new_cursor_y -= row_count_to_transfer;
955+ if let Some ( saved_cursor_y_coordinates) = saved_cursor_y_coordinates. as_mut ( ) {
956+ * saved_cursor_y_coordinates = saved_cursor_y_coordinates
950957 . saturating_sub ( row_count_to_transfer) ;
951958 } ;
952959 }
@@ -960,6 +967,19 @@ impl Grid {
960967 } ,
961968 Ordering :: Equal => { } ,
962969 }
970+ self . cursor . y = new_cursor_y;
971+ self . cursor . x = new_cursor_x;
972+ if let Some ( saved_cursor_position) = self . saved_cursor_position . as_mut ( ) {
973+ match ( saved_cursor_x_coordinates, saved_cursor_y_coordinates) {
974+ ( Some ( saved_cursor_x_coordinates) , Some ( saved_cursor_y_coordinates) ) => {
975+ saved_cursor_position. x = saved_cursor_x_coordinates;
976+ saved_cursor_position. y = saved_cursor_y_coordinates;
977+ } ,
978+ _ => unreachable ! ( "saved cursor {:?} {:?}" ,
979+ saved_cursor_x_coordinates,
980+ saved_cursor_y_coordinates) ,
981+ }
982+ } ;
963983 }
964984 self . height = new_rows;
965985 self . width = new_columns;
0 commit comments