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-gevent-websocket,
python3-toml, python3-toml,
python3-psutil, python3-psutil,
python3-tz python3-tz,
python3-prompt-toolkit
Breaks: yunohost (<< 4.1) Breaks: yunohost (<< 4.1)
Description: prototype interfaces with ease in Python Description: prototype interfaces with ease in Python
Quickly and easily prototype interfaces for your application. Quickly and easily prototype interfaces for your application.

View file

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