diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 599770983..48e437a0e 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -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) diff --git a/cmd2/pt_utils.py b/cmd2/pt_utils.py index 95ed1369c..a71e13204 100644 --- a/cmd2/pt_utils.py +++ b/cmd2/pt_utils.py @@ -6,6 +6,7 @@ Callable, Iterable, ) +from functools import lru_cache from typing import ( TYPE_CHECKING, Any, @@ -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: @@ -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) def rich_to_pt_style(rich_style: StyleType) -> str: """Convert a rich Style object to a prompt_toolkit style string.""" if not rich_style: @@ -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) @@ -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."""