Implement ^Left and ^Right in prompt()

This commit is contained in:
Anthony Sottile
2019-12-31 14:04:17 -08:00
parent 8914ad4ea1
commit 60476134a3
2 changed files with 65 additions and 7 deletions

28
babi.py
View File

@@ -302,6 +302,24 @@ class Prompt:
def _end(self) -> None:
self._x = len(self._s)
def _ctrl_left(self) -> None:
if self._x <= 1:
self._x = 0
else:
self._x -= 1
tp = self._s[self._x - 1].isalnum()
while self._x > 0 and tp == self._s[self._x - 1].isalnum():
self._x -= 1
def _ctrl_right(self) -> None:
if self._x >= len(self._s) - 1:
self._x = len(self._s)
else:
self._x += 1
tp = self._s[self._x].isalnum()
while self._x < len(self._s) and tp == self._s[self._x].isalnum():
self._x += 1
def _backspace(self) -> None:
if self._x > 0:
self._s = self._s[:self._x - 1] + self._s[self._x:]
@@ -371,6 +389,8 @@ class Prompt:
DISPATCH_KEY = {
b'^A': _home,
b'^E': _end,
b'kRIT5': _ctrl_right,
b'kLFT5': _ctrl_left,
b'^K': _cut_to_end,
b'^R': _reverse_search,
b'^C': _cancel,
@@ -721,8 +741,8 @@ class File:
# if we're inside the line, jump to next position that's not our type
else:
self.x = self.x_hint = self.x + 1
isalnum = line[self.x].isalnum()
while self.x < len(line) and isalnum == line[self.x].isalnum():
tp = line[self.x].isalnum()
while self.x < len(line) and tp == line[self.x].isalnum():
self.x = self.x_hint = self.x + 1
@action
@@ -745,8 +765,8 @@ class File:
self.x = self.x_hint = len(self.lines[self.cursor_y])
else:
self.x = self.x_hint = self.x - 1
isalnum = line[self.x - 1].isalnum()
while self.x > 0 and isalnum == line[self.x - 1].isalnum():
tp = line[self.x - 1].isalnum()
while self.x > 0 and tp == line[self.x - 1].isalnum():
self.x = self.x_hint = self.x - 1
@action

View File

@@ -63,18 +63,56 @@ def test_key_navigation_in_command_mode():
h.press('Enter')
def test_command_mode_ctrl_k(tmpdir):
def test_command_mode_ctrl_k():
with run() as h, and_exit(h):
trigger_command_mode(h)
h.press('hello world')
h.await_text('\nhello world\n')
for _ in range(6): # TODO: ^Left
h.press('Left')
h.press('^Left')
h.press('Left')
h.press('^K')
h.await_text('\nhello\n')
h.press('Enter')
def test_command_mode_control_left():
with run() as h, and_exit(h):
trigger_command_mode(h)
h.press('hello world')
h.await_cursor_position(x=11, y=23)
h.press('^Left')
h.await_cursor_position(x=6, y=23)
h.press('^Left')
h.await_cursor_position(x=0, y=23)
h.press('^Left')
h.await_cursor_position(x=0, y=23)
h.press('Right')
h.await_cursor_position(x=1, y=23)
h.press('^Left')
h.await_cursor_position(x=0, y=23)
h.press('^C')
def test_command_mode_control_right():
with run() as h, and_exit(h):
trigger_command_mode(h)
h.press('hello world')
h.await_cursor_position(x=11, y=23)
h.press('^Right')
h.await_cursor_position(x=11, y=23)
h.press('Left')
h.await_cursor_position(x=10, y=23)
h.press('^Right')
h.await_cursor_position(x=11, y=23)
h.press('^A')
h.await_cursor_position(x=0, y=23)
h.press('^Right')
h.await_cursor_position(x=5, y=23)
h.press('^Right')
h.await_cursor_position(x=11, y=23)
h.press('^C')
def test_save_via_command_mode(tmpdir):
f = tmpdir.join('f')