Add ^Up and ^Down for scrolling the screen by one line

This commit is contained in:
Anthony Sottile
2019-11-16 18:45:02 -08:00
parent 5b5280a7b8
commit c3df00db4a
2 changed files with 93 additions and 2 deletions

20
babi.py
View File

@@ -502,6 +502,18 @@ class File:
self.cursor_y = len(self.lines) - 1 self.cursor_y = len(self.lines) - 1
self._scroll_screen_if_needed(margin) self._scroll_screen_if_needed(margin)
@action
def ctrl_up(self, margin: Margin) -> None:
self.file_y = max(0, self.file_y - 1)
self.cursor_y = min(self.cursor_y, self.file_y + margin.body_lines - 1)
self._set_x_after_vertical_movement()
@action
def ctrl_down(self, margin: Margin) -> None:
self.file_y = min(len(self.lines) - 1, self.file_y + 1)
self.cursor_y = max(self.cursor_y, self.file_y)
self._set_x_after_vertical_movement()
@action @action
def go_to_line(self, lineno: int, margin: Margin) -> None: def go_to_line(self, lineno: int, margin: Margin) -> None:
self.x = self.x_hint = 0 self.x = self.x_hint = 0
@@ -627,6 +639,8 @@ class File:
b'^V': page_down, b'^V': page_down,
b'kHOM5': ctrl_home, b'kHOM5': ctrl_home,
b'kEND5': ctrl_end, b'kEND5': ctrl_end,
b'kUP5': ctrl_up,
b'kDN5': ctrl_down,
} }
@edit_action('text') @edit_action('text')
@@ -807,14 +821,16 @@ SEQUENCE_KEY = {
'\x1bOF': curses.KEY_END, '\x1bOF': curses.KEY_END,
} }
SEQUENCE_KEYNAME = { SEQUENCE_KEYNAME = {
'\x1b[1;5H': b'kHOM5', # C-Home '\x1b[1;5H': b'kHOM5', # ^Home
'\x1b[1;5F': b'kEND5', # C-End '\x1b[1;5F': b'kEND5', # ^End
'\x1bOH': b'KEY_HOME', '\x1bOH': b'KEY_HOME',
'\x1bOF': b'KEY_END', '\x1bOF': b'KEY_END',
'\x1b[1;3A': b'kUP3', # M-Up '\x1b[1;3A': b'kUP3', # M-Up
'\x1b[1;3B': b'kDN3', # M-Down '\x1b[1;3B': b'kDN3', # M-Down
'\x1b[1;3C': b'kRIT3', # M-Right '\x1b[1;3C': b'kRIT3', # M-Right
'\x1b[1;3D': b'kLFT3', # M-Left '\x1b[1;3D': b'kLFT3', # M-Left
'\x1b[1;5A': b'kUP5', # ^Up
'\x1b[1;5B': b'kDN5', # ^Down
} }

View File

@@ -649,6 +649,81 @@ def test_scrolling_arrow_key_movement(ten_lines):
h.await_text('line_0') h.await_text('line_0')
def test_ctrl_down_beginning_of_file(ten_lines):
with run(str(ten_lines), height=5) as h, and_exit(h):
h.press('^Down')
h.await_text('line_3')
h.await_cursor_position(x=0, y=1)
assert h.get_cursor_line() == 'line_1'
def test_ctrl_up_moves_screen_up_one_line(ten_lines):
with run(str(ten_lines), height=5) as h, and_exit(h):
h.press('^Down')
h.press('^Up')
h.await_text('line_0')
h.await_text('line_2')
h.await_cursor_position(x=0, y=2)
def test_ctrl_up_at_beginning_of_file_does_nothing(ten_lines):
with run(str(ten_lines), height=5) as h, and_exit(h):
h.press('^Up')
h.await_text('line_0')
h.await_text('line_2')
h.await_cursor_position(x=0, y=1)
def test_ctrl_up_at_bottom_of_screen(ten_lines):
with run(str(ten_lines), height=5) as h, and_exit(h):
h.press('^Down')
h.press('Down')
h.press('Down')
h.await_text('line_1')
h.await_text('line_3')
h.await_cursor_position(x=0, y=3)
h.press('^Up')
h.await_text('line_0')
h.await_cursor_position(x=0, y=3)
def test_ctrl_down_at_end_of_file(ten_lines):
with run(str(ten_lines), height=5) as h, and_exit(h):
h.press('^End')
for i in range(4):
h.press('^Down')
h.press('Up')
h.await_text('line_9')
assert h.get_cursor_line() == 'line_9'
def test_ctrl_down_causing_cursor_movement_should_fix_x(tmpdir):
f = tmpdir.join('f')
f.write('1\n\n2\n\n3\n\n4\n')
with run(str(f), height=5) as h, and_exit(h):
h.press('Right')
h.press('^Down')
h.await_text_missing('\n1\n')
h.await_cursor_position(x=0, y=1)
def test_ctrl_up_causing_cursor_movement_should_fix_x(tmpdir):
f = tmpdir.join('f')
f.write('1\n\n2\n\n3\n\n4\n')
with run(str(f), height=5) as h, and_exit(h):
h.press('^Down')
h.press('^Down')
h.press('Down')
h.press('Down')
h.press('Right')
h.await_text('3')
h.press('^Up')
h.await_text_missing('3')
h.await_cursor_position(x=0, y=3)
@pytest.mark.parametrize('k', ('End', '^E')) @pytest.mark.parametrize('k', ('End', '^E'))
def test_end_key(tmpdir, k): def test_end_key(tmpdir, k):
f = tmpdir.join('f') f = tmpdir.join('f')