Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
7 changes: 6 additions & 1 deletion Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import sys
import _colorize
from _colorize import Syntax

from contextlib import contextmanager
from dataclasses import dataclass, field, fields
Expand Down Expand Up @@ -213,6 +214,7 @@ class Reader:
lxy: tuple[int, int] = field(init=False)
scheduled_commands: list[str] = field(default_factory=list)
can_colorize: bool = False
theme: Syntax | None = None
threading_hook: Callback | None = None

## cached metadata to speed up screen refreshes
Expand Down Expand Up @@ -274,6 +276,8 @@ def __post_init__(self) -> None:
self.cxy = self.pos2xy()
self.lxy = (self.pos, 0)
self.can_colorize = _colorize.can_colorize()
if self.can_colorize:
self.theme = THEME()

self.last_refresh_cache.screeninfo = self.screeninfo
self.last_refresh_cache.pos = self.pos
Expand Down Expand Up @@ -491,7 +495,8 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:
prompt = self.ps1

if self.can_colorize:
t = THEME()
t = self.theme
assert t is not None # theme is set when can_colorize is True
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt

Expand Down
5 changes: 3 additions & 2 deletions Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def __init__(
self.pollob.register(self.input_fd, select.POLLIN)
self.terminfo = terminfo.TermInfo(term or None)
self.term = term
self.is_mac = platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal"

@overload
def _my_getstr(cap: str, optional: Literal[False] = False) -> bytes: ...
Expand Down Expand Up @@ -339,7 +340,7 @@ def prepare(self):
tcsetattr(self.input_fd, termios.TCSADRAIN, raw)

# In macOS terminal we need to deactivate line wrap via ANSI escape code
if platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal":
if self.is_mac:
os.write(self.output_fd, b"\033[?7l")

self.screen = []
Expand Down Expand Up @@ -370,7 +371,7 @@ def restore(self):
self.flushoutput()
tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate)

if platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal":
if self.is_mac:
os.write(self.output_fd, b"\033[?7h")

if hasattr(self, "old_sigwinch"):
Expand Down
Loading