Prompt Styling
ItsPrompt uses prompt_toolkit for its prompts. This module not only provides an easy way to interact with the command line, but also a full set of styling features.
You can learn more about the available styling features in the Styling Documentation of prompt_toolkit.
ItsPrompt makes it a bit easier for you to style each component of a prompt. For every component, we give a separate attribute in the PromptStyle class, which you can style with valid prompt_toolkit styling:
# examples for the different styling class components
Prompt.raw_select(
question='question',
options=(
'option',
'selected_option',
)
)
Prompt.input(
question='question',
default='grayout',
validate=lambda x: 'error',
)
styling tag |
default style |
default design |
|---|---|---|
question_mark |
fg:ansigreen |
|
question |
* |
|
option |
* |
|
selected_option |
fg:ansicyan |
|
tooltip |
fg:ansibrightblue bg:ansiwhite bold |
|
text |
* |
|
grayout |
fg:ansibrightblack |
|
error |
fg:ansiwhite bg:ansired bold |
|
separator |
fg:ansibrightgreen |
|
Note
*These values are not changed from the default prompt_toolkit values.
To create your own style, there are two ways:
Changing the Default Style
To change the default style, you need to import the default_style and change its values:
from ItsPrompt.data.style import default_style
default_style.error = 'fg:ansired bg:ansiwhite'
This will automatically change the style of all prompts, which do not have an own style defined.
Creating your own Style
To define your own style for a specific prompt, import PromptStyle and create an object.
Then assign it to the style argument of any prompt.
from ItsPrompt.data.style import PromptStyle
my_style = PromptStyle(
question_mark='fg:ansiblue',
error='fg:ansired bg:ansiwhite',
)
Note
If you omit a style attribute of your PromptStyle, this attribute will not use
the one given by the default_style. Instead, it will use the default style given by
prompt_toolkit.
If you want to create your own style from the default_style, you can use the
create_from_default() method:
from ItsPrompt.data.style import create_from_default
my_style = create_from_default()
my_style.error = 'fg:ansired bg:ansiwhite'
This will create a copy of the default_style and change its error attribute. All other
attributes will remain the same as the default_style.
Note
Warning! Not copying the default style and changing it instead will result in all prompts using your changes, as a variable is by default not a copy, but a reference to the same object!