Skip to content

Commit

Permalink
fix(compatibility): adjust saved cursor position on resize (#1362)
Browse files Browse the repository at this point in the history
* fix(compatibility): adjust saved cursor position on resize

* style(fmt): rustfmt
  • Loading branch information
imsnif committed Apr 29, 2022
1 parent 3478827 commit 58d52d4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions zellij-server/src/panes/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,12 @@ impl Grid {
}
self.cursor.y = new_cursor_y;
self.cursor.x = new_cursor_x;
self.saved_cursor_position
.as_mut()
.map(|saved_cursor_position| {
saved_cursor_position.y = new_cursor_y;
saved_cursor_position.x = new_cursor_x;
});
} else if new_columns != self.width
&& self.alternate_lines_above_viewport_and_cursor.is_some()
{
Expand All @@ -723,13 +729,24 @@ impl Grid {
);
let rows_pulled = self.viewport.len() - current_viewport_row_count;
self.cursor.y += rows_pulled;
self.saved_cursor_position
.as_mut()
.map(|saved_cursor_position| saved_cursor_position.y += rows_pulled);
}
Ordering::Greater => {
let row_count_to_transfer = current_viewport_row_count - new_rows;
if row_count_to_transfer > self.cursor.y {
self.cursor.y = 0;
self.saved_cursor_position
.as_mut()
.map(|saved_cursor_position| saved_cursor_position.y = 0);
} else {
self.cursor.y -= row_count_to_transfer;
self.saved_cursor_position
.as_mut()
.map(|saved_cursor_position| {
saved_cursor_position.y -= row_count_to_transfer
});
}
if self.alternate_lines_above_viewport_and_cursor.is_none() {
transfer_rows_from_viewport_to_lines_above(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: zellij-server/src/tab/./unit/tab_integration_tests.rs
assertion_line: 1153
expression: snapshot
---
00 (C): ┌ Pane #1 ─────────────────────────────────────────────────────────────────────────── SCROLL: 0/4
01 (C): │ Let's save the cursor position here this overwrote me!tten
02 (C): └──────────────────────────────────────────────────────────────────────────────────────────────────┘
03 (C):
04 (C):

26 changes: 26 additions & 0 deletions zellij-server/src/tab/unit/tab_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,29 @@ fn wide_characters_in_left_title_side() {
);
assert_snapshot!(snapshot);
}

#[test]
fn save_cursor_position_across_resizes() {
// the save cursor position ANSI instruction (CSI s) needs to point to the same character after we
// resize the pane
let size = Size { cols: 100, rows: 5 };
let client_id = 1;
let mut tab = create_new_tab(size);
let mut output = Output::default();

tab.handle_pty_bytes(
1,
Vec::from("\n\nI am some text\nI am another line of text\nLet's save the cursor position here \u{1b}[sI should be ovewritten".as_bytes()),
);
tab.resize_whole_tab(Size { cols: 100, rows: 3 });
tab.handle_pty_bytes(1, Vec::from("\u{1b}[uthis overwrote me!".as_bytes()));

tab.render(&mut output, None);
let snapshot = take_snapshot(
output.serialize().get(&client_id).unwrap(),
size.rows,
size.cols,
Palette::default(),
);
assert_snapshot!(snapshot);
}

0 comments on commit 58d52d4

Please sign in to comment.