cli prompt: misc code improvements

This commit is contained in:
Alexandre Aubin 2021-09-03 15:49:36 +02:00
parent e08d0139f6
commit 8909577b91

View file

@ -536,10 +536,19 @@ 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):
def _prompt(message):
if is_password: if is_password:
return getpass.getpass(colorize(m18n.g("colon", message), color)) return getpass.getpass(colorize(m18n.g("colon", message), color))
elif is_multiline: elif not is_multiline:
set_startup_hook(lambda: insert_text(prefill))
try:
value = input(colorize(m18n.g("colon", message), color))
finally:
set_startup_hook()
return value
else:
while True: while True:
value = input(colorize(m18n.g("edit_text_question", message), color)) value = input(colorize(m18n.g("edit_text_question", message), color))
value = value.lower().strip() value = value.lower().strip()
@ -557,18 +566,12 @@ class Interface:
tf.seek(0) tf.seek(0)
edited_message = tf.read() edited_message = tf.read()
return edited_message.decode("utf-8") return edited_message.decode("utf-8")
else:
set_startup_hook(lambda: insert_text(prefill)) value = _prompt(message)
try:
value = input(colorize(m18n.g("colon", message), color))
finally:
set_startup_hook()
return value
value = prompt(message)
if confirm: if confirm:
m = message[0].lower() + message[1:] m = message[0].lower() + message[1:]
if prompt(m18n.g("confirm", prompt=m)) != value: if _prompt(m18n.g("confirm", prompt=m)) != value:
raise MoulinetteValidationError("values_mismatch") raise MoulinetteValidationError("values_mismatch")
return value return value