summaryrefslogtreecommitdiff
path: root/alacritty_terminal/src/grid
diff options
context:
space:
mode:
Diffstat (limited to 'alacritty_terminal/src/grid')
-rw-r--r--alacritty_terminal/src/grid/mod.rs36
-rw-r--r--alacritty_terminal/src/grid/tests.rs34
2 files changed, 45 insertions, 25 deletions
diff --git a/alacritty_terminal/src/grid/mod.rs b/alacritty_terminal/src/grid/mod.rs
index febdff69..34d989db 100644
--- a/alacritty_terminal/src/grid/mod.rs
+++ b/alacritty_terminal/src/grid/mod.rs
@@ -145,24 +145,21 @@ impl<T: GridCell + PartialEq + Copy> Grid<T> {
Grid { raw, cols, lines, display_offset: 0, selection: None, max_scroll_limit: scrollback }
}
- pub fn buffer_to_visible(&self, point: impl Into<Point<usize>>) -> Option<Point<usize>> {
- let mut point = point.into();
-
- if point.line < self.display_offset || point.line >= self.display_offset + self.lines.0 {
- return None;
+ /// Clamp a buffer point to the visible region.
+ pub fn clamp_buffer_to_visible(&self, point: Point<usize>) -> Point {
+ if point.line < self.display_offset {
+ Point::new(self.lines - 1, self.cols - 1)
+ } else if point.line >= self.display_offset + self.lines.0 {
+ Point::new(Line(0), Column(0))
+ } else {
+ // Since edgecases are handled, conversion is identical as visible to buffer
+ self.visible_to_buffer(point.into()).into()
}
-
- point.line = self.lines.0 + self.display_offset - point.line - 1;
-
- Some(point)
}
+ /// Convert viewport relative point to global buffer indexing.
pub fn visible_to_buffer(&self, point: Point) -> Point<usize> {
- Point { line: self.visible_line_to_buffer(point.line), col: point.col }
- }
-
- fn visible_line_to_buffer(&self, line: Line) -> usize {
- self.line_to_offset(line) + self.display_offset
+ Point { line: self.lines.0 + self.display_offset - point.line.0 - 1, col: point.col }
}
/// Update the size of the scrollback history
@@ -453,17 +450,6 @@ impl<T: GridCell + PartialEq + Copy> Grid<T> {
self.lines = target;
}
- /// Convert a Line index (active region) to a buffer offset
- ///
- /// # Panics
- ///
- /// This method will panic if `Line` is larger than the grid dimensions
- pub fn line_to_offset(&self, line: Line) -> usize {
- assert!(line < self.num_lines());
-
- *(self.num_lines() - line - 1)
- }
-
#[inline]
pub fn scroll_down(&mut self, region: &Range<Line>, positions: Line, template: &T) {
let num_lines = self.num_lines().0;
diff --git a/alacritty_terminal/src/grid/tests.rs b/alacritty_terminal/src/grid/tests.rs
index e4fdad5c..e8f4fb8d 100644
--- a/alacritty_terminal/src/grid/tests.rs
+++ b/alacritty_terminal/src/grid/tests.rs
@@ -37,6 +37,40 @@ impl GridCell for usize {
}
}
+#[test]
+fn grid_clamp_buffer_point() {
+ let mut grid = Grid::new(Line(10), Column(10), 1_000, 0);
+ grid.display_offset = 5;
+
+ let point = grid.clamp_buffer_to_visible(Point::new(10, Column(3)));
+ assert_eq!(point, Point::new(Line(4), Column(3)));
+
+ let point = grid.clamp_buffer_to_visible(Point::new(15, Column(3)));
+ assert_eq!(point, Point::new(Line(0), Column(0)));
+
+ let point = grid.clamp_buffer_to_visible(Point::new(4, Column(3)));
+ assert_eq!(point, Point::new(Line(9), Column(9)));
+
+ grid.display_offset = 0;
+
+ let point = grid.clamp_buffer_to_visible(Point::new(4, Column(3)));
+ assert_eq!(point, Point::new(Line(5), Column(3)));
+}
+
+#[test]
+fn visible_to_buffer() {
+ let mut grid = Grid::new(Line(10), Column(10), 1_000, 0);
+ grid.display_offset = 5;
+
+ let point = grid.visible_to_buffer(Point::new(Line(4), Column(3)));
+ assert_eq!(point, Point::new(10, Column(3)));
+
+ grid.display_offset = 0;
+
+ let point = grid.visible_to_buffer(Point::new(Line(5), Column(3)));
+ assert_eq!(point, Point::new(4, Column(3)));
+}
+
// Scroll up moves lines upwards
#[test]
fn scroll_up() {