11"""Utilities for integrating prompt_toolkit with cmd2."""
22
33import re
4- import weakref
54from collections .abc import (
65 Callable ,
76 Iterable ,
@@ -111,16 +110,20 @@ def rich_to_pt_style(rich_style: StyleType) -> str:
111110
112111 if rich_style .bold is not None :
113112 parts .append ("bold" if rich_style .bold else "nobold" )
114- if rich_style .italic is not None :
115- parts .append ("italic" if rich_style .italic else "noitalic" )
116113 if rich_style .underline is not None :
117114 parts .append ("underline" if rich_style .underline else "nounderline" )
115+ if rich_style .strike is not None :
116+ parts .append ("strike" if rich_style .strike else "nostrike" )
117+ if rich_style .italic is not None :
118+ parts .append ("italic" if rich_style .italic else "noitalic" )
118119 if rich_style .blink is not None :
119120 parts .append ("blink" if rich_style .blink else "noblink" )
120121 if rich_style .reverse is not None :
121122 parts .append ("reverse" if rich_style .reverse else "noreverse" )
122123 if rich_style .conceal is not None :
123124 parts .append ("hidden" if rich_style .conceal else "nohidden" )
125+ if rich_style .dim is not None :
126+ parts .append ("dim" if rich_style .dim else "nodim" )
124127 return " " .join (parts )
125128
126129
@@ -264,21 +267,16 @@ def clear(self) -> None:
264267 self ._loaded_strings .clear ()
265268
266269
267- _lexers : "weakref.WeakSet[Cmd2Lexer]" = weakref .WeakSet ()
268-
269-
270- def _update_lexer_colors () -> None :
271- """Update colors for all active lexers."""
272- for lexer in _lexers :
273- lexer .set_colors ()
274-
275-
276- ru .register_theme_update_callback (_update_lexer_colors )
277-
278-
279270class Cmd2Lexer (Lexer ):
280271 """Lexer that highlights cmd2 command names, aliases, and macros."""
281272
273+ # Use the 'class:' prefix to look up styles in the prompt-toolkit theme
274+ COMMAND_STYLE = f"class:{ Cmd2Style .LEXER_COMMAND } "
275+ ALIAS_STYLE = f"class:{ Cmd2Style .LEXER_ALIAS } "
276+ MACRO_STYLE = f"class:{ Cmd2Style .LEXER_MACRO } "
277+ FLAG_STYLE = f"class:{ Cmd2Style .LEXER_FLAG } "
278+ ARGUMENT_STYLE = f"class:{ Cmd2Style .LEXER_ARGUMENT } "
279+
282280 def __init__ (
283281 self ,
284282 cmd_app : "Cmd" ,
@@ -290,19 +288,6 @@ def __init__(
290288 super ().__init__ ()
291289 self ._cmd_app = cmd_app
292290
293- _lexers .add (self )
294- self .set_colors ()
295-
296- def set_colors (self ) -> None :
297- """Update colors from the current rich theme."""
298- # Retrieve styles dynamically from the current theme
299- theme = ru .get_theme ()
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 ()))
305-
306291 def lex_document (self , document : Document ) -> Callable [[int ], Any ]:
307292 """Lex the document."""
308293 # Get redirection tokens and terminators to avoid highlighting them as values
@@ -319,9 +304,9 @@ def highlight_args(text: str, tokens: list[tuple[str, str]]) -> None:
319304 if space :
320305 tokens .append (("" , match_text ))
321306 elif flag :
322- tokens .append ((self .flag_color , match_text ))
307+ tokens .append ((self .FLAG_STYLE , match_text ))
323308 elif (quoted or word ) and match_text not in exclude_tokens :
324- tokens .append ((self .argument_color , match_text ))
309+ tokens .append ((self .ARGUMENT_STYLE , match_text ))
325310 else :
326311 tokens .append (("" , match_text ))
327312
@@ -355,23 +340,23 @@ def get_line(lineno: int) -> list[tuple[str, str]]:
355340 for shortcut , _ in self ._cmd_app .statement_parser .shortcuts :
356341 if command .startswith (shortcut ):
357342 # Add the shortcut with the command style
358- tokens .append ((self .command_color , shortcut ))
343+ tokens .append ((self .COMMAND_STYLE , shortcut ))
359344
360345 # If there's more in the command word, it's an argument
361346 if len (command ) > len (shortcut ):
362- tokens .append ((self .argument_color , command [len (shortcut ) :]))
347+ tokens .append ((self .ARGUMENT_STYLE , command [len (shortcut ) :]))
363348
364349 shortcut_found = True
365350 break
366351
367352 if not shortcut_found :
368353 style = ""
369354 if command in self ._cmd_app .get_all_commands ():
370- style = self .command_color
355+ style = self .COMMAND_STYLE
371356 elif command in self ._cmd_app .aliases :
372- style = self .alias_color
357+ style = self .ALIAS_STYLE
373358 elif command in self ._cmd_app .macros :
374- style = self .macro_color
359+ style = self .MACRO_STYLE
375360
376361 # Add the command with the determined style
377362 tokens .append ((style , command ))
0 commit comments