A fairly simple method for registering callables as prompt-toolkit completions
Project description
Action Completer
A fairly simple method for registering callables as prompt-toolkit completions.
This package provides the basic features to easily construct a custom completer using decorators to reduce the amount of boilerplate needed to build basic tooling through prompt-toolkit. A quick example is provided below, but if you are interested in the available features and patterns you should read through the documentation.
This is a project that I originally created for myself several times when building personal utilities with prompt-toolkit, and figured it might be useful for other people to eventually use or extend. As a side-effect of this being a personal utility, the provided functionality may not exactly fit what you are looking for and the provided tests do not check all edge cases properly yet.
from pathlib import Path
from action_completer import ActionCompleter
from prompt_toolkit.shortcuts import prompt
from prompt_toolkit.completion import PathCompleter
from prompt_toolkit.validation import Validator
completer = ActionCompleter()
@completer.action("cat")
@completer.param(
PathCompleter(),
cast=Path,
validators=[
Validator.from_callable(
lambda p: Path(p).is_file(),
error_message="Path is not an existing file"
)
]
)
def _cat_action(filepath: Path):
with filepath.open("r") as file_handle:
print(file_handle.read())
prompt_result = prompt(
">>> ",
completer=completer,
validator=completer.get_validator()
)
completer.run_action(prompt_result)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for prompt-toolkit-action-completer-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c581ab631be2a51b62870fd8164036b4730126eb7c61af1a59a2d8fb91fc985f |
|
MD5 | 48d35ce19356a68e5a23292ac06be7ea |
|
BLAKE2b-256 | 21d6ff0f9f070f52e60dac28146d10a78e03132af66c5ad983ec0b91079e33d0 |
Hashes for prompt_toolkit_action_completer-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c2db1cf44622c1d0b835814a1ed4dcf47dec805524e1cb28c2698052c2760e1 |
|
MD5 | 459a0ed6307c79508d837d4752026153 |
|
BLAKE2b-256 | 75f4302713a84c74d7fea798d0576207df732168a5a7d44a968981da370f19a6 |