A function that asks for user input according to some criteria and again on the same line if the criteria is not met. It requires that escape character sequences be enable.
"""
Ask for user input according to some criteria
and again on the same line if the criteria is
not met
"""
__all__ = ['input_wrong_up_line']
def input_wrong_up_line(input_msg, criteria):
a = '\033[A'
input_wrong_msg = a + len(input_msg) * ' '
while True:
input_user = input(input_msg)
if criteria(input_user):
return input_user
print(input_wrong_msg + len(input_user) * ' ' + a)
if __name__ == '__main__':
from colorama import deinit, init
init()
try:
input_wrong_up_line('Exit? [y/ ] ', lambda input_user: input_user == 'y')
finally:
deinit()