-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.py
More file actions
128 lines (120 loc) · 4.86 KB
/
Menu.py
File metadata and controls
128 lines (120 loc) · 4.86 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
import os
import time
import imghdr
from Solver import Solver
import itertools
import threading
import sys
class Menu:
# general vars
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
WAITTIME = 2
# first menu vars
ABOVEFIRSTMENUTEXT = f'{BOLD}General options:{ENDC}\n'
FIRSTMENUTEXT = f'{BOLD}0.{ENDC} Exit.\n\n{BOLD}1.{ENDC} Calculate sphere values.\n\n{BOLD}2.{ENDC} Coming soon.\n\n'
INVALIDOPTIONTEXT = f'{FAIL}The chosen option is invalid. Please just type integers: {BOLD}\'<option>\'{ENDC}'
GOODBYETEXTERROR = f'\n\n{BOLD}Goodbye.{ENDC}\nPlease open an {BOLD}issue{ENDC} on {BOLD}Github{ENDC} if you have any problem with the software.{ENDC}\n\n{UNDERLINE}https://github.com/karastift/Solver.git\n{ENDC}'
GOODBYETEXT = f'{BOLD}Goodbye.{ENDC}'
exited = False
# sphere menu vars
done = False
imagePath = str()
noneText = imagePath if imagePath else f'{WARNING}None{ENDC}'
imageText = f'{OKGREEN}selected image{ENDC}' if imagePath else f'{WARNING}image required{ENDC}'
ABOVEMENUTEXT = f'{BOLD}Sphere options:{ENDC}\n\nSelected image: {noneText}\n\n'
SPHEREMENUTEXT = f'{BOLD}0.{ENDC} Return.\n\n{BOLD}1.{ENDC} Enter path to image of tasks.\n\n2.{ENDC} Get values {imageText}.\n\n'
VALIDIMAGEEXT = ['png', 'jpg', 'jpeg']
def __init__(self):
self.firstMenu()
def refreshVariables(self):
self.noneText = f'{self.OKGREEN}{self.imagePath}{self.ENDC}' if self.imagePath else f'{self.WARNING}None{self.ENDC}'
self.imageText = f'{self.OKGREEN}selected image{self.ENDC}' if self.imagePath else f'{self.WARNING}Image required{self.ENDC}'
self.ABOVEMENUTEXT = f'{self.BOLD}Sphere options:{self.ENDC}\n\nSelected image: {self.noneText}\n\n'
self.SPHEREMENUTEXT = f'{self.BOLD}0.{self.ENDC} Return.\n\n{self.BOLD}1.{self.ENDC} Enter path to image of tasks.\n\n2.{self.ENDC} Get values ({self.imageText}).\n\n'
def firstMenu(self):
try:
self.screenClear()
print(self.ABOVEFIRSTMENUTEXT)
print(self.FIRSTMENUTEXT)
option = input('What do you want to do?\n: ')
if option == '0':
self.exited = True
exit()
elif option == '1':
self.sphereMenu()
else:
print(self.INVALIDOPTIONTEXT)
time.sleep(self.WAITTIME)
self.firstMenu()
except:
if self.exited:
print(self.GOODBYETEXT)
else:
print(self.GOODBYETEXTERROR)
def sphereMenu(self):
try:
self.refreshVariables()
self.screenClear()
print(self.ABOVEMENUTEXT)
print(self.SPHEREMENUTEXT)
option = input('Choose option\n: ')
if option == '0':
self.firstMenu()
elif option == '1':
self.screenClear()
print(self.ABOVEMENUTEXT)
print(self.SPHEREMENUTEXT)
path = input('Enter path: ')
if self.checkImagePath(path):
pass
else:
print(f'{self.FAIL}\'{path}\' does not lead to a valid image.{self.ENDC}')
time.sleep(self.WAITTIME)
self.sphereMenu()
elif option == '2':
S = Solver()
convertedImage = S.convertImage(self.imagePath, 1)
convertedString = S.convertString(convertedImage)
solution = S.calculateSphere(convertedString)
self.screenClear()
S.printSolution(solution)
t = threading.Thread(target=self.animate)
t.start()
input()
self.done = True
self.sphereMenu()
else:
print(self.INVALIDOPTIONTEXT)
time.sleep(self.WAITTIME)
self.sphereMenu()
except:
self.firstMenu()
def checkImagePath(self, imagePath):
if os.path.isfile(f'./{imagePath}') and imghdr.what(f'./{imagePath}') in self.VALIDIMAGEEXT:
self.imagePath = f'./{imagePath}'
return True
elif os.path.isfile(imagePath) and imghdr.what(f'{imagePath}') in self.VALIDIMAGEEXT:
self.imagePath = imagePath
return True
else:
return False
def screenClear(self):
if os.name == 'posix':
os.system('clear')
else:
os.system('cls')
def animate(self):
for c in itertools.cycle(['|', '/', '-', '\\']):
if self.done:
break
sys.stdout.write('\rPress ENTER To Continue ' + c)
sys.stdout.flush()
time.sleep(0.1)