[enh] Add prefill and multiline in prompt

This commit is contained in:
ljf 2021-08-30 14:43:14 +02:00
parent e3ef1ced95
commit 882caf4786
2 changed files with 31 additions and 5 deletions

View file

@ -5,6 +5,7 @@
"confirm": "Confirm {prompt}", "confirm": "Confirm {prompt}",
"deprecated_command": "'{prog} {command}' is deprecated and will be removed in the future", "deprecated_command": "'{prog} {command}' is deprecated and will be removed in the future",
"deprecated_command_alias": "'{prog} {old}' is deprecated and will be removed in the future, use '{prog} {new}' instead", "deprecated_command_alias": "'{prog} {old}' is deprecated and will be removed in the future, use '{prog} {new}' instead",
"edit_text_question": "{}. Edit this text ? [yN]: ",
"error": "Error:", "error": "Error:",
"file_not_exist": "File does not exist: '{path}'", "file_not_exist": "File does not exist: '{path}'",
"folder_exists": "Folder already exists: '{path}'", "folder_exists": "Folder already exists: '{path}'",

View file

@ -6,8 +6,11 @@ import getpass
import locale import locale
import logging import logging
import argparse import argparse
import tempfile
from readline import insert_text, set_startup_hook
from collections import OrderedDict from collections import OrderedDict
from datetime import date, datetime from datetime import date, datetime
from subprocess import call
from moulinette import m18n, Moulinette from moulinette import m18n, Moulinette
from moulinette.actionsmap import ActionsMap from moulinette.actionsmap import ActionsMap
@ -522,7 +525,7 @@ class Interface:
credentials = self.prompt(msg, True, False, color="yellow") credentials = self.prompt(msg, True, False, color="yellow")
return authenticator.authenticate_credentials(credentials=credentials) return authenticator.authenticate_credentials(credentials=credentials)
def prompt(self, message, is_password=False, confirm=False, color="blue"): def prompt(self, message, is_password=False, confirm=False, color="blue", prefill="", is_multiline=False):
"""Prompt for a value """Prompt for a value
Keyword arguments: Keyword arguments:
@ -533,11 +536,33 @@ class Interface:
raise MoulinetteError( raise MoulinetteError(
"Not a tty, can't do interactive prompts", raw_msg=True "Not a tty, can't do interactive prompts", raw_msg=True
) )
def prompt(message):
if is_password:
return getpass.getpass(colorize(m18n.g("colon", message), color))
elif is_multiline:
while True:
value = input(colorize(m18n.g("edit_text_question", message), color))
if value in ["", "n", "N", "no", "NO"]:
return prefill
elif value in ['y', 'yes', 'Y', 'YES']:
break
if is_password: initial_message = prefill.encode('utf-8')
prompt = lambda m: getpass.getpass(colorize(m18n.g("colon", m), color))
else: with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
prompt = lambda m: input(colorize(m18n.g("colon", m), color)) tf.write(initial_message)
tf.flush()
call(["editor", tf.name])
tf.seek(0)
edited_message = tf.read()
return edited_message.decode("utf-8")
else:
set_startup_hook(lambda: insert_text(prefill))
try:
value = input(colorize(m18n.g("colon", message), color))
finally:
set_startup_hook()
return value
value = prompt(message) value = prompt(message)
if confirm: if confirm: