From e6a7ed31f67a9c2170b5f4a0a37ae6089b0daa4b Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Thu, 14 Jan 2021 12:48:36 +0100 Subject: [PATCH] fix(compatibility): clear characters should preserve current styling (#143) * fix(compatibility): clear characters should preserve current styling * style(fmt): rustfmt --- src/panes/grid.rs | 29 ++++++++++++++++++++--------- src/panes/terminal_pane.rs | 7 ++++--- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/panes/grid.rs b/src/panes/grid.rs index e00b6c73..3d0f220b 100644 --- a/src/panes/grid.rs +++ b/src/panes/grid.rs @@ -521,19 +521,27 @@ impl Grid { let row = self.viewport.get_mut(self.cursor.y).unwrap(); row.replace_beginning_with(line_part); } - pub fn clear_all_after_cursor(&mut self) { - self.viewport - .get_mut(self.cursor.y) - .unwrap() - .truncate(self.cursor.x); - self.viewport.truncate(self.cursor.y + 1); + pub fn clear_all_after_cursor(&mut self, replace_with: TerminalCharacter) { + let cursor_row = self.viewport.get_mut(self.cursor.y).unwrap(); + cursor_row.truncate(self.cursor.x); + let mut replace_with_columns_in_cursor_row = vec![replace_with; self.width - self.cursor.x]; + cursor_row.append(&mut replace_with_columns_in_cursor_row); + + let replace_with_columns = vec![replace_with; self.width]; + self.replace_characters_in_line_after_cursor(replace_with); + for row in self.viewport.iter_mut().skip(self.cursor.y + 1) { + row.replace_columns(replace_with_columns.clone()); + } } pub fn clear_cursor_line(&mut self) { self.viewport.get_mut(self.cursor.y).unwrap().truncate(0); } - pub fn clear_all(&mut self) { - self.viewport.clear(); - self.viewport.push(Row::new().canonical()); + pub fn clear_all(&mut self, replace_with: TerminalCharacter) { + let replace_with_columns = vec![replace_with; self.width]; + self.replace_characters_in_line_after_cursor(replace_with); + for row in self.viewport.iter_mut() { + row.replace_columns(replace_with_columns.clone()); + } } fn pad_current_line_until(&mut self, position: usize) { let current_row = self.viewport.get_mut(self.cursor.y).unwrap(); @@ -746,6 +754,9 @@ impl Row { self.columns.push(terminal_character); self.columns.swap_remove(x); } + pub fn replace_columns(&mut self, columns: Vec) { + self.columns = columns; + } pub fn push(&mut self, terminal_character: TerminalCharacter) { self.columns.push(terminal_character); } diff --git a/src/panes/terminal_pane.rs b/src/panes/terminal_pane.rs index b9369fea..3ad60050 100644 --- a/src/panes/terminal_pane.rs +++ b/src/panes/terminal_pane.rs @@ -443,10 +443,12 @@ impl vte::Perform for TerminalPane { } } else if c == 'J' { // clear all (0 => below, 1 => above, 2 => all, 3 => saved) + let mut char_to_replace = EMPTY_TERMINAL_CHARACTER; + char_to_replace.styles = self.pending_styles; if params[0] == 0 { - self.grid.clear_all_after_cursor(); + self.grid.clear_all_after_cursor(char_to_replace); } else if params[0] == 2 { - self.grid.clear_all(); + self.grid.clear_all(char_to_replace); } // TODO: implement 1 } else if c == 'H' { @@ -492,7 +494,6 @@ impl vte::Perform for TerminalPane { Some(&1049) => { if let Some(alternative_grid) = self.alternative_grid.as_mut() { std::mem::swap(&mut self.grid, alternative_grid); - // self.grid = alternative_grid; } self.alternative_grid = None; }