diff --git a/babi.py b/babi.py index c8ce3d9..38a34e5 100644 --- a/babi.py +++ b/babi.py @@ -84,11 +84,19 @@ class Position: self.x -= 1 self.cursor_x_hint = self.x + def home(self, margin: Margin, lines: List[str]) -> None: + self.x = self.cursor_x_hint = 0 + + def end(self, margin: Margin, lines: List[str]) -> None: + self.x = self.cursor_x_hint = len(lines[self.cursor_line]) + DISPATCH = { curses.KEY_DOWN: down, curses.KEY_UP: up, curses.KEY_LEFT: left, curses.KEY_RIGHT: right, + curses.KEY_HOME: home, + curses.KEY_END: end, } def dispatch(self, key: int, margin: Margin, lines: List[str]) -> None: diff --git a/tests/babi_test.py b/tests/babi_test.py index f60bfb5..c190196 100644 --- a/tests/babi_test.py +++ b/tests/babi_test.py @@ -298,6 +298,34 @@ def test_scrolling_arrow_key_movement(tmpdir): h.await_text('line_0') +def test_end_key(tmpdir): + f = tmpdir.join('f') + f.write('hello world\nhello world\n') + + with run(str(f)) as h, and_exit(h): + h.await_text('hello world') + assert h.get_cursor_position() == (0, 1) + h.press('End') + assert h.get_cursor_position() == (11, 1) + h.press('Down') + assert h.get_cursor_position() == (11, 2) + + +def test_home_key(tmpdir): + f = tmpdir.join('f') + f.write('hello world\nhello world\n') + + with run(str(f)) as h, and_exit(h): + h.await_text('hello world') + h.press('Down') + h.press('Left') + assert h.get_cursor_position() == (11, 1) + h.press('Home') + assert h.get_cursor_position() == (0, 1) + h.press('Down') + assert h.get_cursor_position() == (0, 2) + + def test_resize_scrolls_up(tmpdir): f = tmpdir.join('f') f.write('\n'.join(f'line_{i}' for i in range(10)))