Usage¶
rich-prompt provides interactive terminal prompts. Users can navigate and select from a list of options.
All prompt classes follow the same API of rich.prompt.Prompt.
Prompts¶
Call the instance for a reusable prompt, or use the .ask() class method for a one-off prompt.
from richer_prompt import Select
# Reusable prompt instance
select_prompt = Select("Select a color:", choices=["Red", "Green", "Blue"])
choice = select_prompt()
# One-off prompt
choice = Select.ask("Select a color:", choices=["Red", "Green", "Blue"])
The prompt message may be given as a string
(which may contain Console Markup and emoji code)
or as a rich.text.Text instance.
Select.ask("[cyan]?[/cyan] Select a [i]color[/i]:", choices=["Red", "Green", "Blue"])
See the API Reference for details on each prompt class and their available options.
Choices¶
Use Choice objects in the choices list for more control over the display and formatting.
Choice.valueis the actual value returned when the choice is selected (can be any type that implements__str__).Choice.labelreplaces the text in the prompt display. (optional)Choice.descriptionadds secondary text to the choice. (optional)
from richer_prompt import Choice, Tabs
Tabs.ask(
"Do you want to continue?",
choices=[
Choice(False, label="No", description="Cancel and exit"),
Choice(True, label="Yes", description="This action cannot be undone"),
]
)
Themes¶
richer-prompt injects extra styles to the default Rich theme, so you can customize the appearance of prompts with Rich themes.
from rich.console import Console
from rich.theme import Theme
from richer_prompt import MultiSelect
theme = Theme({"richer_prompt.cursor": "blue bold", "richer_prompt.hint": "yellow italic"})
console = Console(theme=theme)
MultiSelect.ask(
"Select multiple options:",
choices=["Option 1", "Option 2", "Option 3"],
console=console
)
The following style names are available for customization:
Style name |
Default style |
Description |
|---|---|---|
|
|
Prompt title text. |
|
|
Prompt description text displayed under the title. |
|
|
Hint text shown at the bottom of prompts. |
|
null |
Base style for choice labels. |
|
|
Secondary text shown next to each choice. |
|
|
Active cursor indicator for the current option. |
|
|
Cursor indicator when focused on the submit row. |
|
null |
Base style for checkbox markers in multiselect prompts. |
|
|
Checkbox marker style for selected items. |
|
null |
Base style for tab labels. |
|
|
Style for the currently active tab. |