From f2d87e84c7d186f088ec6479fbf74b130f7ad11f Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Fri, 4 Dec 2026 01:46:06 +0530 Subject: [PATCH] Fix prompt-toolkit color names for Rich white and bright white ANSI_NAMES maps Rich's standard colors 0-15 to prompt-toolkit names. Color 7 (white, SGR 37) was mapped to "ansiwhite", which prompt-toolkit renders as bright white (SGR 97), and color 15 (bright white) was mapped to "ansibrightwhite", which prompt-toolkit does not know. A theme style using bright_white then made Style() raise "Wrong color format". Use "ansigray" for color 7 and "ansiwhite" for color 15. --- cmd2/pt_utils.py | 8 +++++--- tests/test_pt_utils.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cmd2/pt_utils.py b/cmd2/pt_utils.py index 01319ae64..66e40807a 100644 --- a/cmd2/pt_utils.py +++ b/cmd2/pt_utils.py @@ -52,7 +52,9 @@ BASE_DELIMITERS = " \t\n" + "".join(constants.QUOTES) + "".join(constants.REDIRECTION_CHARS) -# prompt_toolkit accepts these standard ANSI color names directly +# prompt_toolkit accepts these standard ANSI color names directly. Its names for colors 7 and 15 are +# unusual: normal white (SGR 37) is "ansigray" and bright white (SGR 97) is "ansiwhite". It has no +# "ansibrightwhite", which it rejects as a bad color format. ANSI_NAMES = ( "ansiblack", "ansired", @@ -61,7 +63,7 @@ "ansiblue", "ansimagenta", "ansicyan", - "ansiwhite", + "ansigray", "ansibrightblack", "ansibrightred", "ansibrightgreen", @@ -69,7 +71,7 @@ "ansibrightblue", "ansibrightmagenta", "ansibrightcyan", - "ansibrightwhite", + "ansiwhite", ) diff --git a/tests/test_pt_utils.py b/tests/test_pt_utils.py index e6e33c089..3edefd047 100644 --- a/tests/test_pt_utils.py +++ b/tests/test_pt_utils.py @@ -1415,6 +1415,22 @@ def test_rich_to_pt_color_standard(self): c = Color.from_ansi(2) assert pt_utils.rich_to_pt_color(c) == "ansigreen" + def test_rich_to_pt_color_white_is_not_bright_white(self): + from rich.color import Color + + # Rich's "white" is SGR 37, which prompt_toolkit calls "ansigray". Its "ansiwhite" is SGR 97. + assert pt_utils.rich_to_pt_color(Color.parse("white")) == "ansigray" + assert pt_utils.rich_to_pt_color(Color.parse("bright_white")) == "ansiwhite" + + def test_rich_to_pt_color_standard_colors_are_valid(self): + from prompt_toolkit.styles import Style + from rich.color import Color + + # prompt_toolkit raises ValueError for a color name it does not know + for number in range(16): + pt_color = pt_utils.rich_to_pt_color(Color.from_ansi(number)) + Style([("x", f"fg:{pt_color}")]) + def test_rich_to_pt_color_eight_bit(self): from rich.color import Color