8000 gh-111201: Add append to screen method to avoid recalculation by lysnikolaou · Pull Request #119274 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111201: Add append to screen method to avoid recalculation #119274

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

Merged
merged 5 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-111201: Add append to screen method to avoid recalculation
  • Loading branch information
lysnikolaou committed May 20, 2024
commit c6f6b5449fb275ef5d78a8a3e8f7f800fbc1a644
1 change: 1 addition & 0 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ class self_insert(EditCommand):
def do(self) -> None:
r = self.reader
r.insert(self.event * r.get_arg())
r.calc_screen = r.append_to_screen


class insert_nl(EditCommand):
Expand Down
34 changes: 29 additions & 5 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
# types
Command = commands.Command
if False:
from .types import Callback, SimpleContextManager, KeySpec, CommandName
from .types import Callback, SimpleContextManager, KeySpec, CommandName, Callable, Self


def disp_str(buffer: str) -> tuple[str, list[int]]:
Expand Down Expand Up @@ -229,9 +229,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: Callable[[Self], list[str]] = field(init=False)

def __post_init__(self) -> None:
# Enable the use of `insert` without a `prepare` call - necessary to
Expand All @@ -241,14 +243,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 @@ -561,8 +585,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
1 change: 1 addition & 0 deletions Lib/_pyrepl/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Self
from collections.abc import Callable, Iterator

Callback = Callable[[], object]
Expand Down
4 changes: 2 additions & 2 deletions Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def change_encoding(self, encoding: str) -> None:
"""
self.encoding = encoding

def refresh(self, screen, c_xy):
def refresh(self, screen: list[str], c_xy: tuple[int, int]) -> None:
"""
Refresh the console screen.

Expand Down 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