3232class Cmd2Completer (Completer ):
3333 """Completer that delegates to cmd2's completion logic."""
3434
35- def __init__ (self , cmd_app : 'Cmd' , custom_settings : utils .CustomCompletionSettings | None = None ) -> None :
35+ def __init__ (
36+ self ,
37+ cmd_app : 'Cmd' ,
38+ custom_settings : utils .CustomCompletionSettings | None = None ,
39+ ) -> None :
3640 """Initialize prompt_toolkit based completer class."""
3741 self .cmd_app = cmd_app
3842 self .custom_settings = custom_settings
@@ -168,10 +172,31 @@ def store_string(self, string: str) -> None:
168172class Cmd2Lexer (Lexer ):
169173 """Lexer that highlights cmd2 command names, aliases, and macros."""
170174
171- def __init__ (self , cmd_app : 'Cmd' ) -> None :
172- """Initialize the lexer."""
175+ def __init__ (
176+ self ,
177+ cmd_app : 'Cmd' ,
178+ command_color : str = 'ansigreen' ,
179+ alias_color : str = 'ansicyan' ,
180+ macro_color : str = 'ansimagenta' ,
181+ flag_color : str = 'ansired' ,
182+ argument_color : str = 'ansiyellow' ,
183+ ) -> None :
184+ """Initialize the Lexer.
185+
186+ :param cmd_app: cmd2.Cmd instance
187+ :param command_color: color to use for commands, defaults to 'ansigreen'
188+ :param alias_color: color to use for aliases, defaults to 'ansicyan'
189+ :param macro_color: color to use for macros, defaults to 'ansimagenta'
190+ :param flag_color: color to use for flags, defaults to 'ansired'
191+ :param argument_color: color to use for arguments, defaults to 'ansiyellow'
192+ """
173193 super ().__init__ ()
174194 self .cmd_app = cmd_app
195+ self .command_color = command_color
196+ self .alias_color = alias_color
197+ self .macro_color = macro_color
198+ self .flag_color = flag_color
199+ self .argument_color = argument_color
175200
176201 def lex_document (self , document : Document ) -> Callable [[int ], Any ]:
177202 """Lex the document."""
@@ -195,16 +220,30 @@ def get_line(lineno: int) -> list[tuple[str, str]]:
195220
196221 if command :
197222 # Determine the style for the command
198- style = ''
199- if command in self .cmd_app .get_all_commands ():
200- style = 'ansigreen'
201- elif command in self .cmd_app .aliases :
202- style = 'ansicyan'
203- elif command in self .cmd_app .macros :
204- style = 'ansimagenta'
205-
206- # Add the command with the determined style
207- tokens .append ((style , command ))
223+ shortcut_found = False
224+ for shortcut , _ in self .cmd_app .statement_parser .shortcuts :
225+ if command .startswith (shortcut ):
226+ # Add the shortcut with the command style
227+ tokens .append ((self .command_color , shortcut ))
228+
229+ # If there's more in the command word, it's an argument
230+ if len (command ) > len (shortcut ):
231+ tokens .append ((self .argument_color , command [len (shortcut ) :]))
232+
233+ shortcut_found = True
234+ break
235+
236+ if not shortcut_found :
237+ style = ''
238+ if command in self .cmd_app .get_all_commands ():
239+ style = self .command_color
240+ elif command in self .cmd_app .aliases :
241+ style = self .alias_color
242+ elif command in self .cmd_app .macros :
243+ style = self .macro_color
244+
245+ # Add the command with the determined style
246+ tokens .append ((style , command ))
208247
209248 # Add the rest of the line
210249 if cmd_end < len (line ):
@@ -224,9 +263,9 @@ def get_line(lineno: int) -> list[tuple[str, str]]:
224263 if space :
225264 tokens .append (('' , text ))
226265 elif flag :
227- tokens .append (('ansired' , text ))
266+ tokens .append ((self . flag_color , text ))
228267 elif (quoted or word ) and text not in exclude_tokens :
229- tokens .append (('ansiyellow' , text ))
268+ tokens .append ((self . argument_color , text ))
230269 else :
231270 tokens .append (('' , text ))
232271 elif line :
0 commit comments