-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
174 lines (136 loc) · 6.05 KB
/
main.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import threading
import time
import colorama
from colorama import Fore, Back, Style
colorama.init() # makes color work on win
active_project = 'None'
projects = {'None': 0}
def clock():
while True:
if active_project == 'None':
file_thread = threading.Thread(target=write_to_file())
file_thread.setDaemon(True) # doesn't hang the program when you try and leave
file_thread.start()
time.sleep(1)
else:
projects[active_project] += 1
file_thread = threading.Thread(target=write_to_file())
file_thread.setDaemon(True) # doesn't hang the program when you try and leave
file_thread.start()
time.sleep(1)
# print(f'{clock_time}')
def read_file():
file = open("db.txt")
for line in file:
key_file, value_file = line.split(" | ")
value_file = value_file.strip()
projects[key_file] = int(value_file)
def write_to_file():
file = open("db.txt", "w") # deletes everything and writes from beginning
i = 0
for project in projects:
if i == 0: # doesn't include \n in 1st line
file.write(f"{project} | {projects[project]}")
else: # all other lines
file.write(f"\n{project} | {projects[project]}")
i += 1
def display_time(secs):
if secs >= 60:
minutes = secs // 60
secs = secs - (minutes * 60)
if minutes >= 60:
hours = minutes // 60
minutes = minutes - (hours * 60)
if hours >= 24:
days = hours // 24
hours = hours - (days * 24)
print(f"Time elapsed: {Back.MAGENTA + Fore.LIGHTWHITE_EX}{days} days, " +
f"{hours} hours, {minutes} minutes and {secs} seconds")
else:
print(f"Time elapsed: {Back.MAGENTA + Fore.LIGHTWHITE_EX}{hours} hours, {minutes} minutes and {secs} seconds")
else:
print(f"Time elapsed: {Back.MAGENTA + Fore.LIGHTWHITE_EX}{minutes} minutes and {secs} seconds")
else:
print(f"Time elapsed: {Back.MAGENTA + Fore.LIGHTWHITE_EX}{secs} seconds")
print(Style.RESET_ALL)
def menu():
global active_project
while True:
print(Back.BLUE + Fore.LIGHTWHITE_EX + "(1)" + Style.RESET_ALL + " See current project's time\n" +
Back.BLUE + Fore.LIGHTWHITE_EX + "(2)" + Style.RESET_ALL + " See all projects\n" +
Back.BLUE + Fore.LIGHTWHITE_EX + "(3)" + Style.RESET_ALL + " Change active project\n" +
Back.BLUE + Fore.LIGHTWHITE_EX + "(4)" + Style.RESET_ALL + " Add project\n" +
Back.BLUE + Fore.LIGHTWHITE_EX + "(5)" + Style.RESET_ALL + " Remove project\n" +
Back.BLUE + Fore.LIGHTWHITE_EX + "(6)" + Style.RESET_ALL + " Exit")
option = input("--> ")
if option == '1':
print(f"\nActive project: {Back.MAGENTA + Fore.LIGHTWHITE_EX}{active_project}{Style.RESET_ALL}")
display_time(projects[active_project])
elif option == '2':
print("")
for p in projects:
print(f"Name: {Back.MAGENTA + Fore.LIGHTWHITE_EX}{p}{Style.RESET_ALL}")
display_time(projects[p])
elif option == '3':
print("")
for p in projects:
print(f"Name: {Back.MAGENTA + Fore.LIGHTWHITE_EX}{p}{Style.RESET_ALL}")
display_time(projects[p])
proj_name = input("Input the project name: ")
print("")
if proj_name == 'None':
if proj_name == active_project:
print(Back.RED + Fore.LIGHTWHITE_EX + "That project is already active\n")
else:
active_project = proj_name
elif proj_name in projects:
if proj_name == active_project:
print(Back.RED + Fore.LIGHTWHITE_EX + "That project is already active\n")
else:
active_project = proj_name
print(Back.BLUE + Fore.LIGHTWHITE_EX + "Done\n")
else:
print(Back.RED + Fore.LIGHTWHITE_EX + "Incorrect Project Name\n")
elif option == '4':
proj_name = input("\nInput the project name: ")
if proj_name in projects:
print(Back.RED + Fore.LIGHTWHITE_EX + "\nA project with that name already exists\n")
else:
projects[proj_name] = 0 # add project to dictionary
print(Back.BLUE + Fore.LIGHTWHITE_EX + "\nDone\n")
elif option == '5':
print("")
for p in projects:
print(f"Name: {Back.MAGENTA + Fore.LIGHTWHITE_EX}{p}{Style.RESET_ALL}")
display_time(projects[p])
proj_name = input("Input the project name: ")
print("")
if proj_name == 'None':
print(Back.RED + Fore.LIGHTWHITE_EX + "Cant delete None\n")
elif proj_name in projects:
if proj_name == active_project: # if you delete the current active project
active_project = 'None'
del projects[proj_name]
print(Back.BLUE + Fore.LIGHTWHITE_EX + "Done\n")
else:
print(Back.RED + Fore.LIGHTWHITE_EX + "Incorrect Project Name\n")
elif option == '6':
print(Back.MAGENTA + Fore.LIGHTWHITE_EX + "\nGoodbye")
colorama.deinit() # stops using colorama
exit(1)
else:
print(Back.RED + Fore.LIGHTWHITE_EX + "\nIncorrect Input\n")
if __name__ == '__main__':
try:
read_file()
except FileNotFoundError: # if file not found create it
write_to_file()
except:
print(Back.RED + Fore.LIGHTWHITE_EX +
"db.txt is damaged, if it can't be fixed please delete it and run me again")
exit(1)
clock_thread = threading.Thread(target=clock)
clock_thread.setDaemon(True) # doesn't hang the program when you try and leave
clock_thread.start()
# print("Clock Process Started\n")
menu()