47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
from typing import Optional
|
|
import getpass
|
|
import time
|
|
|
|
|
|
def choice(default: str, options: str, msg: str) -> str:
|
|
default = default.lower()
|
|
formatted_options = "".join(
|
|
[(opt + "/" if opt != default else default.upper() + "/") for opt in options]
|
|
)[:-1]
|
|
choice = input(msg + " [" + formatted_options + "] ")
|
|
if choice == "":
|
|
return default
|
|
else:
|
|
return choice.lower()
|
|
|
|
|
|
def confirm(is_true_default: bool, msg: Optional[str] = ""):
|
|
return (
|
|
choice(
|
|
"y" if is_true_default else "n",
|
|
"yn",
|
|
(msg if msg != "" and msg != None else "Do you really want to continue?"),
|
|
)
|
|
== "y"
|
|
)
|
|
|
|
|
|
def confirm_overwrite(msg: Optional[str] = ""):
|
|
return confirm(
|
|
False, (msg if msg != "" else "Do you really want to overwrite your changes?")
|
|
)
|
|
|
|
|
|
def password(msg: str = ""):
|
|
pw = ""
|
|
if msg != "":
|
|
pw = getpass.getpass(msg)
|
|
else:
|
|
pw = getpass.getpass()
|
|
if pw != "":
|
|
return pw
|
|
else:
|
|
time.sleep(1)
|
|
print("Error: Password cannot be empty")
|
|
return password(msg)
|