Added ask and pause functions
This commit is contained in:
parent
96837ff774
commit
0427d2586f
1 changed files with 21 additions and 1 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -27,6 +28,20 @@ LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# Functions
|
# 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'):
|
def input_text(prompt='Enter text'):
|
||||||
"""Get text from user, returns string."""
|
"""Get text from user, returns string."""
|
||||||
prompt = str(prompt)
|
prompt = str(prompt)
|
||||||
|
|
@ -40,7 +55,7 @@ def input_text(prompt='Enter text'):
|
||||||
tcflush(sys.stdin, TCIOFLUSH)
|
tcflush(sys.stdin, TCIOFLUSH)
|
||||||
try:
|
try:
|
||||||
response = input(prompt)
|
response = input(prompt)
|
||||||
LOG.debug('%s.input_text response: %s', __name__, response)
|
LOG.debug('%s%s', prompt, response)
|
||||||
except EOFError:
|
except EOFError:
|
||||||
# Ignore and try again
|
# Ignore and try again
|
||||||
LOG.warning('Exception occured', exc_info=True)
|
LOG.warning('Exception occured', exc_info=True)
|
||||||
|
|
@ -49,5 +64,10 @@ def input_text(prompt='Enter text'):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def pause(prompt='Press Enter to continue... '):
|
||||||
|
"""Simple pause implementation."""
|
||||||
|
input_text(prompt)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print("This file is not meant to be called directly.")
|
print("This file is not meant to be called directly.")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue