From 0038c972fb1a76dbe6a544490ff5d46078165546 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 2 Nov 2019 15:25:53 -0700 Subject: [PATCH] Fix paging up to negative offsets --- babi.py | 2 +- tests/babi_test.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/babi.py b/babi.py index 64c8d56..1a70f2e 100644 --- a/babi.py +++ b/babi.py @@ -313,7 +313,7 @@ class File: if self.cursor_line < margin.body_lines: self.cursor_line = self.file_line = 0 else: - pos = self.file_line - margin.page_size + pos = max(self.file_line - margin.page_size, 0) self.cursor_line = self.file_line = pos self._set_x_after_vertical_movement() diff --git a/tests/babi_test.py b/tests/babi_test.py index 64a16b8..ed47c20 100644 --- a/tests/babi_test.py +++ b/tests/babi_test.py @@ -527,6 +527,19 @@ def test_resize_scroll_does_not_go_negative(tmpdir): assert h.get_screen_line(1) == 'line_0' +def test_page_up_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), height=10) as h, and_exit(h): + for _ in range(8): + h.press('Down') + h.await_cursor_position(x=0, y=4) + h.press('^Y') + h.await_cursor_position(x=0, y=1) + assert h.get_cursor_line() == 'line_0' + + def test_very_narrow_window_status(): with run(height=50) as h, and_exit(h): with h.resize(5, 50):