prompt docs

This commit is contained in:
Will McGugan 2020-07-22 16:18:35 +01:00
parent 73de62b9fc
commit f7cd87f4ef
10 changed files with 105 additions and 17 deletions

View file

@ -18,6 +18,7 @@ Welcome to Rich's documentation!
markup.rst
tables.rst
panel.rst
prompt.rst
columns.rst
logging.rst
progress.rst

View file

@ -7,4 +7,18 @@ To draw a border around any content, construct a :class:`~rich.panel.Panel` with
from rich.panel import Panel
print(Panel("Hello, [red]World!"))
You can change the style of the panel by setting the ``box`` argument to the Panel constructor. See :ref:`appendix_box` for a list of available box styles.
You can change the style of the panel by setting the ``box`` argument to the Panel constructor. See :ref:`appendix_box` for a list of available box styles.
Panels will extend to the full width of the terminal. You can make panel *fit* the content why setting ``fit=True`` on the constructor, or by creating the Panel with :meth:`~rich.panel.Panel.fit`. For example::
from rich import print
from rich.panel import Panel
print(Panel.fit("Hello, [red]World!"))
The Panel constructor accepts a ``title`` argument which will draw a title within the panel::
from rich import print
from rich.panel import Panel
print(Panel("Hello, [red]World!", title="Welcome"))
See :class:`~rich.panel.Panel` for details how to customize Panels.

33
docs/source/prompt.rst Normal file
View file

@ -0,0 +1,33 @@
Prompt
======
Rich has a number :class:`~rich.prompt.Prompt` classes which ask a user for input and loop until a valid response is received. Here's a simple example::
>>> from rich.prompt import Prompt
>>> name = Prompt.ask("Enter your name")
The prompt may be given as a string (which may contain :ref:`console_markup` and emoji code) or as a :class:`~rich.text.Text` instance.
You can set a default value which will be returned if the user presses return without entering any text::
>>> from rich.prompt import Prompt
>>> name = Prompt.ask("Enter your name", default="Paul Atreides")
If you supply a list of choices, the prompt will loop until the user enters one of the choices::
>>> from rich.prompt import Prompt
>>> name = Prompt.ask("Enter your name", choices=["Paul", "Jessica", "Duncan"], default="Paul")
In addition to :class:`~rich.prompt.Prompt` which returns strings, you can also use :class:`~rich.prompt.IntPrompt` which asks the user for an integer, and :class:`~rich.prompt.FloatPrompt` for floats.
The :class:`~rich.prompt.Confirm` class is a specialized prompt which may be used to ask the user a simple yes / no question. Here's an example::
>>> from rich.prompt import Confirm
>>> is_rich_great = Confirm.ask("Do you like rich?")
>>> assert is_rich_great
The Prompt class was designed to be customizable via inheritance. See `prompt.py <https://github.com/willmcgugan/rich/blob/master/rich/prompt.py>`_ for examples.
To see some of the prompts in action, run the following command from the command line::
python -m rich.prompt