-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal_utils.py
113 lines (90 loc) · 2.91 KB
/
terminal_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import re
import time
from typing import Iterable
from colorama import Fore
import sys
from pathlib import Path
from ANSI import ANSI
import time
from typing import Iterable, Any
from enum import StrEnum, auto
def replace_previous_line(string):
sys.stdout.write("\033[F") # back to previous line
sys.stdout.write("\033[K") # clear line
print(string)
def clear_n_previous_lines(n):
for _ in range(n):
sys.stdout.write("\033[F") # back to previous line
sys.stdout.write("\033[K") # clear line
time.sleep(0.05)
def print_n_lines_back(n: int, string: str):
for _ in range(n):
sys.stdout.write("\033[F") # back to previous line
sys.stdout.write("\033[K") # clear line
time.sleep(0.05)
print(string)
def prompt_user(prompt: str, exit_char='q') -> str:
while True:
keyword = input(prompt)
if keyword.lower() == exit_char:
print("Quit Program")
exit()
else:
return keyword
def user_continues_with_dst_option(prompt="Send these files?", dst="") -> bool:
"""
Prompts the user if they want to send files
"""
while True:
if dst == "":
choice = input(f"\n{prompt} (y/n): ").strip().lower()
else:
choice = input(f"\n{prompt} to {dst}? (y/n): ").strip().lower()
if choice == 'y':
print()
return True
elif choice == 'n':
print("No Files Sent")
return False
else:
print("Invalid Input")
def user_choice_bool(prompt="Send these files? (y/n): ") -> bool:
"""
Prompts the user if they want to prompt. Accepts Y/y or N/n
"""
while True:
try:
choice = input(f"\n{prompt}").strip().lower()
if choice == 'y':
return True
elif choice == 'n':
return False
else:
print("Invalid Input")
except KeyboardInterrupt:
raise KeyboardInterrupt
# return None
def user_choice_numbered(args: Iterable[Any],
input_prompt="Enter Option: ", option_prompt="Options: ", delete_lines:bool = True) -> Any | None:
"""
Prompts the user if to select an item from an iterable of items.
If exit_option True, then if input is the exit_char, will return None.
Keyboard Interupts will also return None.
"""
print(f"\n{option_prompt}")
count = 1
for elem in args:
print(f" {count}. {str(elem).title()}")
count += 1
while True:
try:
choice = int(input(f"\n{input_prompt}").strip().lower())
except KeyboardInterrupt:
raise KeyboardInterrupt
except:
choice = -1
if (choice - 1) in range(0, len(args)):
clear_n_previous_lines(count + 2)
return args[choice - 1]
else:
print("Invalid Input")