From 0427d2586f342f387607532de4ac4d179602532e Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Sat, 29 Jun 2019 20:52:38 -0600 Subject: [PATCH] Added ask and pause functions --- scripts/wk/std.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/scripts/wk/std.py b/scripts/wk/std.py index cd86d714..b492fa9b 100644 --- a/scripts/wk/std.py +++ b/scripts/wk/std.py @@ -3,6 +3,7 @@ import logging import os +import re import sys try: @@ -27,6 +28,20 @@ LOG = logging.getLogger(__name__) # Functions +def ask(prompt='Kotaero!'): + """Prompt the user with a Y/N question, returns bool.""" + answer = None + prompt = '{} [Y/N]: '.format(prompt) + while answer is None: + tmp = input_text(prompt) + if re.search(r'^y(es|)$', tmp, re.IGNORECASE): + answer = True + elif re.search(r'^n(o|ope|)$', tmp, re.IGNORECASE): + answer = False + LOG.info('%s%s', prompt, 'Yes' if answer else 'No') + return answer + + def input_text(prompt='Enter text'): """Get text from user, returns string.""" prompt = str(prompt) @@ -40,7 +55,7 @@ def input_text(prompt='Enter text'): tcflush(sys.stdin, TCIOFLUSH) try: response = input(prompt) - LOG.debug('%s.input_text response: %s', __name__, response) + LOG.debug('%s%s', prompt, response) except EOFError: # Ignore and try again LOG.warning('Exception occured', exc_info=True) @@ -49,5 +64,10 @@ def input_text(prompt='Enter text'): return response +def pause(prompt='Press Enter to continue... '): + """Simple pause implementation.""" + input_text(prompt) + + if __name__ == '__main__': print("This file is not meant to be called directly.")