1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00
AdvLabDB/advlabdb/scripts/terminal_utils.py

95 lines
1.9 KiB
Python

import subprocess
from getpass import getpass
def run(command, **kwargs):
return subprocess.run(command, shell=True, **kwargs)
def box(message, context=None):
text_line = "| "
if context is not None:
text_line += context + ": "
text_line += message + " |"
separator = "=" * len(text_line)
print()
print(separator)
print(text_line)
print(separator)
print()
def step(message):
continue_message = "-> Press ENTER to continue or Ctrl+C to interrupt the script <-"
upper_separator = "_" * len(continue_message)
print()
print(upper_separator)
box(message, "Next step")
print(continue_message)
getpass("")
print()
def spaced_hl():
print("\n\n" + "_" * 20 + "\n\n")
def validating_input(
prompt,
options=None,
format_function=lambda ans: ans,
check_constraints_function=lambda ans: True,
):
if options is not None:
prompt += " ["
for opt in options[:-1]:
prompt += opt + "/"
prompt += options[-1] + "]: "
lowered_options = [opt.lower() for opt in options]
def adj_check_constraints_function(ans):
return ans.lower() in lowered_options and check_constraints_function(ans)
else:
prompt += ": "
adj_check_constraints_function = check_constraints_function
ans = None
first_run = True
while ans is None or not adj_check_constraints_function(ans):
if not first_run:
print("Invalid input!\n")
else:
first_run = False
ans = input(prompt)
try:
formatted_ans = format_function(ans)
except Exception as ex:
ans = None
return formatted_ans
def confirm(prompt):
ans = validating_input(
prompt,
options=("y", "n"),
format_function=lambda ans: ans.lower(),
)
if ans == "y":
return True
else:
return False