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
Prev Previous commit
Next Next commit
Rework method to choose function
  • Loading branch information
lysnikolaou committed May 21, 2024
commit 34712d0a3a2f30c76e32c9b40e49f9fa0b5a6d63
4 changes: 3 additions & 1 deletion Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from __future__ import annotations
import os

from .utils import CalcScreen

# Categories of actions:
# killing
# yanking
Expand Down Expand Up @@ -359,7 +361,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
r.calc_screen_method = CalcScreen.CALC_APPEND_SCREEN


class insert_nl(EditCommand):
Expand Down
18 changes: 13 additions & 5 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@


from . import commands, console, input
from .utils import ANSI_ESCAPE_SEQUENCE, wlen
from .utils import ANSI_ESCAPE_SEQUENCE, wlen, CalcScreen
from .trace import trace


# types
Command = commands.Command
if False:
from .types import Callback, SimpleContextManager, KeySpec, CommandName, Callable, Self
from .types import Callback, SimpleContextManager, KeySpec, CommandName


def disp_str(buffer: str) -> tuple[str, list[int]]:
Expand Down Expand Up @@ -233,7 +233,7 @@ class Reader:
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)
calc_screen_method: CalcScreen = CalcScreen.CALC_COMPLETE_SCREEN

def __post_init__(self) -> None:
# Enable the use of `insert` without a `prepare` call - necessary to
Expand All @@ -246,7 +246,6 @@ def __post_init__(self) -> None:
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
Expand All @@ -269,7 +268,7 @@ def append_to_screen(self) -> list[str]:
self.cxy = self.pos2xy()

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

def calc_complete_screen(self) -> list[str]:
Expand Down Expand Up @@ -327,6 +326,15 @@ def calc_complete_screen(self) -> list[str]:
screeninfo.append((0, []))
return screen

def calc_screen(self) -> list[str]:
match self.calc_screen_method:
case CalcScreen.CALC_COMPLETE_SCREEN:
return self.calc_complete_screen()
case CalcScreen.CALC_APPEND_SCREEN:
return self.append_to_screen()
case _:
assert False, "No valid calc_screen"

def process_prompt(self, prompt: str) -> tuple[str, int]:
"""Process the prompt.

Expand Down
1 change: 0 additions & 1 deletion Lib/_pyrepl/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Self
from collections.abc import Callable, Iterator

Callback = Callable[[], object]
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 @@ -214,7 +214,7 @@ def change_encoding(self, encoding: str) -> None:
"""
self.encoding = encoding

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

Expand Down
6 changes: 6 additions & 0 deletions Lib/_pyrepl/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import re
import unicodedata
from enum import Enum

ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]")


class CalcScreen(Enum):
CALC_COMPLETE_SCREEN = 1
CALC_APPEND_SCREEN = 2


def str_width(c: str) -> int:
w = unicodedata.east_asian_width(c)
if w in ('N', 'Na', 'H', 'A'):
Expand Down
Loading
0