66 Callable ,
77 Iterable ,
88)
9+ from functools import lru_cache
910from typing import (
1011 TYPE_CHECKING ,
1112 Any ,
@@ -75,6 +76,7 @@ def pt_filter_style(text: str | ANSI) -> str | ANSI:
7576 return text if isinstance (text , ANSI ) else ANSI (text )
7677
7778
79+ @lru_cache (maxsize = 256 )
7880def rich_to_pt_color (color : "Color | None" ) -> str :
7981 """Convert a rich Color object to a prompt_toolkit color string."""
8082 if not color or color .is_default :
@@ -90,6 +92,7 @@ def rich_to_pt_color(color: "Color | None") -> str:
9092 return f"#{ c .red :02x} { c .green :02x} { c .blue :02x} "
9193
9294
95+ @lru_cache (maxsize = 1024 )
9396def rich_to_pt_style (rich_style : StyleType ) -> str :
9497 """Convert a rich Style object to a prompt_toolkit style string."""
9598 if not rich_style :
@@ -115,10 +118,8 @@ def rich_to_pt_style(rich_style: StyleType) -> str:
115118 if rich_style .blink is not None :
116119 parts .append ("blink" if rich_style .blink else "noblink" )
117120 if rich_style .reverse is not None :
118- # prompt-toolkit uses 'reverse'
119121 parts .append ("reverse" if rich_style .reverse else "noreverse" )
120122 if rich_style .conceal is not None :
121- # prompt-toolkit uses 'hidden' for Rich's 'conceal'
122123 parts .append ("hidden" if rich_style .conceal else "nohidden" )
123124 return " " .join (parts )
124125
@@ -296,11 +297,11 @@ def set_colors(self) -> None:
296297 """Update colors from the current rich theme."""
297298 # Retrieve styles dynamically from the current theme
298299 theme = ru .get_theme ()
299- self .command_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_COMMAND , "" ))
300- self .alias_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_ALIAS , "" ))
301- self .macro_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_MACRO , "" ))
302- self .flag_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_FLAG , "" ))
303- self .argument_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_ARGUMENT , "" ))
300+ self .command_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_COMMAND , Style . null () ))
301+ self .alias_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_ALIAS , Style . null () ))
302+ self .macro_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_MACRO , Style . null () ))
303+ self .flag_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_FLAG , Style . null () ))
304+ self .argument_color = rich_to_pt_style (theme .styles .get (Cmd2Style .LEXER_ARGUMENT , Style . null () ))
304305
305306 def lex_document (self , document : Document ) -> Callable [[int ], Any ]:
306307 """Lex the document."""
0 commit comments