@@ -526,13 +526,10 @@ def __init__(
526526 self ._persistent_history_length = persistent_history_length
527527 self ._initialize_history (persistent_history_file )
528528
529- # Styles used in prompt_toolkit elements like completion menus.
530- # This is initialized when first needed for rendering and is only
531- # updated when the application theme changes. self._pt_style_key
532- # is a tuple of the Rich styles used to build self._pt_style and
533- # acts as a cache key to detect when the theme has changed.
534- self ._pt_style : PtStyle | None = None
535- self ._pt_style_key : tuple [Style , ...] = ()
529+ # Cache for prompt_toolkit completion menu styles
530+ self .pt_style : PtStyle
531+ self .update_pt_style ()
532+ ru .register_theme_update_callback (self .update_pt_style )
536533
537534 # Create the main PromptSession
538535 self .bottom_toolbar = bottom_toolbar
@@ -727,39 +724,35 @@ def _should_continue_multiline(self) -> bool:
727724 # No macro found or already processed. The statement is complete.
728725 return False
729726
730- def _get_pt_style (self ) -> PtStyle :
731- """Return the prompt_toolkit style synchronized with the application theme ."""
727+ def update_pt_style (self ) -> None :
728+ """Update the cached prompt_toolkit style."""
732729 theme = ru .get_theme ()
733-
734- completion_menu = theme .styles .get (Cmd2Style .COMPLETION_MENU , Style .null ())
735- completion_menu_completion = theme .styles .get (Cmd2Style .COMPLETION_MENU_COMPLETION , Style .null ())
736- completion_menu_current = theme .styles .get (Cmd2Style .COMPLETION_MENU_CURRENT , Style .null ())
737- completion_menu_meta = theme .styles .get (Cmd2Style .COMPLETION_MENU_META , Style .null ())
738- completion_menu_meta_current = theme .styles .get (Cmd2Style .COMPLETION_MENU_META_CURRENT , Style .null ())
739-
740- current_key = (
741- completion_menu ,
742- completion_menu_completion ,
743- completion_menu_current ,
744- completion_menu_meta ,
745- completion_menu_meta_current ,
730+ rich_menu_style = theme .styles .get (Cmd2Style .COMPLETION_MENU , Style .null ())
731+ rich_completion_style = theme .styles .get (Cmd2Style .COMPLETION_MENU_COMPLETION , Style .null ())
732+ rich_current_style = theme .styles .get (Cmd2Style .COMPLETION_MENU_CURRENT , Style .null ())
733+ rich_meta_style = theme .styles .get (Cmd2Style .COMPLETION_MENU_META , Style .null ())
734+ rich_meta_current_style = theme .styles .get (Cmd2Style .COMPLETION_MENU_META_CURRENT , Style .null ())
735+
736+ menu_style = rich_to_pt_style (rich_menu_style )
737+ completion_style = rich_to_pt_style (rich_completion_style )
738+ current_style = rich_to_pt_style (rich_current_style )
739+ meta_style = rich_to_pt_style (rich_meta_style )
740+ meta_current_style = rich_to_pt_style (rich_meta_current_style )
741+
742+ self .pt_style = PtStyle .from_dict (
743+ {
744+ "completion-menu" : menu_style ,
745+ "completion-menu.completion" : completion_style ,
746+ "completion-menu.completion.current" : current_style ,
747+ "completion-menu.meta.completion" : meta_style ,
748+ "completion-menu.meta.completion.current" : meta_current_style ,
749+ "completion-menu.multi-column-meta" : meta_current_style ,
750+ }
746751 )
747752
748- if self ._pt_style is None or current_key != self ._pt_style_key :
749- self ._pt_style_key = current_key
750-
751- self ._pt_style = PtStyle .from_dict (
752- {
753- "completion-menu" : rich_to_pt_style (completion_menu ),
754- "completion-menu.completion" : rich_to_pt_style (completion_menu_completion ),
755- "completion-menu.completion.current" : rich_to_pt_style (completion_menu_current ),
756- "completion-menu.meta.completion" : rich_to_pt_style (completion_menu_meta ),
757- "completion-menu.meta.completion.current" : rich_to_pt_style (completion_menu_meta_current ),
758- "completion-menu.multi-column-meta" : rich_to_pt_style (completion_menu_meta_current ),
759- }
760- )
761-
762- return self ._pt_style
753+ def _get_pt_style (self ) -> "PtStyle" :
754+ """Return the cached prompt_toolkit style."""
755+ return self .pt_style
763756
764757 def _create_main_session (self , auto_suggest : bool , completekey : str ) -> PromptSession [str ]:
765758 """Create and return the main PromptSession for the application.
0 commit comments