ScoreWalker/scorewalker-utils/KMeans/ConsoleUtils.py
2025-03-13 00:13:53 -06:00

54 lines
2.1 KiB
Python

"""
This is a small project to help with some console functionality. Currently it just offers the ability to print a
progress bar to the console.
"""
# TODO: Add the yes_or_no function here.
import sys
# Returns a green progress bar with a percentage on the left.
def get_prg_line(current, total, bar_size, line_size, percent_decimals=2):
num_to_repeat = int(bar_size*(current/total))
if num_to_repeat == 0:
num_to_repeat = 1
if current == total:
tmp_result = '{0:} '.format('Done!') + '\033[102m' + (' ' * num_to_repeat) + '\033[0m' + \
(' ' * (bar_size - num_to_repeat) + '\n')
else:
tmp_result = ('{1:.{0}%} '.format(percent_decimals, current/total) + '\033[102m' + (' ' * num_to_repeat) +\
('\033[0m' + (' ' * (bar_size - num_to_repeat))))
final_result = ('{:^%d}' % line_size).format(tmp_result)
return final_result
# Writes a progress bar to the console with the given info and a % on the left.
# This will update if it is the only line printed!
def print_progress_bar(current, total, bar_size, line_size, precision=2):
sys.stdout.write('\r' + get_prg_line(current, total, bar_size, line_size, precision))
def yes_or_no(message):
"""
Will prompt the user with the given message and accept either 'y', 'yes', 'n', or 'no' as inputs ignoring case.
The program will exit (with status 0) if the user enters no and will continue if the user enters yes.
.. note: The user will be prompted to re-enter input if it is not valid with the following message:
'Invalid input, enter Y(es) or N(o):' (This message will be printed until valid input is provided).
:param message: The message to display to the user.
:type message: str
.. raw:: html
<br>
:return: None.
"""
decision = input(message)
if decision.lower() == 'y' or decision.lower() == 'yes':
return
elif decision.lower() == 'n' or decision.lower() == 'no':
print('Exiting...')
exit(0)
else:
yes_or_no(' Invalid input, enter Y(es) or N(o): ')