Implement Home and End keys

This commit is contained in:
Anthony Sottile
2019-08-31 15:42:23 -07:00
parent 3dd1a0333a
commit 784c18320a
2 changed files with 36 additions and 0 deletions

View File

@@ -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:

View File

@@ -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)))