Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,21 @@ def do(self) -> None:
for i in range(r.get_arg()):
r.pos = r.bow()

class up_history(MotionCommand):
def do(self) -> None:
r = self.reader
if r.historyi > 0:
r.select_item(r.historyi - 1)
else:
r.error("start of buffer")

class down_history(MotionCommand):
def do(self) -> None:
r = self.reader
if r.historyi < len(r.history):
r.select_item(r.historyi + 1)
else:
r.error("end of buffer")

class self_insert(EditCommand):
def do(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion Lib/_pyrepl/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def _parse_single_key_sequence(key: str, s: int) -> tuple[list[str], int]:
if ctrl:
if len(ret) == 1:
ret = chr(ord(ret) & 0x1F) # curses.ascii.ctrl()
elif ret in {"left", "right"}:
elif ret in {"left", "right", "up", "down"}:
ret = f"ctrl {ret}"
else:
raise KeySpecError("\\C- followed by invalid key")
Expand Down
2 changes: 2 additions & 0 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def make_default_commands() -> dict[CommandName, CommandClass]:
+ [(c, "self-insert") for c in map(chr, range(128, 256)) if c.isalpha()]
+ [
(r"\<up>", "up"),
(r"\C-\<up>", "up_history"),
(r"\<down>", "down"),
(r"\C-\<down>", "down_history"),
(r"\<left>", "left"),
(r"\C-\<left>", "backward-word"),
(r"\<right>", "right"),
Expand Down
4 changes: 4 additions & 0 deletions Lib/_pyrepl/unix_eventqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@
# for xterm, gnome-terminal, xfce terminal, etc.
b'\033[1;5D': 'ctrl left',
b'\033[1;5C': 'ctrl right',
b'\033[1;5A': 'ctrl up',
b'\033[1;5B': 'ctrl down',
# for rxvt
b'\033Od': 'ctrl left',
b'\033Oc': 'ctrl right',
b'\033Oa': 'ctrl up',
b'\033Ob': 'ctrl down',
}

def get_terminal_keycodes(ti: TermInfo) -> dict[bytes, str]:
Expand Down
2 changes: 2 additions & 0 deletions Lib/_pyrepl/windows_eventqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
b'\x1b[D': 'left',
b'\x1b[1;5D': 'ctrl left',
b'\x1b[1;5C': 'ctrl right',
b'\x1b[1;5A': 'ctrl up',
b'\x1b[1;5B': 'ctrl down',

b'\x1b[H': 'home',
b'\x1b[F': 'end',
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_pyrepl/test_keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def test_combinations(self):
self.assertEqual(parse_keys("\\C-a\\n\\<up>"), ["\x01", "\n", "up"])
self.assertEqual(parse_keys("\\M-a\\t\\<down>"), ["\033", "a", "\t", "down"])

def test_control_arrow_keys(self):
self.assertEqual(parse_keys("\\C-\\<left>"), ["ctrl left"])
self.assertEqual(parse_keys("\\C-\\<right>"), ["ctrl right"])
self.assertEqual(parse_keys("\\C-\\<up>"), ["ctrl up"])
self.assertEqual(parse_keys("\\C-\\<down>"), ["ctrl down"])

def test_keyspec_errors(self):
cases = [
("\\Ca", "\\C must be followed by `-'"),
Expand Down
73 changes: 73 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,79 @@ def test_history_navigation_with_down_arrow(self):
self.assertEqual(output, "1+1")
self.assert_screen_equal(reader, "1+1", clean=True)

def test_history_navigation_with_ctrl_up(self):
# Submit two multiline blocks, then use ctrl+up to jump directly
# to the previous history entry (unlike up which moves line-by-line)
code = "def foo():\nx = 1\n\ndef bar():\ny = 2\n\n"
events = itertools.chain(
code_to_events(code),
[
# Single ctrl+up should recall the entire previous entry
Event(evt="key", data="ctrl up", raw=bytearray(b"\x1b[1;5A")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
],
)

reader = self.prepare_reader(events)

output = multiline_input(reader)
self.assertEqual(output, "def foo():\n x = 1\n ")
output = multiline_input(reader)
self.assertEqual(output, "def bar():\n y = 2\n ")
# One ctrl+up jumps straight to previous history item
output = multiline_input(reader)
self.assertEqual(output, "def foo():\n x = 1\n ")

def test_history_navigation_with_ctrl_down(self):
# Submit two multiline entries, ctrl+up twice then ctrl+down once
# With multiline entries, regular down would only move one line,
# but ctrl+down jumps to the next history entry entirely.
code = "def foo():\nx = 1\n\ndef bar():\ny = 2\n\n"
events = itertools.chain(
code_to_events(code),
[
Event(evt="key", data="ctrl up", raw=bytearray(b"\x1b[1;5A")),
Event(evt="key", data="ctrl up", raw=bytearray(b"\x1b[1;5A")),
Event(evt="key", data="ctrl down", raw=bytearray(b"\x1b[1;5B")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
],
)

reader = self.prepare_reader(events)

output = multiline_input(reader)
self.assertEqual(output, "def foo():\n x = 1\n ")
output = multiline_input(reader)
self.assertEqual(output, "def bar():\n y = 2\n ")
# ctrl+up twice (to foo), ctrl+down once (back to bar)
output = multiline_input(reader)
self.assertEqual(output, "def bar():\n y = 2\n ")

def test_ctrl_up_at_start_of_history(self):
# Submit a multiline block, then try ctrl+up twice
# (second one should error since we're already at the oldest entry)
code = "def foo():\nx = 1\n\n"
events = itertools.chain(
code_to_events(code),
[
# Already at oldest item; second ctrl+up should error but not crash
Event(evt="key", data="ctrl up", raw=bytearray(b"\x1b[1;5A")),
Event(evt="key", data="ctrl up", raw=bytearray(b"\x1b[1;5A")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
],
)

reader = self.prepare_reader(events)

output = multiline_input(reader)
self.assertEqual(output, "def foo():\n x = 1\n ")
# Second ctrl+up at start of history stays on first item
output = multiline_input(reader)
self.assertEqual(output, "def foo():\n x = 1\n ")

def test_history_search(self):
events = itertools.chain(
code_to_events("1+1\n2+2\n3+3\n"),
Expand Down
Loading