aboutsummaryrefslogtreecommitdiff
path: root/alacritty_terminal/src/vi_mode.rs
diff options
context:
space:
mode:
Diffstat (limited to 'alacritty_terminal/src/vi_mode.rs')
-rw-r--r--alacritty_terminal/src/vi_mode.rs86
1 files changed, 73 insertions, 13 deletions
diff --git a/alacritty_terminal/src/vi_mode.rs b/alacritty_terminal/src/vi_mode.rs
index 6f370642..5bf5eaed 100644
--- a/alacritty_terminal/src/vi_mode.rs
+++ b/alacritty_terminal/src/vi_mode.rs
@@ -1,4 +1,4 @@
-use std::cmp::{max, min};
+use std::cmp::min;
use alacritty_config_derive::ConfigDeserialize;
@@ -160,21 +160,11 @@ impl ViModeCursor {
/// Get target cursor point for vim-like page movement.
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn scroll<T: EventListener>(mut self, term: &Term<T>, lines: i32) -> Self {
- // Check number of lines the cursor needs to be moved.
- let overscroll = if lines > 0 {
- let max_scroll = term.history_size() - term.grid().display_offset();
- max(0, lines - max_scroll as i32)
- } else {
- let max_scroll = term.grid().display_offset();
- min(0, lines + max_scroll as i32)
- };
-
// Clamp movement to within visible region.
- let line = (self.point.line - overscroll).grid_clamp(term, Boundary::Grid);
+ let line = (self.point.line - lines).grid_clamp(term, Boundary::Grid);
// Find the first occupied cell after scrolling has been performed.
- let target_line = (self.point.line - lines).grid_clamp(term, Boundary::Grid);
- let column = first_occupied_in_line(term, target_line).unwrap_or_default().column;
+ let column = first_occupied_in_line(term, line).unwrap_or_default().column;
// Move cursor.
self.point = Point::new(line, column);
@@ -388,6 +378,7 @@ fn is_boundary<T>(term: &Term<T>, point: Point, direction: Direction) -> bool {
mod tests {
use super::*;
+ use crate::ansi::Handler;
use crate::config::MockConfig;
use crate::index::{Column, Line};
use crate::term::{SizeInfo, Term};
@@ -756,4 +747,73 @@ mod tests {
cursor = cursor.motion(&mut term, ViMotion::WordLeft);
assert_eq!(cursor.point, Point::new(Line(0), Column(0)));
}
+
+ #[test]
+ fn scroll_simple() {
+ let mut term = term();
+
+ // Create 1 line of scrollback.
+ for _ in 0..20 {
+ term.newline();
+ }
+
+ let mut cursor = ViModeCursor::new(Point::new(Line(0), Column(0)));
+
+ cursor = cursor.scroll(&term, -1);
+ assert_eq!(cursor.point, Point::new(Line(1), Column(0)));
+
+ cursor = cursor.scroll(&term, 1);
+ assert_eq!(cursor.point, Point::new(Line(0), Column(0)));
+
+ cursor = cursor.scroll(&term, 1);
+ assert_eq!(cursor.point, Point::new(Line(-1), Column(0)));
+ }
+
+ #[test]
+ fn scroll_over_top() {
+ let mut term = term();
+
+ // Create 40 lines of scrollback.
+ for _ in 0..59 {
+ term.newline();
+ }
+
+ let mut cursor = ViModeCursor::new(Point::new(Line(19), Column(0)));
+
+ cursor = cursor.scroll(&term, 20);
+ assert_eq!(cursor.point, Point::new(Line(-1), Column(0)));
+
+ cursor = cursor.scroll(&term, 20);
+ assert_eq!(cursor.point, Point::new(Line(-21), Column(0)));
+
+ cursor = cursor.scroll(&term, 20);
+ assert_eq!(cursor.point, Point::new(Line(-40), Column(0)));
+
+ cursor = cursor.scroll(&term, 20);
+ assert_eq!(cursor.point, Point::new(Line(-40), Column(0)));
+ }
+
+ #[test]
+ fn scroll_over_bottom() {
+ let mut term = term();
+
+ // Create 40 lines of scrollback.
+ for _ in 0..59 {
+ term.newline();
+ }
+
+ let mut cursor = ViModeCursor::new(Point::new(Line(-40), Column(0)));
+
+ cursor = cursor.scroll(&term, -20);
+ assert_eq!(cursor.point, Point::new(Line(-20), Column(0)));
+
+ cursor = cursor.scroll(&term, -20);
+ assert_eq!(cursor.point, Point::new(Line(0), Column(0)));
+
+ cursor = cursor.scroll(&term, -20);
+ assert_eq!(cursor.point, Point::new(Line(19), Column(0)));
+
+ cursor = cursor.scroll(&term, -20);
+ assert_eq!(cursor.point, Point::new(Line(19), Column(0)));
+ }
}