From 1f7bb1d54c25761cc39d1af044e58cd626a311b1 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 21 Sep 2021 20:13:39 +0200 Subject: [PATCH] Rework cli prompt() again --- debian/control | 3 ++- moulinette/interfaces/cli.py | 43 +++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/debian/control b/debian/control index b399c78d..8713affa 100644 --- a/debian/control +++ b/debian/control @@ -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. diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 6e1eb8cd..faab2294 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -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(