7878from prompt_toolkit .input import DummyInput , create_input
7979from prompt_toolkit .key_binding import KeyBindings
8080from prompt_toolkit .output import DummyOutput , create_output
81+ from prompt_toolkit .output .color_depth import ColorDepth
8182from prompt_toolkit .patch_stdout import patch_stdout
8283from prompt_toolkit .shortcuts import CompleteStyle , PromptSession , choice , set_title
84+ from prompt_toolkit .styles import DynamicStyle
85+ from prompt_toolkit .styles import Style as PtStyle
8386from rich .console import (
8487 Group ,
8588 JustifyMethod ,
@@ -192,6 +195,7 @@ def __init__(self, msg: str = "") -> None:
192195 Cmd2History ,
193196 Cmd2Lexer ,
194197 pt_filter_style ,
198+ rich_to_pt_style ,
195199)
196200from .utils import (
197201 Settable ,
@@ -523,6 +527,11 @@ def __init__(
523527 self ._persistent_history_length = persistent_history_length
524528 self ._initialize_history (persistent_history_file )
525529
530+ # Cache for prompt_toolkit completion menu styles
531+ self .pt_style : PtStyle
532+ self .update_pt_style ()
533+ ru .register_theme_update_callback (self .update_pt_style )
534+
526535 # Create the main PromptSession
527536 self .bottom_toolbar = bottom_toolbar
528537 self .main_session = self ._create_main_session (auto_suggest , completekey )
@@ -716,6 +725,36 @@ def _should_continue_multiline(self) -> bool:
716725 # No macro found or already processed. The statement is complete.
717726 return False
718727
728+ def update_pt_style (self ) -> None :
729+ """Update the cached prompt_toolkit style."""
730+ theme = ru .get_theme ()
731+ rich_menu_style = theme .styles .get (Cmd2Style .COMPLETION_MENU , "" )
732+ rich_completion_style = theme .styles .get (Cmd2Style .COMPLETION_MENU_COMPLETION , "" )
733+ rich_current_style = theme .styles .get (Cmd2Style .COMPLETION_MENU_CURRENT , "" )
734+ rich_meta_style = theme .styles .get (Cmd2Style .COMPLETION_MENU_META , "" )
735+ rich_meta_current_style = theme .styles .get (Cmd2Style .COMPLETION_MENU_META_CURRENT , "" )
736+
737+ menu_style = rich_to_pt_style (rich_menu_style )
738+ completion_style = rich_to_pt_style (rich_completion_style )
739+ current_style = rich_to_pt_style (rich_current_style )
740+ meta_style = rich_to_pt_style (rich_meta_style )
741+ meta_current_style = rich_to_pt_style (rich_meta_current_style )
742+
743+ self .pt_style = PtStyle .from_dict (
744+ {
745+ "completion-menu" : menu_style ,
746+ "completion-menu.completion" : completion_style ,
747+ "completion-menu.completion.current" : current_style ,
748+ "completion-menu.meta.completion" : meta_style ,
749+ "completion-menu.meta.completion.current" : meta_current_style ,
750+ "completion-menu.multi-column-meta" : meta_current_style ,
751+ }
752+ )
753+
754+ def _get_pt_style (self ) -> "PtStyle" :
755+ """Return the cached prompt_toolkit style."""
756+ return self .pt_style
757+
719758 def _create_main_session (self , auto_suggest : bool , completekey : str ) -> PromptSession [str ]:
720759 """Create and return the main PromptSession for the application.
721760
@@ -747,6 +786,7 @@ def _(event: Any) -> None: # pragma: no cover
747786 kwargs : dict [str , Any ] = {
748787 "auto_suggest" : AutoSuggestFromHistory () if auto_suggest else None ,
749788 "bottom_toolbar" : self .get_bottom_toolbar if self .bottom_toolbar else None ,
789+ "color_depth" : ColorDepth .TRUE_COLOR ,
750790 "complete_style" : CompleteStyle .MULTI_COLUMN ,
751791 "complete_in_thread" : True ,
752792 "complete_while_typing" : False ,
@@ -757,6 +797,7 @@ def _(event: Any) -> None: # pragma: no cover
757797 "multiline" : filters .Condition (self ._should_continue_multiline ),
758798 "prompt_continuation" : self .continuation_prompt ,
759799 "rprompt" : self .get_rprompt ,
800+ "style" : DynamicStyle (self ._get_pt_style ),
760801 }
761802
762803 if self .stdin .isatty () and self .stdout .isatty ():
@@ -3561,6 +3602,7 @@ def read_input(
35613602
35623603 temp_session : PromptSession [str ] = PromptSession (
35633604 auto_suggest = self .main_session .auto_suggest ,
3605+ color_depth = self .main_session .color_depth ,
35643606 complete_style = self .main_session .complete_style ,
35653607 complete_in_thread = self .main_session .complete_in_thread ,
35663608 complete_while_typing = self .main_session .complete_while_typing ,
@@ -3569,6 +3611,7 @@ def read_input(
35693611 key_bindings = self .main_session .key_bindings ,
35703612 input = self .main_session .input ,
35713613 output = self .main_session .output ,
3614+ style = self .main_session .style ,
35723615 )
35733616
35743617 return self ._read_raw_input (prompt , temp_session )
@@ -3585,8 +3628,10 @@ def read_secret(
35853628 :raises Exception: any other exceptions raised by prompt()
35863629 """
35873630 temp_session : PromptSession [str ] = PromptSession (
3631+ color_depth = self .main_session .color_depth ,
35883632 input = self .main_session .input ,
35893633 output = self .main_session .output ,
3634+ style = self .main_session .style ,
35903635 )
35913636
35923637 return self ._read_raw_input (prompt , temp_session , is_password = True )
0 commit comments