Skip to content

gh-120767: Add REPL history navigation with partial text #121859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ def make_default_commands() -> dict[CommandName, type[Command]]:
+ [(c, "self-insert") for c in map(chr, range(32, 127)) if c != "\\"]
+ [(c, "self-insert") for c in map(chr, range(128, 256)) if c.isalpha()]
+ [
(r"\<up>", "up"),
(r"\<down>", "down"),
(r"\<up>", "history-search-backward"),
(r"\<down>", "history-search-forward"),
(r"\<left>", "left"),
(r"\C-\<left>", "backward-word"),
(r"\<right>", "right"),
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,46 @@ def test_history_navigation_with_up_arrow(self):
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")

def test_history_navigation_with_up_arrow_and_partial_text(self):
events = itertools.chain(
code_to_events("spam = 1\nham = 2\neggs = 3\nsp"),
[
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
],
)

reader = self.prepare_reader(events)

output = multiline_input(reader)
self.assertEqual(output, "spam = 1")

def test_history_navigation_with_up_arrow_and_partial_text_with_similar_entries(self):
events = itertools.chain(
code_to_events("a=111\na=11\na=1\na="),
[
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
],
)

reader = self.prepare_reader(events)
print()
output = multiline_input(reader)
self.assertEqual(output, "a=111")
self.assertEqual(clean_screen(reader.screen), "a=111")
output = multiline_input(reader)
self.assertEqual(output, "a=11")
self.assertEqual(clean_screen(reader.screen), "a=11")
output = multiline_input(reader)
self.assertEqual(output, "a=1")
self.assertEqual(clean_screen(reader.screen), "a=1")
output = multiline_input(reader)
self.assertEqual(output, "a=111")
self.assertEqual(clean_screen(reader.screen), "a=111")

def test_history_with_multiline_entries(self):
code = "def foo():\nx = 1\ny = 2\nz = 3\n\ndef bar():\nreturn 42\n\n"
events = list(itertools.chain(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Enhance REPL history navigation with partial text support

The REPL now correctly navigates history based on partial text
in the buffer when using the up arrow.
Comment on lines +1 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Enhance REPL history navigation with partial text support
The REPL now correctly navigates history based on partial text
in the buffer when using the up arrow.
Enhance REPL history navigation with partial text support.
The REPL now correctly navigates history based on partial text
in the buffer when using the up arrow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be careful with the word correctly here, as it could mean many different things.

Loading