From 1dacc99183373bffa3ba287aa3241f3b1da67016 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sat, 30 May 2020 20:45:44 +0000 Subject: Refactor Term/Grid separation This commit aims to clear up the separation between Term and Grid to make way for implementing search. The `cursor` and `cursor_save` have been moved to the grid, since they're always bound to their specific grid and this makes updating easier. Since the selection is independent of the active grid, it has been moved to the `Term`. --- alacritty_terminal/src/index.rs | 135 +++++++++++++++++++++++++++++++++++----- 1 file changed, 118 insertions(+), 17 deletions(-) (limited to 'alacritty_terminal/src/index.rs') diff --git a/alacritty_terminal/src/index.rs b/alacritty_terminal/src/index.rs index 72e7158a..a5f2bc76 100644 --- a/alacritty_terminal/src/index.rs +++ b/alacritty_terminal/src/index.rs @@ -53,27 +53,28 @@ impl Point { #[inline] #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn sub(mut self, num_cols: usize, rhs: usize) -> Point + pub fn sub(mut self, num_cols: Column, rhs: usize) -> Point where L: Copy + Default + Into + Add + Sub, { - let line_changes = - (rhs.saturating_sub(self.col.0) as f32 / num_cols as f32).ceil() as usize; - if self.line.into() > Line(line_changes) { + let num_cols = num_cols.0; + let line_changes = (rhs + num_cols - 1).saturating_sub(self.col.0) / num_cols; + if self.line.into() >= Line(line_changes) { self.line = self.line - line_changes; + self.col = Column((num_cols + self.col.0 - rhs % num_cols) % num_cols); + self } else { - self.line = Default::default(); + Point::new(L::default(), Column(0)) } - self.col = Column((num_cols + self.col.0 - rhs % num_cols) % num_cols); - self } #[inline] #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn add(mut self, num_cols: usize, rhs: usize) -> Point + pub fn add(mut self, num_cols: Column, rhs: usize) -> Point where L: Copy + Default + Into + Add + Sub, { + let num_cols = num_cols.0; self.line = self.line + (rhs + self.col.0) / num_cols; self.col = Column((self.col.0 + rhs) % num_cols); self @@ -81,30 +82,30 @@ impl Point { #[inline] #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn sub_absolute(mut self, num_cols: usize, rhs: usize) -> Point + pub fn sub_absolute(mut self, num_cols: Column, rhs: usize) -> Point where L: Copy + Default + Into + Add + Sub, { - self.line = - self.line + (rhs.saturating_sub(self.col.0) as f32 / num_cols as f32).ceil() as usize; + let num_cols = num_cols.0; + self.line = self.line + ((rhs + num_cols - 1).saturating_sub(self.col.0) / num_cols); self.col = Column((num_cols + self.col.0 - rhs % num_cols) % num_cols); self } #[inline] #[must_use = "this returns the result of the operation, without modifying the original"] - pub fn add_absolute(mut self, num_cols: usize, rhs: usize) -> Point + pub fn add_absolute(mut self, num_cols: Column, rhs: usize) -> Point where L: Copy + Default + Into + Add + Sub, { - let line_changes = (rhs + self.col.0) / num_cols; - if self.line.into() > Line(line_changes) { + let line_changes = (rhs + self.col.0) / num_cols.0; + if self.line.into() >= Line(line_changes) { self.line = self.line - line_changes; + self.col = Column((self.col.0 + rhs) % num_cols.0); + self } else { - self.line = Default::default(); + Point::new(L::default(), num_cols - 1) } - self.col = Column((self.col.0 + rhs) % num_cols); - self } } @@ -453,4 +454,104 @@ mod tests { assert!(Point::new(Line(1), Column(1)) > Point::new(Line(0), Column(1))); assert!(Point::new(Line(1), Column(1)) > Point::new(Line(1), Column(0))); } + + #[test] + fn sub() { + let num_cols = Column(42); + let point = Point::new(0, Column(13)); + + let result = point.sub(num_cols, 1); + + assert_eq!(result, Point::new(0, point.col - 1)); + } + + #[test] + fn sub_wrap() { + let num_cols = Column(42); + let point = Point::new(1, Column(0)); + + let result = point.sub(num_cols, 1); + + assert_eq!(result, Point::new(0, num_cols - 1)); + } + + #[test] + fn sub_clamp() { + let num_cols = Column(42); + let point = Point::new(0, Column(0)); + + let result = point.sub(num_cols, 1); + + assert_eq!(result, point); + } + + #[test] + fn add() { + let num_cols = Column(42); + let point = Point::new(0, Column(13)); + + let result = point.add(num_cols, 1); + + assert_eq!(result, Point::new(0, point.col + 1)); + } + + #[test] + fn add_wrap() { + let num_cols = Column(42); + let point = Point::new(0, num_cols - 1); + + let result = point.add(num_cols, 1); + + assert_eq!(result, Point::new(1, Column(0))); + } + + #[test] + fn add_absolute() { + let num_cols = Column(42); + let point = Point::new(0, Column(13)); + + let result = point.add_absolute(num_cols, 1); + + assert_eq!(result, Point::new(0, point.col + 1)); + } + + #[test] + fn add_absolute_wrap() { + let num_cols = Column(42); + let point = Point::new(1, num_cols - 1); + + let result = point.add_absolute(num_cols, 1); + + assert_eq!(result, Point::new(0, Column(0))); + } + + #[test] + fn add_absolute_clamp() { + let num_cols = Column(42); + let point = Point::new(0, num_cols - 1); + + let result = point.add_absolute(num_cols, 1); + + assert_eq!(result, point); + } + + #[test] + fn sub_absolute() { + let num_cols = Column(42); + let point = Point::new(0, Column(13)); + + let result = point.sub_absolute(num_cols, 1); + + assert_eq!(result, Point::new(0, point.col - 1)); + } + + #[test] + fn sub_absolute_wrap() { + let num_cols = Column(42); + let point = Point::new(0, Column(0)); + + let result = point.sub_absolute(num_cols, 1); + + assert_eq!(result, Point::new(1, num_cols - 1)); + } } -- cgit v1.2.3-54-g00ecf