8000 gh-119548: Add a 'clear' command to the REPL (#119549) · python/cpython@e3bac04 · GitHub
[go: up one dir, main page]

Skip to content

Commit e3bac04

Browse files
authored
gh-119548: Add a 'clear' command to the REPL (#119549)
1 parent a531fd7 commit e3bac04

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Lib/_pyrepl/reader.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ class Reader:
238238
cxy: tuple[int, int] = field(init=False)
239239
lxy: tuple[int, int] = field(init=False)
240240
calc_screen: CalcScreen = field(init=False)
241+
scheduled_commands: list[str] = field(default_factory=list)
241242

242243
def __post_init__(self) -> None:
243244
# Enable the use of `insert` without a `prepare` call - necessary to
@@ -557,6 +558,10 @@ def prepare(self) -> None:
557558
self.restore()
558559
raise
559560

561+
while self.scheduled_commands:
562+
cmd = self.scheduled_commands.pop()
563+
self.do_cmd((cmd, []))
564+
560565
def last_command_is(self, cls: type) -> bool:
561566
if not self.last_command:
562567
return False

Lib/_pyrepl/simple_interact.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ def _strip_final_indent(text: str) -> str:
5757
return text
5858

5959

60+
def _clear_screen():
61+
reader = _get_reader()
62+
reader.scheduled_commands.append("clear_screen")
63+
64+
6065
REPL_COMMANDS = {
6166
"exit": _sitebuiltins.Quitter('exit', ''),
6267
"quit": _sitebuiltins.Quitter('quit' ,''),
6368
"copyright": _sitebuiltins._Printer('copyright', sys.copyright),
6469
"help": "help",
65-
"clear": "clear_screen",
70+
"clear": _clear_screen,
6671
}
6772

6873
class InteractiveColoredConsole(code.InteractiveConsole):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a ``clear`` command to the REPL. Patch by Pablo Galindo

0 commit comments

Comments
 (0)
0