[cli] Add prompt command for CLI logger. (#9897)

Co-authored-by: Richard Liaw <rliaw@berkeley.edu>
This commit is contained in:
Robert Nishihara 2020-08-31 11:03:53 -07:00 committed by GitHub
parent 5d4d67c47d
commit 0bba5485d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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.