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