Skip to content

Fix prompt-toolkit color names for Rich white and bright white - #1863

Merged
tleonhardt merged 1 commit into
python-cmd2:mainfrom
mokshupadhyay:fix/pt-white-color-names
Dec 3, 2026
Merged

tleonhardt merged 1 commit into
python-cmd2:mainfrom
mokshupadhyay:fix/pt-white-color-names

Conversation

@mokshupadhyay

Copy link
Copy Markdown
Contributor

cmd2/pt_utils.py maps Rich's 16 standard colors to prompt_toolkit color names with the tuple ANSI_NAMES. prompt_toolkit's names for the last two are unusual: normal white (color 7, SGR 37) is ansigray, and bright white (color 15, SGR 97) is ansiwhite; it has no ansibrightwhite. The tuple used ansiwhite for color 7 (so a normal white was drawn as bright white) and ansibrightwhite for color 15, a name prompt_toolkit rejects.

Real results (prompt_toolkit.styles.base.ANSI_COLOR_NAMES contains ansigray and ansiwhite but not ansibrightwhite), on main and with this change:

rich_to_pt_color(Color.parse("white"))          main: ansiwhite                         after: ansigray
rich_to_pt_color(Color.parse("bright_white"))   main: ansibrightwhite                   after: ansiwhite
Style([("x", rich_to_pt_style("bright_white"))])  main: ValueError: Wrong color format 'ansibrightwhite'   after: ok
black and bright_black                          unchanged (ansiblack, ansibrightblack)

The fix changes those two entries of ANSI_NAMES and explains the odd names in a comment.

Tests: test_rich_to_pt_color_white_is_not_bright_white and test_rich_to_pt_color_standard_colors_are_valid (every standard color builds a valid prompt_toolkit Style) in tests/test_pt_utils.py; both fail on main and pass now. The full suite passes (3231 passed, 5 skipped) and ruff check and ruff format --check are clean on the two changed files. I did not run make check or make docs-test, and I did not edit any Markdown file (AGENTS.md asks to ask first).

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.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (4502b11) to head (f2d87e8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1863   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files          23       23           
  Lines        6777     6777           
=======================================
  Hits         6761     6761           
  Misses         16       16           
Flag Coverage Δ
unittests 99.76% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@tleonhardt

Copy link
Copy Markdown
Member

Thanks for the fix and the regression tests, @mokshupadhyay. I reproduced both issues through cmd2’s update_theme() on the base commit. Plain white selected bright white (foreground SGR 97), and bright_white raised ValueError when cmd2 built the prompt-toolkit style.

The two replacements in ANSI_NAMES are correct. Rich assigns white and bright_white to slots 7 and 15. prompt-toolkit calls those slots ansigray and ansiwhite. Rich standard colors · prompt-toolkit style strings

I checked all 16 colors through the same theme API on this branch. Every foreground and background matched Rich’s terminal codes, including 37/47 for white and 97/107 for bright_white. The exact-name test catches the intensity mix-up, and the all-colors test catches names that prompt-toolkit rejects. make test passed locally (3,233 passed, 3 skipped). Looks good to merge.

@tleonhardt
tleonhardt merged commit 17bdb4f into python-cmd2:main Dec 3, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants