Debian Codegraph @

docs/features/prompt.md

raw symbols SCIP
1 # Prompt
2
3 `cmd2` issues a configurable prompt before soliciting user input.
4
5 ## Customizing the Prompt
6
7 This prompt can be configured by setting the [cmd2.Cmd.prompt][] instance attribute. This contains
8 the string which should be printed as a prompt for user input. See the
9 [getting_started.py](https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py)
10 example for the simple use case of statically setting the prompt.
11
12 ## Continuation Prompt
13
14 When a user types a [Multiline Command](./multiline_commands.md) it may span more than one line of
15 input. The prompt for the first line of input is specified by the `cmd2.Cmd.prompt` instance
16 attribute. The prompt for subsequent lines of input is defined by the `cmd2.Cmd.continuation_prompt`
17 attribute. See the
18 [getting_started.py](https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py)
19 example for a demonstration of customizing the continuation prompt.
20
21 ## Updating the prompt
22
23 If you wish to update the prompt between commands, you can do so using one of the
24 [Application Lifecycle Hooks](./hooks.md#application-lifecycle-hooks) such as a
25 [Postcommand hook](./hooks.md#postcommand-hooks). See
26 [python_scripting.py](https://github.com/python-cmd2/cmd2/blob/main/examples/python_scripting.py)
27 for an example of dynamically updating the prompt.
28
29 ## Asynchronous Feedback
30
31 `cmd2` provides a function to deliver asynchronous feedback to the user without interfering with the
32 command line. This allows feedback to be provided while the user is still entering text at the
33 prompt.
34
35 - [cmd2.Cmd.add_alert][]
36
37 ### Asynchronous Feedback Mechanisms
38
39 Alerts can interact with the CLI in two ways:
40
41 1. **Message Printing**: It can print a message directly above the current prompt line.
42 1. **Prompt Updates**: It can dynamically replace the text of the active prompt to reflect changing
43 state.
44
45 !!! note
46
47 To ensure the user interface remains accurate, a prompt update is ignored if the alert
48 was created before the current prompt was rendered. This prevents older alerts from overwriting a newer
49 prompt, though the alert's message will still be printed.
50
51 ### Terminal Window Management
52
53 `cmd2` also provides a function to change the title of the terminal window.
54
55 - [cmd2.Cmd.set_window_title][]
56
57 The easiest way to understand these functions is to see the
58 [async_printing.py](https://github.com/python-cmd2/cmd2/blob/main/examples/async_printing.py)
59 example for a demonstration.
60
61 ## Bottom Toolbar
62
63 `cmd2` supports an optional, persistent bottom toolbar that is always visible at the bottom of the
64 terminal window while the application is idle and waiting for input.
65
66 ### Enabling the Toolbar
67
68 To enable the toolbar, set `enable_bottom_toolbar=True` in the [cmd2.Cmd.__init__][] constructor:
69
70 ```py
71 class App(cmd2.Cmd):
72 def __init__(self):
73 super().__init__(enable_bottom_toolbar=True)
74 ```
75
76 ### Customizing Toolbar Content
77
78 You can customize the content of the toolbar by overriding the [cmd2.Cmd.get_bottom_toolbar][]
79 method.
80
81 ```py
82 from prompt_toolkit.formatted_text import AnyFormattedText
83
84 def get_bottom_toolbar(self) -> AnyFormattedText:
85 return [
86 ('ansigreen', 'My Application Name'),
87 ('', ' - '),
88 ('ansiyellow', 'Current Status: Idle'),
89 ]
90 ```
91
92 ### Refreshing the Toolbar
93
94 Since the toolbar is rendered by `prompt-toolkit` as part of the prompt, it is naturally redrawn
95 whenever the prompt is refreshed. If you want the toolbar to update automatically (for example, to
96 display a clock), you can set `refresh_interval` in the [cmd2.Cmd.__init__][] constructor to a value
97 greater than 0.0.
98
99 ```py
100 class App(cmd2.Cmd):
101 def __init__(self):
102 super().__init__(refresh_interval=0.5)
103 ```
104
105 See the
106 [getting_started.py](https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py)
107 example for a demonstration of this technique.