Skip to content

Commit

Permalink
gh-119548: Add a 'clear' command to the REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal committed May 25, 2024
1 parent 406ffb5 commit 3e31ea8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class Reader:
cxy: tuple[int, int] = field(init=False)
lxy: tuple[int, int] = field(init=False)
calc_screen: CalcScreen = field(init=False)
scheduled_commands: list[str] = field(default_factory=list)

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

while self.scheduled_commands:
cmd = self.scheduled_commands.pop()
self.do_cmd((cmd, []))

def last_command_is(self, cls: type) -> bool:
if not self.last_command:
return False
Expand Down
7 changes: 6 additions & 1 deletion Lib/_pyrepl/simple_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ def _strip_final_indent(text: str) -> str:
return text


def _clear_screen():
reader = _get_reader()
reader.scheduled_commands.append("clear_screen")


REPL_COMMANDS = {
"exit": _sitebuiltins.Quitter('exit', ''),
"quit": _sitebuiltins.Quitter('quit' ,''),
"copyright": _sitebuiltins._Printer('copyright', sys.copyright),
"help": "help",
"clear": "clear_screen",
"clear": _clear_screen,
}

class InteractiveColoredConsole(code.InteractiveConsole):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a ``clear`` command to the REPL. Patch by Pablo Galindo

0 comments on commit 3e31ea8

Please sign in to comment.