From 0bba5485d33728a5d165f13c55733de31968d419 Mon Sep 17 00:00:00 2001 From: Robert Nishihara Date: Mon, 31 Aug 2020 11:03:53 -0700 Subject: [PATCH] [cli] Add prompt command for CLI logger. (#9897) Co-authored-by: Richard Liaw --- python/ray/autoscaler/cli_logger.py | 32 ++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/python/ray/autoscaler/cli_logger.py b/python/ray/autoscaler/cli_logger.py index b31f3a5b0..1bb53ccf4 100644 --- a/python/ray/autoscaler/cli_logger.py +++ b/python/ray/autoscaler/cli_logger.py @@ -550,7 +550,8 @@ class _CliLogger(): confirm_str = cf.underlined("Confirm [" + yn_str + "]:") + " " rendered_message = _format_msg(msg, *args, **kwargs) - if rendered_message and rendered_message[-1] != "\n": + # the rendered message ends with ascii coding + if rendered_message and not msg.endswith("\n"): rendered_message += " " msg_len = len(rendered_message.split("\n")[-1]) @@ -602,6 +603,35 @@ class _CliLogger(): return res + def prompt(self, msg: str, *args, **kwargs): + """Prompt the user for some text input. + + Args: + msg (str): The mesage to display to the user before the prompt. + + Returns: + The string entered by the user. + """ + if self.old_style: + return + + complete_str = cf.underlined(msg) + rendered_message = _format_msg(complete_str, *args, **kwargs) + # the rendered message ends with ascii coding + if rendered_message and not msg.endswith("\n"): + rendered_message += " " + self._print(rendered_message, linefeed=False) + + res = "" + try: + ans = sys.stdin.readline() + ans = ans.lower() + res = ans.strip() + except KeyboardInterrupt: + self.newline() + + return res + def old_confirm(self, msg: str, yes: bool): """Old confirm dialog proxy.