From 0041f4b7a100753fa07783eb3c9fb37d3f3723bb Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 16 Nov 2025 00:36:56 -0800 Subject: [PATCH 1/3] Allow users to hook gen_color function to pyrepl Reader --- Lib/_pyrepl/reader.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py index 0ebd9162eca4bb..bff69bb7d10348 100644 --- a/Lib/_pyrepl/reader.py +++ b/Lib/_pyrepl/reader.py @@ -28,13 +28,20 @@ from dataclasses import dataclass, field, fields from . import commands, console, input -from .utils import wlen, unbracket, disp_str, gen_colors, THEME +from .utils import wlen, unbracket, disp_str, gen_colors, ColorSpan, THEME from .trace import trace # types Command = commands.Command -from .types import Callback, SimpleContextManager, KeySpec, CommandName +from .types import ( + Callable, + Callback, + Iterator, + SimpleContextManager, + KeySpec, + CommandName, +) # syntax classes @@ -213,6 +220,7 @@ class Reader: lxy: tuple[int, int] = field(init=False) scheduled_commands: list[str] = field(default_factory=list) can_colorize: bool = False + gen_colors: Callable[[str], Iterator[ColorSpan]] = field(default=gen_colors) threading_hook: Callback | None = None ## cached metadata to speed up screen refreshes @@ -312,7 +320,7 @@ def calc_screen(self) -> list[str]: prompt_from_cache = (offset and self.buffer[offset - 1] != "\n") if self.can_colorize: - colors = list(gen_colors(self.get_unicode())) + colors = list(self.gen_colors(self.get_unicode())) else: colors = None trace("colors = {colors}", colors=colors) From 55a016f327d0603b7917517877dfe646a8ff15b3 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 16 Nov 2025 08:45:51 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-11-16-08-45-48.gh-issue-139167.Yr43k1.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-11-16-08-45-48.gh-issue-139167.Yr43k1.rst diff --git a/Misc/NEWS.d/next/Library/2025-11-16-08-45-48.gh-issue-139167.Yr43k1.rst b/Misc/NEWS.d/next/Library/2025-11-16-08-45-48.gh-issue-139167.Yr43k1.rst new file mode 100644 index 00000000000000..35a471f9b3bffb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-16-08-45-48.gh-issue-139167.Yr43k1.rst @@ -0,0 +1 @@ +Allow users to use customized ``gen_colors`` function for pyrepl ``Reader``. From dbe609466ec7e565b4d9e43a4f64de92a684df1a Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 16 Nov 2025 00:52:49 -0800 Subject: [PATCH 3/3] Fix mypy --- Lib/_pyrepl/reader.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py index bff69bb7d10348..6536b9316c1e59 100644 --- a/Lib/_pyrepl/reader.py +++ b/Lib/_pyrepl/reader.py @@ -34,14 +34,8 @@ # types Command = commands.Command -from .types import ( - Callable, - Callback, - Iterator, - SimpleContextManager, - KeySpec, - CommandName, -) +from collections.abc import Callable, Iterator +from .types import Callback, SimpleContextManager, KeySpec, CommandName # syntax classes