From 54d50ed3be810861d3c2fa500c3fcc8e802198d9 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Fri, 16 Feb 2018 18:35:54 -0800 Subject: Fix scrolling backwards in tmux --- src/grid/mod.rs | 11 +++++++---- src/grid/storage.rs | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 1cb0876e..ca43471d 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -157,6 +157,10 @@ impl Grid { self.scroll_limit = min(self.scroll_limit + count, self.raw.len() - *self.lines); } + fn decrease_scroll_limit(&mut self, count: usize) { + self.scroll_limit = self.scroll_limit.saturating_sub(count); + } + /// Add lines to the visible area /// /// The behavior in Terminal.app and iTerm.app is to keep the cursor at the @@ -236,17 +240,16 @@ impl Grid { // active, the bottom lines are restored in the next step. self.raw.rotate_up(*positions); + self.decrease_scroll_limit(*positions); + // Now, restore any scroll region lines let lines = self.lines; for i in IndexRange(region.end .. lines) { - // First do the swap - // TODO there is a bug here causing a panic. - // TODO math self.raw.swap_lines(i, i + positions); } // Finally, reset recycled lines - for i in 0..*positions { + for i in IndexRange(Line(0)..positions) { self.raw[i].reset(&self.template_row); } } else { diff --git a/src/grid/storage.rs b/src/grid/storage.rs index 0ca2f525..1588b006 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -62,6 +62,10 @@ impl Storage { (requested + self.zero) % self.len() } + fn compute_line_index(&self, requested: Line) -> usize { + ((self.len() + self.zero + *self.visible_lines) - *requested) % self.len() + } + pub fn swap(&mut self, a: usize, b: usize) { let a = self.compute_index(a); let b = self.compute_index(b); @@ -69,10 +73,9 @@ impl Storage { } pub fn swap_lines(&mut self, a: Line, b: Line) { - println!("visible: {}, a: {}, b: {}", self.visible_lines, a, b); - let a = self.visible_lines - a; - let b = self.visible_lines - b; - self.swap(*a, *b); + let a = self.compute_line_index(a); + let b = self.compute_line_index(b); + self.inner.swap(a, b); } pub fn iter_mut(&mut self) -> IterMut { -- cgit