Skip to content

Commit f10f42b

Browse files
committed
Change rich_to_pt_style to take a StyleType instead of Style
where StyleType = Style | str
1 parent 2bcda9a commit f10f42b

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

cmd2/cmd2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def __init__(
526526

527527
# Cache for prompt_toolkit completion menu styles
528528
self._cached_pt_style: PtStyle | None = None
529-
self._cached_pt_style_params: tuple[Style | None, Style | None] | None = None
529+
self._cached_pt_style_params: tuple[StyleType, StyleType] | None = None
530530

531531
# Create the main PromptSession
532532
self.bottom_toolbar = bottom_toolbar
@@ -724,8 +724,8 @@ def _should_continue_multiline(self) -> bool:
724724
def _get_pt_style(self) -> "PtStyle":
725725
"""Return the prompt_toolkit style for the completion menu."""
726726
theme = ru.get_theme()
727-
rich_item_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_ITEM)
728-
rich_meta_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_META)
727+
rich_item_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_ITEM, "")
728+
rich_meta_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_META, "")
729729

730730
current_params = (rich_item_style, rich_meta_style)
731731
if self._cached_pt_style is not None and self._cached_pt_style_params == current_params:

cmd2/pt_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from prompt_toolkit.formatted_text import ANSI
2121
from prompt_toolkit.history import History
2222
from prompt_toolkit.lexers import Lexer
23-
from rich.style import Style
23+
from rich.style import Style, StyleType
2424

2525
from . import (
2626
constants,
@@ -88,10 +88,14 @@ def rich_to_pt_color(color: "Color | None") -> str:
8888
return f"#{c.red:02x}{c.green:02x}{c.blue:02x}"
8989

9090

91-
def rich_to_pt_style(rich_style: Style | None) -> str:
91+
def rich_to_pt_style(rich_style: StyleType) -> str:
9292
"""Convert a rich Style object to a prompt_toolkit style string."""
9393
if not rich_style:
9494
return ""
95+
96+
if isinstance(rich_style, str):
97+
rich_style = Style.parse(rich_style)
98+
9599
parts = ["noreverse"]
96100

97101
fg_color = rich_to_pt_color(rich_style.color)

0 commit comments

Comments
 (0)