Skip to content

Add styles to theme for prompt-toolkit completion styles#1653

Open
tleonhardt wants to merge 20 commits intomainfrom
pt-colors
Open

Add styles to theme for prompt-toolkit completion styles#1653
tleonhardt wants to merge 20 commits intomainfrom
pt-colors

Conversation

@tleonhardt
Copy link
Copy Markdown
Member

@tleonhardt tleonhardt commented Apr 24, 2026

Add styles to theme for completion item and metadata foreground and background colors.

This is an attempt to provide higher-contrast color options and to make them configurable as part of a theme.

Added rich_to_pt_style and rich_to_pt_color conversion helper functions to convert from rich color/style objects to prompt-toolkit color/style strings.

  • For ColorSystem.STANDARD colors (0-15), it maps directly to prompt-toolkit's ansicolor names (e.g. ansired, ansibrightred). This ensures that prompt-toolkit delegates to the terminal's theme for the basic 16 colors.
  • For ColorSystem.EIGHT_BIT and ColorSystem.TRUECOLOR, it falls back to parsing the RGB hex code (e.g., #ff0000), which prompt-toolkit supports natively.

Also added the existing Cmd2Lexer colors to the styles and theme.

…round color

This is an attempt to provide higher-contrast color options and to make them configurable as part of a theme.
@tleonhardt tleonhardt requested a review from kmvanbrunt as a code owner April 24, 2026 23:23
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.50%. Comparing base (cc88ca6) to head (55785f5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1653      +/-   ##
==========================================
- Coverage   99.51%   99.50%   -0.02%     
==========================================
  Files          21       21              
  Lines        4737     4812      +75     
==========================================
+ Hits         4714     4788      +74     
- Misses         23       24       +1     
Flag Coverage Δ
unittests 99.50% <100.00%> (-0.02%) ⬇️

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

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

Also:
- Cache prompt-toolkit completion menu styles for efficiency
- Fixed color check bug related to default terminal style
@kmvanbrunt kmvanbrunt marked this pull request as draft April 26, 2026 03:55
BRIGHT_GREEN is one of the standard 16 colors typically configured by a user's terminal theme.

LIGHT_GREEN is from the extended 256-color range.
Created a new converter function to safely extract the correct string representation for prompt-toolkit:

- For `ColorSystem.STANDARD` colors (0-15), it maps directly to `prompt-toolkit`'s `ansicolor` names (e.g. `ansired`, `ansibrightred`). This ensures that `prompt-toolkit` delegates to the terminal's theme for the basic 16 colors.
- For `ColorSystem.EIGHT_BIT` and `ColorSystem.TRUECOLOR`, it falls back to parsing the RGB hex code (e.g., `#ff0000`), which `prompt-toolkit` supports natively.

Also:
- Renamed `to_pt_style` to `rich_to_pt_style` to make its intent more obvious
- Added tests for the new `rich_to_pt_color` conversion function
@tleonhardt tleonhardt marked this pull request as ready for review April 26, 2026 19:13
Comment thread cmd2/pt_utils.py Outdated
Comment thread cmd2/pt_utils.py
Comment thread cmd2/pt_utils.py
Comment thread cmd2/pt_utils.py
Comment thread cmd2/cmd2.py Outdated
Comment thread cmd2/cmd2.py Outdated
Comment thread cmd2/pt_utils.py
tleonhardt added 2 commits May 5, 2026 10:28
Since prommpt-toolkit calls the lex_document method of the Cmd2Lexer instance quite frequnetly, we don't want to dynamically fetech the colors to use on every call.

Instead I extracted the code for retrieving the lexer colors from the rich theme into a set_colors helper method and the set_theme method will call this for any Cmd2Lexer instances.
… styles

rich_to_pt_style should now support all styles in common between rich and prompt-toolkit
Comment thread cmd2/rich_utils.py Outdated
Comment thread cmd2/cmd2.py Outdated
@tleonhardt tleonhardt changed the title Add styles to theme for completion item and metadata colors Add styles to theme for prompt-toolkit completion styles May 5, 2026
@tleonhardt tleonhardt added this to the 4.0.0 milestone May 5, 2026
@tleonhardt tleonhardt self-assigned this May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants