Added ask and pause functions

This commit is contained in:
2Shirt 2019-06-29 20:52:38 -06:00
parent 96837ff774
commit 0427d2586f
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

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