Skip to content

Commit

Permalink
gh-111201: Add append to screen method to avoid recalculation (#119274)
Browse files Browse the repository at this point in the history
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
  • Loading branch information
lysnikolaou and ambv committed May 22, 2024
1 parent d065edf commit c886bec
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,10 @@ def do(self) -> None:
class self_insert(EditCommand):
def do(self) -> None:
r = self.reader
r.insert(self.event * r.get_arg())
text = self.event * r.get_arg()
r.insert(text)
if len(text) == 1 and r.pos == len(r.buffer):
r.calc_screen = r.append_to_screen


class insert_nl(EditCommand):
Expand Down
16 changes: 8 additions & 8 deletions Lib/_pyrepl/completing_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def do(self) -> None:
if p:
r.insert(p)
if last_is_completer:
if not r.cmpltn_menu_vis:
r.cmpltn_menu_vis = 1
if not r.cmpltn_menu_visible:
r.cmpltn_menu_visible = True
r.cmpltn_menu, r.cmpltn_menu_end = build_menu(
r.console, completions, r.cmpltn_menu_end,
r.use_brackets, r.sort_in_column)
Expand All @@ -208,7 +208,7 @@ def do(self) -> None:

commands.self_insert.do(self)

if r.cmpltn_menu_vis:
if r.cmpltn_menu_visible:
stem = r.get_stem()
if len(stem) < 1:
r.cmpltn_reset()
Expand All @@ -235,7 +235,7 @@ class CompletingReader(Reader):

### Instance variables
cmpltn_menu: list[str] = field(init=False)
cmpltn_menu_vis: int = field(init=False)
cmpltn_menu_visible: bool = field(init=False)
cmpltn_menu_end: int = field(init=False)
cmpltn_menu_choices: list[str] = field(init=False)

Expand All @@ -255,9 +255,9 @@ def after_command(self, cmd: Command) -> None:
if not isinstance(cmd, (complete, self_insert)):
self.cmpltn_reset()

def calc_screen(self) -> list[str]:
screen = super().calc_screen()
if self.cmpltn_menu_vis:
def calc_complete_screen(self) -> list[str]:
screen = super().calc_complete_screen()
if self.cmpltn_menu_visible:
ly = self.lxy[1]
screen[ly:ly] = self.cmpltn_menu
self.screeninfo[ly:ly] = [(0, [])]*len(self.cmpltn_menu)
Expand All @@ -270,7 +270,7 @@ def finish(self) -> None:

def cmpltn_reset(self) -> None:
self.cmpltn_menu = []
self.cmpltn_menu_vis = 0
self.cmpltn_menu_visible = False
self.cmpltn_menu_end = 0
self.cmpltn_menu_choices = []

Expand Down
34 changes: 30 additions & 4 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
# types
Command = commands.Command
if False:
from typing import Callable
from .types import Callback, SimpleContextManager, KeySpec, CommandName
CalcScreen = Callable[[], list[str]]


def disp_str(buffer: str) -> tuple[str, list[int]]:
Expand Down Expand Up @@ -231,9 +233,11 @@ class Reader:
keymap: tuple[tuple[str, str], ...] = ()
input_trans: input.KeymapTranslator = field(init=False)
input_trans_stack: list[input.KeymapTranslator] = field(default_factory=list)
screen: list[str] = field(default_factory=list)
screeninfo: list[tuple[int, list[int]]] = field(init=False)
cxy: tuple[int, int] = field(init=False)
lxy: tuple[int, int] = field(init=False)
calc_screen: CalcScreen = field(init=False)

def __post_init__(self) -> None:
# Enable the use of `insert` without a `prepare` call - necessary to
Expand All @@ -243,14 +247,36 @@ def __post_init__(self) -> None:
self.input_trans = input.KeymapTranslator(
self.keymap, invalid_cls="invalid-key", character_cls="self-insert"
)
self.screeninfo = [(0, [0])]
self.screeninfo = [(0, [])]
self.cxy = self.pos2xy()
self.lxy = (self.pos, 0)
self.calc_screen = self.calc_complete_screen

def collect_keymap(self) -> tuple[tuple[KeySpec, CommandName], ...]:
return default_keymap

def calc_screen(self) -> list[str]:
def append_to_screen(self) -> list[str]:
new_screen = self.screen.copy() or ['']

new_character = self.buffer[-1]
new_character_len = wlen(new_character)

last_line_len = wlen(new_screen[-1])
if last_line_len + new_character_len >= self.console.width: # We need to wrap here
new_screen[-1] += '\\'
self.screeninfo[-1][1].append(1)
new_screen.append(self.buffer[-1])
self.screeninfo.append((0, [new_character_len]))
else:
new_screen[-1] += self.buffer[-1]
self.screeninfo[-1][1].append(new_character_len)
self.cxy = self.pos2xy()

# Reset the function that is used for completing the screen
self.calc_screen = self.calc_complete_screen
return new_screen

def calc_complete_screen(self) -> list[str]:
"""The purpose of this method is to translate changes in
self.buffer into changes in self.screen. Currently it rips
everything down and starts from scratch, which whilst not
Expand Down Expand Up @@ -563,8 +589,8 @@ def update_screen(self) -> None:
def refresh(self) -> None:
"""Recalculate and refresh the screen."""
# this call sets up self.cxy, so call it first.
screen = self.calc_screen()
self.console.refresh(screen, self.cxy)
self.screen = self.calc_screen()
self.console.refresh(self.screen, self.cxy)
self.dirty = False

def do_cmd(self, cmd: tuple[str, list[str]]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def refresh(self, screen, c_xy):

self.__show_cursor()

self.screen = screen
self.screen = screen.copy()
self.move_cursor(cx, cy)
self.flushoutput()

Expand Down

0 comments on commit c886bec

Please sign in to comment.