3232import contextlib
3333import copy
3434import dataclasses
35+ import datetime
3536import functools
3637import glob
3738import inspect
7374from prompt_toolkit .application import create_app_session , get_app
7475from prompt_toolkit .auto_suggest import AutoSuggestFromHistory
7576from prompt_toolkit .completion import Completer , DummyCompleter
76- from prompt_toolkit .formatted_text import ANSI , FormattedText
77+ from prompt_toolkit .formatted_text import ANSI , AnyFormattedText
7778from prompt_toolkit .history import InMemoryHistory
7879from prompt_toolkit .input import DummyInput , create_input
7980from prompt_toolkit .key_binding import KeyBindings
@@ -367,7 +368,6 @@ def __init__(
367368 allow_redirection : bool = True ,
368369 auto_load_commands : bool = False ,
369370 auto_suggest : bool = True ,
370- bottom_toolbar : bool = False ,
371371 complete_in_thread : bool = True ,
372372 command_sets : Iterable [CommandSet [Any ]] | None = None ,
373373 include_ipy : bool = False ,
@@ -376,7 +376,7 @@ def __init__(
376376 multiline_commands : Iterable [str ] | None = None ,
377377 persistent_history_file : str = "" ,
378378 persistent_history_length : int = 1000 ,
379- refresh_interval : float = 0 ,
379+ refresh_interval : float = 0.0 ,
380380 shortcuts : Mapping [str , str ] | None = None ,
381381 silence_startup_script : bool = False ,
382382 startup_script : str = "" ,
@@ -405,7 +405,6 @@ def __init__(
405405 :param auto_suggest: If True, cmd2 will provide fish shell style auto-suggestions
406406 based on history. User can press right-arrow key to accept the
407407 provided suggestion.
408- :param bottom_toolbar: if ``True``, then a bottom toolbar will be displayed.
409408 :param complete_in_thread: if ``True``, then completion will run in a separate thread.
410409 :param command_sets: Provide CommandSet instances to load during cmd2 initialization.
411410 This allows CommandSets with custom constructor parameters to be
@@ -418,7 +417,7 @@ def __init__(
418417 :param persistent_history_file: file path to load a persistent cmd2 command history from
419418 :param persistent_history_length: max number of history items to write
420419 to the persistent history file
421- :param refresh_interval: How often, in seconds, to refresh the UI. Defaults to 0.
420+ :param refresh_interval: How often, in seconds, to refresh the UI. Defaults to 0.0.
422421 prompt-toolkit already refreshes the UI every time a key is pressed.
423422 Set this value if you need the UI to update automatically without
424423 user input (e.g., for displaying a clock or background status
@@ -535,7 +534,6 @@ def __init__(
535534 self ._initialize_history (persistent_history_file )
536535
537536 # Create the main PromptSession
538- self .bottom_toolbar = bottom_toolbar
539537 self .complete_in_thread = complete_in_thread
540538 self .refresh_interval = refresh_interval
541539 self .main_session = self ._create_main_session (auto_suggest , completekey )
@@ -759,7 +757,7 @@ def _(event: Any) -> None: # pragma: no cover
759757 # Base configuration
760758 kwargs : dict [str , Any ] = {
761759 "auto_suggest" : AutoSuggestFromHistory () if auto_suggest else None ,
762- "bottom_toolbar" : self .get_bottom_toolbar if self . bottom_toolbar else None ,
760+ "bottom_toolbar" : self .get_bottom_toolbar ,
763761 "color_depth" : ColorDepth .TRUE_COLOR ,
764762 "complete_style" : CompleteStyle .MULTI_COLUMN ,
765763 "complete_in_thread" : self .complete_in_thread ,
@@ -1983,49 +1981,35 @@ def ppretty(
19831981 end = end ,
19841982 )
19851983
1986- def get_bottom_toolbar (self ) -> list [ str | tuple [ str , str ]] | None :
1984+ def get_bottom_toolbar (self ) -> AnyFormattedText :
19871985 """Get the bottom toolbar content.
19881986
1989- Returns None if `self.bottom_toolbar` is False. Otherwise, returns a
1990- list of tokens to populate the toolbar (which can span multiple lines).
1987+ This method is intended to be called by prompt-toolkit as a UI lifecycle callback.
1988+ Because prompt-toolkit triggers this callback on every UI refresh (e.g., on every
1989+ keypress and at scheduled refresh intervals), keeping this function highly optimized
1990+ is critical to ensuring the CLI remains responsive.
19911991
1992- NOTE: prompt-toolkit calls this method on every UI refresh (e.g., on every keypress
1993- and at scheduled refresh intervals). To ensure the CLI remains responsive, keep
1994- this function highly optimized.
1995- """
1996- if not self .bottom_toolbar :
1997- return None
1998-
1999- import datetime
2000- import shutil
1992+ Override this if you want a bottom toolbar displaying contextual information useful for
1993+ your application. This could be information like the application name, current state,
1994+ or even a real-time clock.
20011995
2002- # Get the current time in ISO format with 0.01s precision
2003- dt = datetime .datetime .now (datetime .timezone .utc ).astimezone ()
2004- now = dt .strftime ("%Y-%m-%dT%H:%M:%S.%f" )[:- 4 ] + dt .strftime ("%z" )
2005- left_text = sys .argv [0 ]
2006-
2007- # Get terminal width to calculate padding for right-alignment
2008- cols , _ = shutil .get_terminal_size ()
2009- padding_size = cols - len (left_text ) - len (now ) - 1
2010- if padding_size < 1 :
2011- padding_size = 1
2012- padding = " " * padding_size
1996+ :return: Content to populate the bottom toolbar, or None to hide it.
1997+ """
1998+ return None
20131999
2014- # Return formatted text for prompt-toolkit
2015- return [
2016- ("ansigreen" , left_text ),
2017- ("" , padding ),
2018- ("ansicyan" , now ),
2019- ]
2000+ def get_rprompt (self ) -> AnyFormattedText :
2001+ """Provide text to populate the prompt-toolkit right prompt.
20202002
2021- def get_rprompt (self ) -> str | FormattedText | None :
2022- """Provide text to populate prompt-toolkit right prompt with.
2003+ This method is intended to be called by prompt-toolkit as a UI lifecycle callback.
2004+ Because prompt-toolkit triggers this callback on every UI refresh (e.g., on every
2005+ keypress and at scheduled refresh intervals), keeping this function highly optimized
2006+ is critical to ensuring the CLI remains responsive.
20232007
2024- Override this if you want a right-prompt displaying contetual information useful for your application.
2025- This could be information like current Git branch, time, current working directory, etc that is displayed
2026- without cluttering the main input area.
2008+ Override this if you want a right-prompt displaying contextual information useful for
2009+ your application. This could be information like the current Git branch, time, or current
2010+ working directory that is displayed without cluttering the main input area.
20272011
2028- :return: any type of formatted text to display as the right prompt
2012+ :return: Content to populate the right prompt, or None to hide it.
20292013 """
20302014 return None
20312015
@@ -2932,8 +2916,6 @@ def onecmd_plus_hooks(
29322916 command's stdout.
29332917 :return: True if running of commands should stop
29342918 """
2935- import datetime
2936-
29372919 stop = False
29382920 statement = None
29392921
0 commit comments