Skip to content

Commit bbf8787

Browse files
committed
cmd2.Cmd now updates pt_style based on callback method registation
1 parent 633511d commit bbf8787

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

cmd2/cmd2.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,9 @@ def __init__(
528528
self._initialize_history(persistent_history_file)
529529

530530
# Cache for prompt_toolkit completion menu styles
531-
self._cached_pt_style: PtStyle | None = None
532-
self._cached_pt_style_params: tuple[StyleType, StyleType, StyleType, StyleType, StyleType] | None = None
531+
self.pt_style: PtStyle
532+
self.update_pt_style()
533+
ru.register_theme_update_callback(self.update_pt_style)
533534

534535
# Create the main PromptSession
535536
self.bottom_toolbar = bottom_toolbar
@@ -724,27 +725,22 @@ def _should_continue_multiline(self) -> bool:
724725
# No macro found or already processed. The statement is complete.
725726
return False
726727

727-
def _get_pt_style(self) -> "PtStyle":
728-
"""Return the cached prompt_toolkit style."""
728+
def update_pt_style(self) -> None:
729+
"""Update the cached prompt_toolkit style."""
729730
theme = ru.get_theme()
730731
rich_menu_style = theme.styles.get(Cmd2Style.COMPLETION_MENU, "")
731732
rich_completion_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_COMPLETION, "")
732733
rich_current_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_CURRENT, "")
733734
rich_meta_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_META, "")
734735
rich_meta_current_style = theme.styles.get(Cmd2Style.COMPLETION_MENU_META_CURRENT, "")
735736

736-
current_params = (rich_menu_style, rich_completion_style, rich_current_style, rich_meta_style, rich_meta_current_style)
737-
if self._cached_pt_style is not None and self._cached_pt_style_params == current_params:
738-
return self._cached_pt_style
739-
740737
menu_style = rich_to_pt_style(rich_menu_style)
741738
completion_style = rich_to_pt_style(rich_completion_style)
742739
current_style = rich_to_pt_style(rich_current_style)
743740
meta_style = rich_to_pt_style(rich_meta_style)
744741
meta_current_style = rich_to_pt_style(rich_meta_current_style)
745742

746-
self._cached_pt_style_params = current_params
747-
self._cached_pt_style = PtStyle.from_dict(
743+
self.pt_style = PtStyle.from_dict(
748744
{
749745
"completion-menu": menu_style,
750746
"completion-menu.completion": completion_style,
@@ -755,7 +751,9 @@ def _get_pt_style(self) -> "PtStyle":
755751
}
756752
)
757753

758-
return self._cached_pt_style
754+
def _get_pt_style(self) -> "PtStyle":
755+
"""Return the cached prompt_toolkit style."""
756+
return self.pt_style
759757

760758
def _create_main_session(self, auto_suggest: bool, completekey: str) -> PromptSession[str]:
761759
"""Create and return the main PromptSession for the application.

tests/test_cmd2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,7 @@ def do_orate(self, opts, arg) -> None:
18711871
self.stdout.write(arg + "\n")
18721872

18731873

1874-
def test_get_pt_style_caching(base_app) -> None:
1874+
def test_update_pt_style_caching(base_app) -> None:
18751875
# Get the initial style (populates the cache)
18761876
style1 = base_app._get_pt_style()
18771877

@@ -1917,6 +1917,7 @@ def test_get_pt_style_caching(base_app) -> None:
19171917

19181918
finally:
19191919
ru._APP_THEME = orig_theme
1920+
ru.set_theme()
19201921

19211922

19221923
@pytest.fixture

0 commit comments

Comments
 (0)