From 6443a727eaa7f663655e4426749676488fdbc265 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Mon, 28 Mar 2022 12:39:54 +0200 Subject: [PATCH] fix(compatibility): ansi scroll down (#1279) * fix(compatibility): ansi scroll down * style(fmt): rustfmt --- src/tests/fixtures/scroll_down | 3 + zellij-server/src/panes/grid.rs | 33 +++++------ zellij-server/src/panes/unit/grid_tests.rs | 17 ++++++ ...s__grid__grid_tests__scroll_down_ansi.snap | 57 +++++++++++++++++++ 4 files changed, 94 insertions(+), 16 deletions(-) create mode 100755 src/tests/fixtures/scroll_down create mode 100644 zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__scroll_down_ansi.snap diff --git a/src/tests/fixtures/scroll_down b/src/tests/fixtures/scroll_down new file mode 100755 index 000000000..5a194bbe8 --- /dev/null +++ b/src/tests/fixtures/scroll_down @@ -0,0 +1,3 @@ + + +line1line2 diff --git a/zellij-server/src/panes/grid.rs b/zellij-server/src/panes/grid.rs index fe1a81ea0..fd06967ae 100644 --- a/zellij-server/src/panes/grid.rs +++ b/zellij-server/src/panes/grid.rs @@ -814,30 +814,31 @@ impl Grid { } } pub fn rotate_scroll_region_up(&mut self, count: usize) { - if let Some((scroll_region_top, scroll_region_bottom)) = self.scroll_region { + if let Some((scroll_region_top, scroll_region_bottom)) = self + .scroll_region + .or(Some((0, self.height.saturating_sub(1)))) + { for _ in 0..count { - let columns = VecDeque::from(vec![EMPTY_TERMINAL_CHARACTER; self.width]); - if scroll_region_bottom < self.viewport.len() { - self.viewport.remove(scroll_region_bottom); - } - if scroll_region_top < self.viewport.len() { - self.viewport - .insert(scroll_region_top, Row::from_columns(columns).canonical()); + if self.cursor.y >= scroll_region_top && self.cursor.y <= scroll_region_bottom { + self.pad_lines_until(scroll_region_bottom, EMPTY_TERMINAL_CHARACTER); + if self.viewport.get(scroll_region_bottom).is_some() { + self.viewport.remove(scroll_region_bottom); + } } } self.output_buffer.update_all_lines(); // TODO: only update scroll region lines } } pub fn rotate_scroll_region_down(&mut self, count: usize) { - if let Some((scroll_region_top, scroll_region_bottom)) = self.scroll_region { + if let Some((scroll_region_top, scroll_region_bottom)) = self + .scroll_region + .or(Some((0, self.height.saturating_sub(1)))) + { for _ in 0..count { - let columns = VecDeque::from(vec![EMPTY_TERMINAL_CHARACTER; self.width]); - self.viewport.remove(scroll_region_top); - if self.viewport.len() > scroll_region_top { - self.viewport - .insert(scroll_region_bottom, Row::from_columns(columns).canonical()); - } else { - self.viewport.push(Row::from_columns(columns).canonical()); + if self.cursor.y >= scroll_region_top && self.cursor.y <= scroll_region_bottom { + self.pad_lines_until(scroll_region_top, EMPTY_TERMINAL_CHARACTER); + self.viewport.remove(scroll_region_top); + self.pad_lines_until(scroll_region_bottom, EMPTY_TERMINAL_CHARACTER); } } self.output_buffer.update_all_lines(); // TODO: only update scroll region lines diff --git a/zellij-server/src/panes/unit/grid_tests.rs b/zellij-server/src/panes/unit/grid_tests.rs index e4b15503c..a03849a40 100644 --- a/zellij-server/src/panes/unit/grid_tests.rs +++ b/zellij-server/src/panes/unit/grid_tests.rs @@ -1602,3 +1602,20 @@ pub fn replace_non_wide_characters_with_wide_characters() { } assert_snapshot!(format!("{:?}", grid)); } + +#[test] +pub fn scroll_down_ansi() { + let mut vte_parser = vte::Parser::new(); + let mut grid = Grid::new( + 51, + 112, + Palette::default(), + Rc::new(RefCell::new(LinkHandler::new())), + ); + let fixture_name = "scroll_down"; + let content = read_fixture(fixture_name); + for byte in content { + vte_parser.advance(&mut grid, byte); + } + assert_snapshot!(format!("{:?}", grid)); +} diff --git a/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__scroll_down_ansi.snap b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__scroll_down_ansi.snap new file mode 100644 index 000000000..7a9418a37 --- /dev/null +++ b/zellij-server/src/panes/unit/snapshots/zellij_server__panes__grid__grid_tests__scroll_down_ansi.snap @@ -0,0 +1,57 @@ +--- +source: zellij-server/src/panes/./unit/grid_tests.rs +assertion_line: 1620 +expression: "format!(\"{:?}\", grid)" +--- +00 (C): +01 (C): line1 +02 (C): line2 +03 (C): +04 (C): +05 (C): +06 (C): +07 (C): +08 (C): +09 (C): +10 (C): +11 (C): +12 (C): +13 (C): +14 (C): +15 (C): +16 (C): +17 (C): +18 (C): +19 (C): +20 (C): +21 (C): +22 (C): +23 (C): +24 (C): +25 (C): +26 (C): +27 (C): +28 (C): +29 (C): +30 (C): +31 (C): +32 (C): +33 (C): +34 (C): +35 (C): +36 (C): +37 (C): +38 (C): +39 (C): +40 (C): +41 (C): +42 (C): +43 (C): +44 (C): +45 (C): +46 (C): +47 (C): +48 (C): +49 (C): +50 (C): +