-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·368 lines (322 loc) · 12.5 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from rich.console import Console
from rich.table import Table
from datetime import timedelta, date
from tinydb import TinyDB, Query
import time
import sys
import cursor
from user import (
User,
Settings,
Run,
Day,
DEFAULT_USERNAME,
POMODORO,
SHORT_BREAK,
LONG_BREAK,
)
def show_time_left(console: Console, time_left: int):
"""
Function called every second timer is active
"""
console.print(f"{time_left}", end="\r")
def start_timer(console: Console, session_length_sec: int):
"""
Function for running the timer
"""
time_left_seconds = session_length_sec
# Run the timer until the timer hits zero or user signal to stop
active = True
while active:
try:
# Update the timer and print the time to the terminal
while time_left_seconds >= 0:
# Convert seconds into timedelta object
time_left = timedelta(seconds=time_left_seconds)
show_time_left(console, time_left) # Print time left
time.sleep(1.0) # Wait a second to update time
time_left_seconds -= 1
active = False
print("")
# If the user is trying to exit the timer or program by sending a signal (Ctrl-C)
except KeyboardInterrupt:
console.print("\nTime paused!", style="bold red")
console.print("(1) Continue", style="bold red")
console.print("(2) Finish early", style="bold red")
again = True
# Prompt the user for input until it is validly provided
while again:
option = get_user_input(console)
# User input of 1 to continue timer and 2 to end timer
if option == 1:
again = False
elif option == 2:
return session_length_sec - time_left_seconds
# Notification that the timer is up
console.print("Time is up!", style="bold red")
return session_length_sec
def print_settings(console: Console, settings: Settings):
"""
Print the available settings
"""
console.print("--------------------------------", style="red")
console.print("Settings", style="bold red")
console.print(f"(1) Pomodoro: {settings.pomodoro}", style="bold red")
console.print(f"(2) Short break: {settings.short_break}", style="bold red")
console.print(f"(3) Long break: {settings.long_break}", style="bold red")
console.print(
f"(4) Sessions until each long break: {settings.sessions_until_long}",
style="bold red",
)
console.print(f"(5) Auto start sessions: {settings.auto_start}", style="bold red")
console.print("(0) Return to menu", style="bold red")
console.print("--------------------------------", style="red")
def get_user_input(console: Console):
"""
Function for getting user input with error handling
"""
choice = 0
again = True
while again:
try:
choice = int(console.input("[bold red]:[/] "))
again = False
except ValueError:
console.print("Input must be an integer", style="bold red")
return choice
def view_settings(console: Console, settings: Settings):
"""
Display the settings and allow for changes to be made
"""
print_settings(console, settings)
again = True
while again:
try:
choice = int(console.input("[bold red]:[/] "))
if choice < 0 or choice > 5:
continue
elif choice == 0:
break
elif choice == 1:
console.print("New pomodoro time (max of 150 mins.)", style="red bold")
value = get_user_input(console)
settings.pomodoro = value
elif choice == 2:
console.print(
"New short break time (max of 20 mins.)", style="red bold"
)
value = get_user_input(console)
settings.short_break = value
elif choice == 3:
console.print("New long break time (max of 40 mins.)", style="red bold")
value = get_user_input(console)
settings.long_break = value
elif choice == 4:
console.print(
"New number of sessions until each long break (max of 10)",
style="red bold",
)
value = get_user_input(console)
settings.sessions_until_long = value
elif choice == 5:
console.print("Auto start sessions (y or n)", style="red bold")
yes_or_no = input()
if yes_or_no.lower() == "y":
settings.auto_start = True
elif yes_or_no.lower() == "n":
settings.auto_start = False
again = False
except ValueError:
console.print("Input must be an integer", style="bold red")
def change_session(console: Console):
"""
Allow for changing the current session type and return the user's numerical choice
"""
console.print("Change the current session type: ", style="red bold")
console.print("(1) Pomodoro", style="red bold")
console.print("(2) Short break", style="red bold")
console.print("(3) Long break", style="red bold")
console.print("(0) Return to menu", style="red bold")
again = True
while again:
choice = get_user_input(console)
if choice >= 0 and choice <= 3:
return choice - 1
def init_db():
"""
Initialize and return database object
"""
db = TinyDB("db.json")
if not db.all():
# Set default settings for first-time user
db.insert(User())
return db
def next_session(user: User, run: Run):
"""
Change to the next session type
"""
if (
run.current_session_type == SHORT_BREAK
or run.current_session_type == LONG_BREAK
): # If it is a break session then next session is pomodoro
run.current_session_type = POMODORO
elif (
run.current_session_type == POMODORO
): # Pomodoro session is proceeded by either a short or long break
if run.current_session_num % (user.settings.sessions_until_long) == 0:
run.current_session_type = LONG_BREAK
else:
run.current_session_type = SHORT_BREAK
run.current_session_num += 1
def view_stats(console: Console, user: User, run: Run):
"""
Print overall and daily statistics
"""
console.print("Overall Statistics: ", style="red bold")
console.print(
f"Total Time Studied: {timedelta(seconds = user.total_time_studied + run.total_time_studied)}\n",
style="red bold",
)
table = Table(title="Daily Statistics")
table.add_column("Date", justify="center", style="cyan", no_wrap=True)
table.add_column("Time Studied", justify="center", style="magenta")
table.add_column("Sessions Completed", justify="center", style="green")
for day in user.days:
table.add_row(
str(day.date),
str(timedelta(seconds=day.time_studied)),
str(day.sessions_completed),
)
table.add_row(
"Current",
str(timedelta(seconds=run.total_time_studied)),
str(run.current_session_num - 1),
)
console.print(table)
def replace_underscore(input: str):
return input.replace("_", " ")
def get_session_type(input: int):
if input == 0:
return POMODORO
elif input == 1:
return SHORT_BREAK
elif input == 2:
return LONG_BREAK
return "invalid"
def main():
db = init_db()
"""
Initialize variables
"""
console = Console(width=40)
cursor.hide()
# Get the first item in the database (there should only be one item)
data = db.all()[0]
user = User()
user.set_values(data)
run = Run()
choice = 0
while True:
"""
Print main menu information after each session or when returning from another menu
"""
console.print("Pomodoro Timer", style="dark_slate_gray2 on red", justify="left")
console.print("--------------------------------", style="red")
console.print(
f"Current session: {replace_underscore(run.current_session_type)}",
style="bold red",
)
# Only show session number when it is a pomodoro session
if run.current_session_type == POMODORO:
console.print(f"Session #{run.current_session_num}", style="bold red")
console.print(
f"Session length: {user.get_session_length(run.current_session_type)} minutes",
style="bold red",
)
console.print(
f"Total time studied: {timedelta(seconds = run.total_time_studied)}",
style="bold red",
)
console.print("--------------------------------", style="red")
console.print("Press 1 to start session.", style="red bold")
console.print("Press 2 to adjust settings.", style="red bold")
console.print("Press 3 to change current session.", style="red bold")
console.print("Press 4 to see your stats.", style="red bold")
console.print("Press 0 to quit.", style="red bold")
again = True
while again:
choice = get_user_input(console)
if choice >= 0 and choice <= 4:
again = False
"""
Perform action depending on user's main menu input
"""
if choice == 1: # Start session
restart = True
while restart: # Start sessions automatically
session_time_studied = start_timer(
console,
user.get_session_length(run.current_session_type) * 60,
)
if (
run.current_session_type == POMODORO
): # Only add session time if it is the pomodoro session
run.total_time_studied += session_time_studied
if (
session_time_studied
< user.get_session_length(run.current_session_type) * 60
or user.settings.auto_start is False
): # Don't auto start
restart = False
continue
next_session(user, run.current_session_type) # Change session
console.print(
f"Current session: {replace_underscore(run.current_session_type)}",
style="bold red",
)
elif choice == 2: # View settings
view_settings(console, user.settings)
continue
elif choice == 3: # View session change
new_session = get_session_type(change_session(console))
if new_session != "invalid":
run.current_session_type = new_session
continue
elif choice == 4: # View statistics
view_stats(console, user, run)
continue
elif choice == 0: # Exit program
# Track time studied across all program runs
user.total_time_studied += run.total_time_studied
today = date.today().strftime("%m/%d/%y")
day_obj = None
for day in user.days: # Look for today's date to update the data
if day.date == today:
day_obj = day
if day_obj: # If data for today exists
program_run_data = {
"date": day_obj.date,
"time_studied": day_obj.time_studied + run.total_time_studied,
"sessions_completed": day_obj.sessions_completed
+ run.current_session_num
- 1,
}
# Remove the previous data for today
user.days.pop()
else: # If this is the first data for today
program_run_data = {
"date": today,
"time_studied": run.total_time_studied,
"sessions_completed": run.current_session_num - 1,
}
# Add the data for today to data for all days
new_day = Day()
new_day.set_values(program_run_data)
user.days.append(new_day)
# Update the database
db.update(user.get_dict(), Query().name == DEFAULT_USERNAME)
sys.exit(0) # Exit the program
# Change the current session and update the session number
next_session(user, run)
if __name__ == "__main__":
main() # Start of program