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,promptandconfirmthis meansNone, while forselect_multipleit 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,promptandconfirmthis meansNone, while forselect_multipleit means an empty list -[]. Defaults to False.transient(bool)- If False, elements will remain displayed after their 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:
promptstr - The prompt that will be displayedtarget_typeUnion[Type[T], Type[str]], optional - Type to convert the answer to. Defaults to str.validatorCallable[[Any], bool], optional - Optional function to validate the input. Defaults to lambda input: True.securebool, optional - If True, input will be hidden. Defaults to False.raise_validation_failbool, optional - If True, invalid inputs will raiserich.internals.ValidationError, else the error will be reported onto the console. Defaults to True.raise_type_conversion_failbool, optional - If True, invalid inputs will raiserich.internals.ConversionError, else the error will be reported onto the console. Defaults to True.initial_valuestr, 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[str, T]],
preprocessor: Callable[[T], str] = lambda val: str(val),
cursor: str = '>',
cursor_style: str = 'pink1',
cursor_index: int = 0,
return_index: bool = False,
strict: bool = False,
pagination: bool = False,
page_size: Optional[int] = None) -> Union[int, Any, None]
A prompt that allows selecting one option from a list of options
Arguments:
optionsList[Union[str, T]] - A list of options to select from. Ifpreprocessoris left as default (not passed), it needs to be a list of strings or objects with a__str__method. Otherwise, you can pass apreprocessorto create a string representation of arbitrary data-structures.preprocessorCallable[[T], str] - A callable that can be used to preprocess the list of options prior to printing. For example, if you passed aPersonobject withnameattribute, preprocessor could belambda person: person.nameto just show the content ofnameattribute in the select dialog. Defaults tolambda val: valcursorstr, optional - Cursor that is going to appear in front of currently selected option. Defaults to '> '.cursor_stylestr, optional - Rich friendly style for the cursor. Defaults to 'pink1'.cursor_indexint, optional - Option can be preselected based on its list index. Defaults to 0.return_indexbool, optional - IfTrue,selectwill return the index of selected element in options. Defaults toFalse.strictbool, optional - If emptyoptionsis provided and strict isFalse, None will be returned, if it'sTrue,ValueErrorwill be thrown. Defaults to False.paginationbool, optional - IfTrue, pagination will be used. Defaults to False.page_sizeOptional[int], optional - Number of options to show on a single page if pagination is enabled. IfNone, the page size is derived automatically from the terminal height. Pagination is also enabled automatically when options exceed the terminal height. Defaults to None.
Raises:
ValueError- Thrown if nooptionsare provided and strict isTrueKeyboardInterrupt- 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[str, T]],
preprocessor: Callable[[T], str] = lambda val: str(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: Optional[int] = None) -> List[Union[int, Any]]
A prompt that allows selecting multiple options from a list of options
Arguments:
optionsList[Union[str, T]] - A list of options to select from. Ifpreprocessoris left as default (not passed), it needs to be a list of strings or objects with a__str__method. Otherwise, you can pass apreprocessorto create a string representation of arbitrary data-structures.preprocessorCallable[[T], str] - A callable that can be used to preprocess the list of options prior to printing. For example, if you passed aPersonobject withnameattribute, preprocessor could belambda person: person.nameto just show the content ofnameattribute in the select_multiple dialog. Defaults tolambda val: valtick_characterstr, optional - Character that will be used as a tick in a checkbox. Defaults to 'x'.tick_stylestr, optional - Rich friendly style for the tick character. Defaults to 'pink1'.cursor_stylestr, optional - Rich friendly style for the option when the cursor is currently on it. Defaults to 'pink1'.ticked_indicesOptional[List[int]], optional - Indices of options that are pre-ticked when the prompt appears. Defaults to None.cursor_indexint, optional - Index of the option cursor starts at. Defaults to 0.minimal_countint, optional - Minimal count of options that need to be selected. Defaults to 0.maximal_countOptional[int], optional - Maximal count of options that need to be selected. Defaults to None.return_indicesbool, optional - IfTrue,select_multiplewill return the indices of ticked elements in options. Defaults toFalse.strictbool, optional - If emptyoptionsis provided and strict isFalse, None will be returned, if it'sTrue,ValueErrorwill be thrown. Defaults to False.paginationbool, optional - IfTrue, pagination will be used. Defaults to False.page_sizeOptional[int], optional - Number of options to show on a single page if pagination is enabled. IfNone, the page size is derived automatically from the terminal height. Pagination is also enabled automatically when options exceed the terminal height. Defaults to None.
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:
questionstr - Question to be askedyes_textstr, optional - Text of the positive response. Defaults to 'Yes'.no_textstr, optional - Text of the negative response. Defaults to 'No'.has_to_match_casebool, optional - Check if typed response matches case. Defaults to False.enter_empty_confirmsbool, optional - No response is confirmation. Defaults to True.default_is_yesbool, optional - Default is Yes. Defaults to False.cursorstr, optional - What character(s) to use as a cursor. Defaults to '> '.cursor_stylestr, optional - Rich friendly style for the cursor. Defaults to 'pink1'.char_promptbool, 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_charactersList[str] - List of strings that will be displayed in sequence by a spinnertextstr - Static text that will be shown after the spinner. Defaults toLoading...refresh_per_secondfloat, optional - Number of refreshes the spinner will do a second, this will affect the fluidity of the "animation". Defaults to 10.transientbool, optional - If the spinner will disappear after it's done, otherwise not. Defaults to True.
Raises:
ValueError- Raised when nospinner_charactersare provided in
start
def start() -> None
Starts the spinner
stop
def stop() -> None
Stops the spinner