Skip to content

Commit 022f7b5

Browse files
committed
Use same saved cursor logic in height resize as width
See zellij-org#2182 for original introduction that only added it in one branch, this fixes an issue where the saved cursor was incorrectly reset when the real cursor was
1 parent a2fb4a8 commit 022f7b5

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

zellij-server/src/panes/grid.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ impl Grid {
923923
self.saved_cursor_position.as_ref().map(|saved_cursor| saved_cursor.y);
924924

925925
let new_cursor_x = self.cursor.x;
926-
let mut saved_cursor_x_coordinates =
926+
let saved_cursor_x_coordinates =
927927
self.saved_cursor_position.as_ref().map(|saved_cursor| saved_cursor.x);
928928

929929
let current_viewport_row_count = self.viewport.len();
@@ -947,15 +947,16 @@ impl Grid {
947947
let row_count_to_transfer = current_viewport_row_count - new_rows;
948948
if row_count_to_transfer > new_cursor_y {
949949
new_cursor_y = 0;
950-
if let Some(saved_cursor_y_coordinates) = saved_cursor_y_coordinates.as_mut() {
951-
*saved_cursor_y_coordinates = 0
952-
};
953950
} else {
954951
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
957-
.saturating_sub(row_count_to_transfer);
958-
};
952+
}
953+
if let Some(saved_cursor_y_coordinates) = saved_cursor_y_coordinates.as_mut() {
954+
if row_count_to_transfer > *saved_cursor_y_coordinates {
955+
println!("resetting saved y");
956+
*saved_cursor_y_coordinates = 0;
957+
} else {
958+
*saved_cursor_y_coordinates -= row_count_to_transfer;
959+
}
959960
}
960961
transfer_rows_from_viewport_to_lines_above(
961962
&mut self.viewport,

zellij-server/src/panes/unit/grid_tests.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,48 @@ pub fn scroll_up_increase_width_and_scroll_down() {
24002400
assert_snapshot!(format!("{:?}", grid));
24012401
}
24022402

2403+
#[test]
2404+
fn saved_cursor_across_resize() {
2405+
let mut vte_parser = vte::Parser::new();
2406+
let sixel_image_store = Rc::new(RefCell::new(SixelImageStore::default()));
2407+
let terminal_emulator_color_codes = Rc::new(RefCell::new(HashMap::new()));
2408+
let debug = false;
2409+
let arrow_fonts = true;
2410+
let styled_underlines = true;
2411+
let mut grid = Grid::new(
2412+
4,
2413+
20,
2414+
Rc::new(RefCell::new(Palette::default())),
2415+
terminal_emulator_color_codes,
2416+
Rc::new(RefCell::new(LinkHandler::new())),
2417+
Rc::new(RefCell::new(None)),
2418+
sixel_image_store,
2419+
Style::default(),
2420+
debug,
2421+
arrow_fonts,
2422+
styled_underlines,
2423+
);
2424+
let mut parse = |s, grid: &mut Grid|
2425+
for b in Vec::from(s) { vte_parser.advance(&mut *grid, b) };
2426+
let content = "\n
2427+
\rLine 1 >fill to 20_<
2428+
\rLine 2 >fill to 20_<
2429+
\rLine 3 >fill to 20_<
2430+
\rL\u{1b}[sine 4 >fill to 20_<";
2431+
parse(content, &mut grid);
2432+
// Move real cursor position up three lines
2433+
let content = "\u{1b}[3A";
2434+
parse(content, &mut grid);
2435+
// Truncate top of terminal, resetting cursor (but not saved cursor)
2436+
grid.change_size(3, 20);
2437+
// Wrap, resetting cursor again (but not saved cursor)
2438+
grid.change_size(3, 10);
2439+
// Restore saved cursor position and write ZZZ
2440+
let content = "\u{1b}[uZZZ";
2441+
parse(content, &mut grid);
2442+
assert_snapshot!(format!("{:?}", grid));
2443+
}
2444+
24032445
#[test]
24042446
pub fn move_cursor_below_scroll_region() {
24052447
let mut vte_parser = vte::Parser::new();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: zellij-server/src/panes/./unit/grid_tests.rs
3+
assertion_line: 2443
4+
expression: "format!(\"{:?}\", grid)"
5+
---
6+
00 (W): ll to 20_<
7+
01 (C): LZZZ 4 >fi
8+
02 (W): ll to 20_<
9+

0 commit comments

Comments
 (0)