Skip to content

Commit 0041f4b

Browse files
Allow users to hook gen_color function to pyrepl Reader
1 parent 85f3009 commit 0041f4b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Lib/_pyrepl/reader.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@
2828
from dataclasses import dataclass, field, fields
2929

3030
from . import commands, console, input
31-
from .utils import wlen, unbracket, disp_str, gen_colors, THEME
31+
from .utils import wlen, unbracket, disp_str, gen_colors, ColorSpan, THEME
3232
from .trace import trace
3333

3434

3535
# types
3636
Command = commands.Command
37-
from .types import Callback, SimpleContextManager, KeySpec, CommandName
37+
from .types import (
38+
Callable,
39+
Callback,
40+
Iterator,
41+
SimpleContextManager,
42+
KeySpec,
43+
CommandName,
44+
)
3845

3946

4047
# syntax classes
@@ -213,6 +220,7 @@ class Reader:
213220
lxy: tuple[int, int] = field(init=False)
214221
scheduled_commands: list[str] = field(default_factory=list)
215222
can_colorize: bool = False
223+
gen_colors: Callable[[str], Iterator[ColorSpan]] = field(default=gen_colors)
216224
threading_hook: Callback | None = None
217225

218226
## cached metadata to speed up screen refreshes
@@ -312,7 +320,7 @@ def calc_screen(self) -> list[str]:
312320
prompt_from_cache = (offset and self.buffer[offset - 1] != "\n")
313321

314322
if self.can_colorize:
315-
colors = list(gen_colors(self.get_unicode()))
323+
colors = list(self.gen_colors(self.get_unicode()))
316324
else:
317325
colors = None
318326
trace("colors = {colors}", colors=colors)

0 commit comments

Comments
 (0)