From a0b7eb2f9c05e1379df77e080f3c19f900e6b85e Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 13 Sep 2019 17:32:47 -0700 Subject: [PATCH] Fix scrolling to negative offsets --- babi.py | 1 + tests/babi_test.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/babi.py b/babi.py index be21915..f79b761 100644 --- a/babi.py +++ b/babi.py @@ -61,6 +61,7 @@ class Position: def maybe_scroll_up(self, margin: Margin) -> None: if self.cursor_line < self.file_line: self.file_line -= self._scroll_amount() + self.file_line = max(self.file_line, 0) def up(self, margin: Margin, lines: List[str]) -> None: if self.cursor_line > 0: diff --git a/tests/babi_test.py b/tests/babi_test.py index 479772b..d1a9078 100644 --- a/tests/babi_test.py +++ b/tests/babi_test.py @@ -385,6 +385,25 @@ def test_resize_scrolls_up(tmpdir): assert h.screenshot().splitlines()[3] == 'line_7' +def test_resize_scroll_does_not_go_negative(tmpdir): + f = tmpdir.join('f') + f.write('\n'.join(f'line_{i}' for i in range(10))) + + with run(str(f)) as h, and_exit(h): + for _ in range(5): + h.press('Down') + h.await_cursor_position(x=0, y=6) + + with h.resize(80, 7): + h.await_text_missing('line_9') + h.await_text('line_9') + + for _ in range(2): + h.press('Up') + + assert h.screenshot().splitlines()[1] == 'line_0' + + def test_very_narrow_window_status(): with run(height=50) as h, and_exit(h): with h.resize(5, 50):