-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenues.py
206 lines (192 loc) · 6.5 KB
/
menues.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
# This file gives the menues and does the appropriate actions for the selections
# Some other functions are included to reduce clutter and imports
import datetime
import os
import shutil
import inquirer
# import from other self-made modules
from graphs import *
from models import (
get_dates_and_hours,
get_skill_names,
create_skill,
set_goal,
get_stats,
delete_skill,
BASE,
DATABASE,
)
from output import lvlup_help, show_stats
def main_menu():
"""
Provide main navigation options. Runs in an infinite loop, until exited"""
clear_screen() # clear screen before displaying option
# list with all possible menu options
menu_options = ["Improve Skill", "Insights", "Settings", "Exit"]
# inquirer to display the menu
menu = [
inquirer.List(
"main_menu",
message="What do you want to do?",
choices=menu_options,
carousel=True,
default="Improve Skill",
),
]
# get the choice from the menu as a string
choice = inquirer.prompt(menu)["main_menu"]
# logic for selections, compared with menu_options
if choice == menu_options[0]:
skill = skill_menu() # return the skill
return skill # breaks out of loop, further actions in main.py
elif choice == menu_options[1]:
insight_menu() # call the insight menu
elif choice == menu_options[2]:
settings_menu() # call the settings menu
elif choice == menu_options[3]:
print("Goodbye! See you soon.")
exit() # break out of the programs
def skill_menu():
"""Let the user pick a skill to improve.
Used as a submenu for verious actions."""
clear_screen() # start with a fresh screen
# get skills from the Skill model
menu_options = [skill for skill in get_skill_names()]
menu_options.append("Go Back") # include the option to get back
# inquirer logic
menu = [
inquirer.List(
"skill_menu", message="Pick a skill", choices=menu_options, carousel=True,
),
]
# store selection in string
choice = inquirer.prompt(menu)["skill_menu"]
if choice == menu_options[-1]:
main_menu() # if go back, call main_menu
else:
return choice # return the skill choice for next action
def settings_menu():
"""Let the user choose a setting"""
clear_screen() # start with fresh screen
# all options implemented
menu_options = [
"Add a new skill",
"Delete a skill",
"Set a daily goal",
"Create backup",
"Display Help",
"Go Back",
]
# inquirer logic
menu = [
inquirer.List(
"settings_menu",
message="Chose your setting",
choices=menu_options,
carousel=True,
),
]
choice = inquirer.prompt(menu)["settings_menu"]
if choice == menu_options[0]:
# Add a new skill
create_skill() # call function to create a new skill
elif choice == menu_options[1]:
# delete a skill
skillname = skill_menu() # get the skill from skill_menu
# confirmation to delete the skill
key = input(f"Are you 100% sure you want to delete {skillname}? [y/N] ")
if key.lower() == "y":
delete_skill(skillname) # actually delete the skill
# give user feedback
input(f"Deleted {skillname}\nPress ENTER to continue...")
elif choice == menu_options[2]:
# Set a daily goal for a skill
skillname = skill_menu() # choose the skill
print("\nHow many minutes do you plan on investing per day?")
set_goal(skillname) # call function to set goal in Skill model
elif choice == menu_options[3]:
# create a backup
file_path = backup_db() # create backup and return filepath
# give user feedback, let them know where the backup is
print(f"Backup successfully created at {file_path}")
input("Press ENTER to continue...")
elif choice == menu_options[4]:
# display the help string
lvlup_help()
elif choice == menu_options[-1]:
# go back to main menu
main_menu()
def insight_menu():
"""Let the user select the skills to get insights for"""
clear_screen() # start with a fresh screen
# two menues, first select a skill, then the kind of insight to display
menu_options_1 = [skill for skill in get_skill_names()]
menu_options_2 = [
"Stats",
"Graph: All Time",
"Graph: Past Month",
"Graph: Past Week",
]
menu = [
# checkbox allows for multiple selections
inquirer.Checkbox(
"skills",
message="Pick your skills by pressing the SPACE BAR",
choices=menu_options_1,
),
inquirer.List(
"insights",
message="Pick what insight to display",
choices=menu_options_2,
carousel=True,
),
]
# prompt the menu
choices = inquirer.prompt(menu)
# selected skills
skill_list = choices["skills"]
# kind of insight to display
insight = choices["insights"]
# make sure something is selected
if len(skill_list) == 0:
print(
"\n\nERROR:\nOh no! Looks like you didn't pick a skill!\nPick at least one skill!"
)
input("Press ENTER to continue...")
insight_menu()
else:
# call appropriate insight
skills_dict = get_dates_and_hours(skill_list) # get data for selection
if insight == menu_options_2[0]:
clear_screen()
print(show_stats(get_stats(skills_dict)))
input("Press ENTER to continue...")
elif insight == menu_options_2[1]:
# all time progress
plot_cumulated_progress(skills_dict, start=0)
elif insight == menu_options_2[2]:
# monthly time investment
plot_minutes(skills_dict, start=-30)
elif insight == menu_options_2[3]:
# weekly time investment
plot_minutes(skills_dict, start=-7)
def clear_screen():
"""Clear the terminal screen"""
os.system("cls" if os.name == "nt" else "clear")
def backup_db():
"""Create a backup of the database"""
# contruct filepath and name for backup
new_backup = (
BASE
+ os.sep
+ "db"
+ os.sep
+ "backup-"
+ datetime.datetime.now().strftime("%Y-%m-%d")
+ ".db"
)
# copy the existing database to new filename
shutil.copy2(
DATABASE, new_backup,
)
return new_backup