Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,11 @@ def _should_continue_multiline(self) -> bool:
def update_pt_style(self) -> None:
"""Update the cached prompt_toolkit style."""
theme = ru.get_theme()
rich_menu_style = theme.styles.get(Cmd2Style.COMPLETION_MENU, "")
rich_completion_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_COMPLETION, "")
rich_current_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_CURRENT, "")
rich_meta_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_META, "")
rich_meta_current_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_META_CURRENT, "")
rich_menu_style = theme.styles.get(Cmd2Style.COMPLETION_MENU, Style.null())
rich_completion_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_COMPLETION, Style.null())
rich_current_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_CURRENT, Style.null())
rich_meta_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_META, Style.null())
rich_meta_current_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_META_CURRENT, Style.null())

menu_style = rich_to_pt_style(rich_menu_style)
completion_style = rich_to_pt_style(rich_completion_style)
Expand Down
15 changes: 8 additions & 7 deletions cmd2/pt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Callable,
Iterable,
)
from functools import lru_cache
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -75,6 +76,7 @@ def pt_filter_style(text: str | ANSI) -> str | ANSI:
return text if isinstance(text, ANSI) else ANSI(text)


@lru_cache(maxsize=256)
def rich_to_pt_color(color: "Color | None") -> str:
"""Convert a rich Color object to a prompt_toolkit color string."""
if not color or color.is_default:
Expand All @@ -90,6 +92,7 @@ def rich_to_pt_color(color: "Color | None") -> str:
return f"#{c.red:02x}{c.green:02x}{c.blue:02x}"


@lru_cache(maxsize=1024)
Comment thread
kmvanbrunt marked this conversation as resolved.
def rich_to_pt_style(rich_style: StyleType) -> str:
"""Convert a rich Style object to a prompt_toolkit style string."""
if not rich_style:
Expand All @@ -115,10 +118,8 @@ def rich_to_pt_style(rich_style: StyleType) -> str:
if rich_style.blink is not None:
parts.append("blink" if rich_style.blink else "noblink")
if rich_style.reverse is not None:
# prompt-toolkit uses 'reverse'
parts.append("reverse" if rich_style.reverse else "noreverse")
if rich_style.conceal is not None:
# prompt-toolkit uses 'hidden' for Rich's 'conceal'
parts.append("hidden" if rich_style.conceal else "nohidden")
return " ".join(parts)

Expand Down Expand Up @@ -296,11 +297,11 @@ def set_colors(self) -> None:
"""Update colors from the current rich theme."""
# Retrieve styles dynamically from the current theme
theme = ru.get_theme()
self.command_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_COMMAND, ""))
self.alias_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_ALIAS, ""))
self.macro_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_MACRO, ""))
self.flag_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_FLAG, ""))
self.argument_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_ARGUMENT, ""))
self.command_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_COMMAND, Style.null()))
self.alias_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_ALIAS, Style.null()))
self.macro_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_MACRO, Style.null()))
self.flag_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_FLAG, Style.null()))
self.argument_color = rich_to_pt_style(theme.styles.get(Cmd2Style.LEXER_ARGUMENT, Style.null()))

def lex_document(self, document: Document) -> Callable[[int], Any]:
"""Lex the document."""
Expand Down
Loading