-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_prompts.py
More file actions
146 lines (130 loc) · 5.5 KB
/
python_prompts.py
File metadata and controls
146 lines (130 loc) · 5.5 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# python_prompts
# ==============
# Lightweight prompt system for python based on:
# [https://github.com/terkelg/prompts]
import sys
from time import sleep
from termcolor import colored
def PromptChain(chain):
for prompt in chain:
if prompt.prompt() == False:
break
def scrollTxt(text):
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
sleep(0.03)
class IntegerPrompt:
def __init__(self, name, _type, msg, validate, error, flush_output):
self.name = name
self.type = _type
self.msg = msg
self.validate = validate
self.error = error
self.flush_output = flush_output
def prompt(self):
globals()[self.name] = input(colored('? ', 'cyan') + colored(self.msg, attrs=['bold']) + colored(' › ', 'grey'))
output_check = False
# Smaller than
if self.type == '<':
if int(globals()[self.name]) > int(self.validate):
if not self.flush_output: print('\033[0m' + colored('› ', 'grey') + colored(f'\033[3m{self.error}\033[3m', 'red'))
output_check = False
else:
print('\033[0m{ ' + f'{self.name}: ' + colored(globals()[self.name], 'yellow') + ' }')
output_check = True
# Larger than
elif self.type == '>':
if int(globals()[self.name]) < int(self.validate):
if not self.flush_output: print('\033[0m' + colored('› ', 'grey') + colored(f'\033[3m{self.error}\033[3m', 'red'))
output_check = False
else:
print('\033[0m{ ' + f'{self.name}: ' + colored(globals()[self.name], 'yellow') + ' }')
output_check = True
# Inequality <
elif self.type == '<=':
if int(globals()[self.name]) <= int(self.validate):
if not self.flush_output: print('\033[0m{ ' + f'{self.name}: ' + colored(globals()[self.name], 'yellow') + ' }')
output_check = True
else:
print('\033[0m' + colored('› ', 'grey') + colored(f'\033[3m{self.error}\033[3m', 'red'))
output_check = False
# Inequality >
elif self.type == '>=':
if int(globals()[self.name]) >= int(self.validate):
if not self.flush_output: print('\033[0m{ ' + f'{self.name}: ' + colored(globals()[self.name], 'yellow') + ' }')
output_check = True
else:
print('\033[0m' + colored('› ', 'grey') + colored(f'\033[3m{self.error}\033[3m', 'red'))
output_check = False
# Equals
elif self.type == '=':
if int(globals()[self.name]) != int(self.validate):
if not self.flush_output: print('\033[0m' + colored('› ', 'grey') + colored(f'\033[3m{self.error}\033[3m', 'red'))
output_check = False
else:
print('\033[0m{ ' + f'{self.name}: ' + colored(globals()[self.name], 'yellow') + ' }')
output_check = True
return output_check
class StringPrompt:
def __init__(self, name, msg, validate, error, flush_output):
self.name = name
self.msg = msg
self.validate = validate
self.error = error
self.flush_output = flush_output
def prompt(self):
globals()[self.name] = input(colored('? ', 'cyan') + colored(self.msg, attrs=['bold']) + colored(' › ', 'grey'))
output_check = False
# For array validation
if type(self.validate) == type([]):
for index, validation in enumerate(self.validate):
if globals()[self.name].casefold() == validation.casefold():
if not self.flush_output: print('\033[0m{ ' + f'{self.name}: ' + colored(globals()[self.name], 'yellow') + ' }')
output_check = True
break
else:
print('\033[0m' + colored('› ', 'grey') + colored(f'\033[3m{self.error}\033[3m', 'red'))
output_check = False
# For single string validation
elif type(self.validate) == type(''):
if globals()[self.name].casefold() == self.validate.casefold():
if not self.flush_output: print('\033[0m{ ' + f'{self.name}: ' + colored(globals()[self.name], 'yellow') + ' }')
output_check = True
else:
print('\033[0m' + colored('› ', 'grey') + colored(f'\033[3m{self.error}\033[3m', 'red'))
output_check = False
return output_check
class MultipleChoicePrompt:
def __init__(self, name, msg, validate, error, choices, flush_output):
self.name = name
self.msg = msg
self.validate = validate
self.error = error
self.choices = choices
self.flush_output = flush_output
def prompt(self):
output_check = False
for option in self.choices:
text = '[' + str(self.choices.index(option) + 1) + ']'
scrollTxt(colored(text, attrs=['bold']) + f' {option}\n')
globals()[self.name] = input(colored('? ', 'cyan') + colored(self.msg, attrs=['bold']) + colored(' › ', 'grey'))
if int(globals()[self.name]) - 1 == self.validate:
choice = self.choices[int(globals()[self.name]) - 1]
globals()[self.name] = choice
if not self.flush_output: print('\033[0m{ ' + f'{self.name}: ' + colored(globals()[self.name], 'yellow') + ' }')
output_check = True
else:
print('\033[0m' + colored('› ', 'grey') + colored(f'\033[3m{self.error}\033[3m', 'red'))
output_check = False
return output_check
def return_prompt_variable(prompt):
return globals()[prompt.name]
# age = IntegerPrompt('Age', '>=', 'What\'s your age?', 7, 'Hey!', False)
# name = StringPrompt('Name', 'What\'s your name?', 'Shaunak', 'Hey!', False)
# choices = ["cheese", "tomato"]
# question3 = MultipleChoicePrompt('Choice', 'What do you pick?', 0, 'Hey!', choices, False)
# question4 = IntegerPrompt('Hobbies', '>=', 'How many hobbies do you have?', 1, 'Hey!', False)
# chain = [age, name, question3, question4]
# PromptChain(chain)