Rework cli prompt() again

This commit is contained in:
Alexandre Aubin 2021-09-21 20:13:39 +02:00
parent 8e3a52446a
commit 1f7bb1d54c
2 changed files with 35 additions and 11 deletions

3
debian/control vendored
View file

@ -14,7 +14,8 @@ Depends: ${misc:Depends}, ${python3:Depends},
python3-gevent-websocket,
python3-toml,
python3-psutil,
python3-tz
python3-tz,
python3-prompt-toolkit
Breaks: yunohost (<< 4.1)
Description: prototype interfaces with ease in Python
Quickly and easily prototype interfaces for your application.

View file

@ -533,6 +533,8 @@ class Interface:
color="blue",
prefill="",
is_multiline=False,
autocomplete=[],
help=None,
):
"""Prompt for a value
@ -547,16 +549,37 @@ class Interface:
def _prompt(message):
if is_password:
return getpass.getpass(colorize(m18n.g("colon", message), color))
elif not is_multiline:
print(colorize(m18n.g("colon", message), color), end="")
set_startup_hook(lambda: insert_text(prefill))
try:
value = input()
finally:
set_startup_hook()
return value
if not is_multiline:
import prompt_toolkit
from prompt_toolkit.contrib.completers import WordCompleter
from pygments.token import Token
autocomplete_ = WordCompleter(autocomplete)
style = prompt_toolkit.styles.style_from_dict({
Token.Message: f'#ansi{color} bold',
})
def get_bottom_toolbar_tokens(cli):
if help:
return [(Token, help)]
else:
return []
def get_tokens(cli):
return [
(Token.Message, message),
(Token, ': '),
]
return prompt_toolkit.prompt(get_prompt_tokens=get_tokens,
get_bottom_toolbar_tokens=get_bottom_toolbar_tokens,
style=style,
default=prefill,
true_color=True,
completer=autocomplete_,
is_password=is_password)
else:
while True:
value = input(