API Documentation
A Python library of interactive CLI elements you have been looking for
Class DefaultKeys
class DefaultKeys()
A map of default keybindings.
Attributes:
escape(List[Union[Tuple[int, ...], str]]): Keys that escape the current context. select(List[Union[Tuple[int, ...], str]]): Keys that trigger list element selection. confirm(List[Union[Tuple[int, ...], str]]): Keys that trigger list confirmation. backspace(List[Union[Tuple[int, ...], str]]): Keys that trigger deletion of the previous character. delete(List[Union[Tuple[int, ...], str]]): Keys that trigger deletion of the next character. down(List[Union[Tuple[int, ...], str]]): Keys that select the element below. up(List[Union[Tuple[int, ...], str]]): Keys that select the element above. left(List[Union[Tuple[int, ...], str]]): Keys that select the element to the left. right(List[Union[Tuple[int, ...], str]]): Keys that select the element to the right. home(List[Union[Tuple[int, ...], str]]): Keys that move to the beginning of the context. end(List[Union[Tuple[int, ...], str]]): Keys that move to the end of the context.
Class Config
class Config()
A map of default configuration
Attributes:
raise_on_interrupt(bool)
- If True, functions will raise KeyboardInterrupt whenever one is encountered when waiting for input, otherwise, they will return some sane alternative to their usual return. Forselect
,prompt
andconfirm
this meansNone
, while forselect_multiple
it means an empty list -[]
. Defaults to False.raise_on_escape(bool)
- If True, functions will raise Abort whenever the escape key is encountered when waiting for input, otherwise, they will return some sane alternative to their usual return. Forselect
,prompt
andconfirm
this meansNone
, while forselect_multiple
it means an empty list -[]
. Defaults to False.transient(bool)
- If False, elements will remain displayed after its context has ended. Defaults to True.
prompt
def prompt(
prompt: str,
target_type: Type[TargetType] = str,
validator: Callable[[TargetType], bool] = lambda input: True,
secure: bool = False,
raise_validation_fail: bool = True,
raise_type_conversion_fail: bool = True,
initial_value: Optional[str] = None,
completion: Optional[Callable[[str], List[str]]] = None) -> TargetType
Function that prompts the user for written input
Arguments:
prompt
str - The prompt that will be displayedtarget_type
Union[Type[T], Type[str]], optional - Type to convert the answer to. Defaults to str.validator
Callable[[Any], bool], optional - Optional function to validate the input. Defaults to lambda input: True.secure
bool, optional - If True, input will be hidden. Defaults to False.raise_validation_fail
bool, optional - If True, invalid inputs will raiserich.internals.ValidationError
, else the error will be reported onto the console. Defaults to True.raise_type_conversion_fail
bool, optional - If True, invalid inputs will raiserich.internals.ConversionError
, else the error will be reported onto the console. Defaults to True.initial_value
str, optional - If present, the value is placed in the prompt as the default value.
Raises:
ValidationError
- Raised if validation with provided validator failsConversionError
- Raised if the value cannot be converted to provided typeKeyboardInterrupt
- Raised when keyboard interrupt is encountered and Config.raise_on_interrupt is True
Returns:
Union[T, str]: Returns a value formatted as provided type or string if no type is provided
select
def select(options: List[Union[Tuple[int, ...], str]],
preprocessor: Callable[[Any], Any] = lambda val: val,
cursor: str = '>',
cursor_style: str = 'pink1',
cursor_index: int = 0,
return_index: bool = False,
strict: bool = False,
pagination: bool = False,
page_size: int = 5) -> Union[int, Any, None]
A prompt that allows selecting one option from a list of options
Arguments:
options
List[Union[Tuple[int, ...], str]] - A list of options to select frompreprocessor
Callable[[Any], Any] - A callable that can be used to preprocess the list of options prior to printing. For example, if you passed aPerson
object withname
attribute, preprocessor could belambda person: person.name
to just show the content ofname
attribute in the select dialog. Defaults tolambda val: val
cursor
str, optional - Cursor that is going to appear in front of currently selected option. Defaults to '> '.cursor_style
str, optional - Rich friendly style for the cursor. Defaults to 'pink1'.cursor_index
int, optional - Option can be preselected based on its list index. Defaults to 0.return_index
bool, optional - IfTrue
,select
will return the index of selected element in options. Defaults toFalse
.strict
bool, optional - If emptyoptions
is provided and strict isFalse
, None will be returned, if it'sTrue
,ValueError
will be thrown. Defaults to False.pagination
bool, optional - IfTrue
, pagination will be used. Defaults to False.page_size
int, optional - Number of options to show on a single page if pagination is enabled. Defaults to 5.
Raises:
ValueError
- Thrown if nooptions
are provided and strict isTrue
KeyboardInterrupt
- Raised when keyboard interrupt is encountered and Config.raise_on_interrupt is True
Returns:
Union[int, str, None]: Selected value or the index of a selected option or None
select_multiple
def select_multiple(options: List[Union[Tuple[int, ...], str]],
preprocessor: Callable[[Any], Any] = lambda val: val,
tick_character: str = '✓',
tick_style: str = 'pink1',
cursor_style: str = 'pink1',
ticked_indices: Optional[List[int]] = None,
cursor_index: int = 0,
minimal_count: int = 0,
maximal_count: Optional[int] = None,
return_indices: bool = False,
strict: bool = False,
pagination: bool = False,
page_size: int = 5) -> List[Union[int, Any]]
A prompt that allows selecting multiple options from a list of options
Arguments:
options
List[Union[Tuple[int, ...], str]] - A list of options to select frompreprocessor
Callable[[Any], Any] - A callable that can be used to preprocess the list of options prior to printing. For example, if you passed aPerson
object withname
attribute, preprocessor could belambda person: person.name
to just show the content ofname
attribute in the select dialog. Defaults tolambda val: val
tick_character
str, optional - Character that will be used as a tick in a checkbox. Defaults to 'x'.tick_style
str, optional - Rich friendly style for the tick character. Defaults to 'pink1'.cursor_style
str, optional - Rich friendly style for the option when the cursor is currently on it. Defaults to 'pink1'.ticked_indices
Optional[List[int]], optional - Indices of options that are pre-ticked when the prompt appears. Defaults to None.cursor_index
int, optional - Index of the option cursor starts at. Defaults to 0.minimal_count
int, optional - Minimal count of options that need to be selected. Defaults to 0.maximal_count
Optional[int], optional - Maximal count of options that need to be selected. Defaults to None.return_indices
bool, optional - IfTrue
,select_multiple
will return the indices of ticked elements in options. Defaults toFalse
.strict
bool, optional - If emptyoptions
is provided and strict isFalse
, None will be returned, if it'sTrue
,ValueError
will be thrown. Defaults to False.pagination
bool, optional - IfTrue
, pagination will be used. Defaults to False.page_size
int, optional - Number of options to show on a single page if pagination is enabled. Defaults to 5.
Raises:
KeyboardInterrupt
- Raised when keyboard interrupt is encountered and Config.raise_on_interrupt is True
Returns:
Union[List[str], List[int]]: A list of selected values or indices of selected options
confirm
def confirm(question: str,
yes_text: str = 'Yes',
no_text: str = 'No',
has_to_match_case: bool = False,
enter_empty_confirms: bool = True,
default_is_yes: bool = False,
cursor: str = '>',
cursor_style: str = 'pink1',
char_prompt: bool = True) -> Optional[bool]
A prompt that asks a question and offers two responses
Arguments:
question
str - Question to be askedyes_text
str, optional - Text of the positive response. Defaults to 'Yes'.no_text
str, optional - Text of the negative response. Defaults to 'No'.has_to_match_case
bool, optional - Check if typed response matches case. Defaults to False.enter_empty_confirms
bool, optional - No response is confirmation. Defaults to True.default_is_yes
bool, optional - Default is Yes. Defaults to False.cursor
str, optional - What character(s) to use as a cursor. Defaults to '> '.cursor_style
str, optional - Rich friendly style for the cursor. Defaults to 'pink1'.char_prompt
bool, optional - Print [Y/n] after the question. Defaults to True.
Raises:
KeyboardInterrupt
- Raised when keyboard interrupt is encountered and Config.raise_on_interrupt is True
Returns:
Optional[bool]
Class Spinner
class Spinner()
__init__
def __init__(spinner_characters: List[str] = DOTS,
text: str = 'Loading...',
refresh_per_second: float = 10,
transient: bool = True)
Creates a spinner which can be used to provide some user feedback during long processing
Arguments:
spinner_characters
List[str] - List of strings that will be displayed in sequence by a spinnertext
str - Static text that will be shown after the spinner. Defaults toLoading...
refresh_per_second
float, optional - Number of refreshes the spinner will do a second, this will affect the fluidity of the "animation". Defaults to 10.transient
bool, optional - If the spinner will disappear after it's done, otherwise not. Defaults to True.
Raises:
ValueError
- Raised when nospinner_characters
are provided in
start
def start() -> None
Starts the spinner
stop
def stop() -> None
Stops the spinner