diff --git a/locales/en.json b/locales/en.json index 68aa640a..358f326f 100644 --- a/locales/en.json +++ b/locales/en.json @@ -5,6 +5,7 @@ "confirm": "Confirm {prompt}", "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", + "edit_text_question": "{}. Edit this text ? [yN]: ", "error": "Error:", "file_not_exist": "File does not exist: '{path}'", "folder_exists": "Folder already exists: '{path}'", diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index c75b8d60..7802b8d1 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -6,8 +6,11 @@ import getpass import locale import logging import argparse +import tempfile +from readline import insert_text, set_startup_hook from collections import OrderedDict from datetime import date, datetime +from subprocess import call from moulinette import m18n, Moulinette from moulinette.actionsmap import ActionsMap @@ -522,7 +525,7 @@ class Interface: credentials = self.prompt(msg, True, False, color="yellow") 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 Keyword arguments: @@ -534,15 +537,41 @@ class Interface: "Not a tty, can't do interactive prompts", raw_msg=True ) - if is_password: - prompt = lambda m: getpass.getpass(colorize(m18n.g("colon", m), color)) - else: - prompt = lambda m: input(colorize(m18n.g("colon", m), color)) - value = prompt(message) + def _prompt(message): + + if is_password: + return getpass.getpass(colorize(m18n.g("colon", message), color)) + 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: + value = input(colorize(m18n.g("edit_text_question", message), color)) + value = value.lower().strip() + if value in ["", "n", "no"]: + return prefill + elif value in ['y', 'yes']: + break + + initial_message = prefill.encode('utf-8') + + with tempfile.NamedTemporaryFile(suffix=".tmp") as tf: + tf.write(initial_message) + tf.flush() + call(["editor", tf.name]) + tf.seek(0) + edited_message = tf.read() + return edited_message.decode("utf-8") + + value = _prompt(message) if confirm: 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") return value diff --git a/moulinette/utils/filesystem.py b/moulinette/utils/filesystem.py index 4844dd1f..9b1456d9 100644 --- a/moulinette/utils/filesystem.py +++ b/moulinette/utils/filesystem.py @@ -67,16 +67,17 @@ def read_json(file_path): return loaded_json -def read_yaml(file_path): +def read_yaml(file_): """ Safely read a yaml file Keyword argument: - file_path -- Path to the yaml file + file -- Path or stream to the yaml file """ # Read file - file_content = read_file(file_path) + file_path = file_ if isinstance(file_, str) else file_.name + file_content = read_file(file_) if isinstance(file_, str) else file_ # Try to load yaml to check if it's syntaxically correct try: