-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathmain.py
More file actions
342 lines (287 loc) · 11.1 KB
/
Copy pathmain.py
File metadata and controls
342 lines (287 loc) · 11.1 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
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
"""
🎮 Python Mini Projects — Interactive Launcher Menu
===================================================
A central, interactive CLI menu at the root level to browse,
search, and launch all games, math utilities, and other tools.
"""
import os
import subprocess
import sys
import json
REGISTRY_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "projects_registry.json")
try:
with open(REGISTRY_PATH, "r", encoding="utf-8") as f:
PROJECTS = json.load(f)
except Exception as e:
print(f"Error loading project registry: {e}")
PROJECTS = []
DIFFICULTY_BADGES = {
"beginner": "🟢 Beg",
"intermediate": "🟡 Int",
"advanced": "🔴 Adv",
}
CATEGORY_EMOJIS = {
"games": "🎮",
"math": "🔢",
"utilities": "🔧",
}
# ── Keyboard Shortcuts ─────────────────────────────────────
# Users can override these by creating keyboard_shortcuts.json
# in the same directory as main.py. See `show_help()` for details.
KEYBOARD_SHORTCUTS = {
"main_menu": {
"g": "games",
"m": "math",
"u": "utilities",
"s": "search",
"l": "list_all",
"q": "exit",
"?": "help",
"h": "help",
},
"global": {
"?": "help",
"h": "help",
"b": "back",
},
}
# Attempt to load user-defined shortcuts
_SHORTCUTS_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "keyboard_shortcuts.json")
if os.path.exists(_SHORTCUTS_PATH):
try:
with open(_SHORTCUTS_PATH, "r", encoding="utf-8") as f:
_user_shortcuts = json.load(f)
KEYBOARD_SHORTCUTS["main_menu"].update(_user_shortcuts.get("main_menu", {}))
KEYBOARD_SHORTCUTS["global"].update(_user_shortcuts.get("global", {}))
except Exception:
pass
def print_header():
print("\n" + "═" * 60)
print(" 🚀 PYTHON MINI PROJECTS — INTERACTIVE LAUNCHER")
print("═" * 60)
def print_footer():
print("═" * 60)
def show_help():
"""Display a help modal with all available keyboard shortcuts."""
os.system('cls' if os.name == 'nt' else 'clear')
print_header()
print(" ⌨️ KEYBOARD SHORTCUTS — Press any key to return")
print("─" * 60)
print(" MAIN MENU shortcuts:")
print(" g → Games")
print(" m → Math Utilities")
print(" u → General Utilities")
print(" s → Search Projects")
print(" l → List All Projects")
print(" q → Exit")
print(" ? or h → Show this help")
print(" SUB-MENU shortcuts:")
print(" b → Back to previous menu")
print(" ? or h → Show this help")
print(" NUMERIC input:")
print(" You can still type numbers to select menu items")
print("─" * 60)
print(" CUSTOMIZATION:")
print(" Create keyboard_shortcuts.json in the project root")
print(" to remap any shortcut. Example:")
print(' {"main_menu": {"x": "exit", "1": "games"}}')
print_footer()
input(" 👉 Press Enter to return...")
def list_projects_by_category(category_name):
filtered = [p for p in PROJECTS if p["category"] == category_name]
filtered_sorted = sorted(filtered, key=lambda p: p["name"])
return filtered_sorted
def launch_project(path):
if not os.path.exists(path):
print(f"\n❌ Error: File not found at '{path}'")
input("\nPress Enter to return to menu...")
return
print(f"\n🚀 Launching: {os.path.basename(path)}")
print("─" * 60 + "\n")
try:
# Run with current python executable
subprocess.run([sys.executable, path])
except Exception as e:
print(f"\n❌ Error executing script: {e}")
print("\n" + "─" * 60)
input("ℹ️ Script finished. Press Enter to return to the launcher...")
def _handle_global_shortcuts(choice, allow_back=True):
"""Return a canonical action string for global shortcuts, or None."""
lowered = choice.lower().strip()
if lowered in KEYBOARD_SHORTCUTS["global"]:
action = KEYBOARD_SHORTCUTS["global"][lowered]
if action == "back" and allow_back:
return "back"
if action == "help":
show_help()
return "help_shown"
return None
def main_menu():
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print_header()
print(" Please select a category to browse:")
print("\n [1] 🎮 Games (or press 'g')")
print(" [2] 🔢 Math (or press 'm')")
print(" [3] 🔧 Utilities (or press 'u')")
print(" [4] 🔍 Search (or press 's')")
print(" [5] 📋 List All (or press 'l')")
print(" [6] ❌ Exit (or press 'q')")
print(" [?] ❓ Help (press '?' or 'h')")
print_footer()
choice = input("👉 Enter choice (1-6, or shortcut key): ").strip()
# Single-key shortcuts
lowered = choice.lower()
if lowered in KEYBOARD_SHORTCUTS["main_menu"]:
action = KEYBOARD_SHORTCUTS["main_menu"][lowered]
if action == "help":
show_help()
continue
elif action == "exit":
print("\n👋 Happy Coding! Goodbye.\n")
break
elif action == "games":
category_menu("games", "Games")
elif action == "math":
category_menu("math", "Math Utilities")
elif action == "utilities":
category_menu("utilities", "General Utilities")
elif action == "search":
search_menu()
elif action == "list_all":
list_all_menu()
continue
if lowered in KEYBOARD_SHORTCUTS["global"]:
g_action = _handle_global_shortcuts(choice, allow_back=False)
if g_action == "help_shown":
continue
if choice == "1":
category_menu("games", "Games")
elif choice == "2":
category_menu("math", "Math Utilities")
elif choice == "3":
category_menu("utilities", "General Utilities")
elif choice == "4":
search_menu()
elif choice == "5":
list_all_menu()
elif choice == "6":
print("\n👋 Happy Coding! Goodbye.\n")
break
else:
input("\n⚠️ Invalid selection. Press Enter to try again...")
def category_menu(category_key, category_title):
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print_header()
print(f" 📂 Category: {category_title}")
print("─" * 60)
items = list_projects_by_category(category_key)
for idx, item in enumerate(items, start=1):
difficulty = DIFFICULTY_BADGES.get(item["difficulty"], item["difficulty"])
print(f" [{idx:2d}] {item['emoji']} {item['name']:30s} [{difficulty}]")
print(f" {item['description']}")
print()
print(f" [B] 🔙 Back to Main Menu")
print(f" [?] ❓ Help")
print_footer()
choice = input("👉 Select project number (or 'b' / '?'): ").strip().lower()
g_action = _handle_global_shortcuts(choice, allow_back=True)
if g_action == "back":
break
if g_action == "help_shown":
continue
if choice == 'b':
break
try:
val = int(choice)
if 1 <= val <= len(items):
launch_project(items[val - 1]["path"])
else:
input("\n⚠️ Number out of range. Press Enter to try again...")
except ValueError:
input("\n⚠️ Invalid input. Please enter a number, 'b', or '?'. Press Enter to try again...")
def search_menu():
os.system('cls' if os.name == 'nt' else 'clear')
print_header()
print(" 🔍 Search Projects")
print_footer()
query = input("👉 Enter search keyword (e.g., game, solver, cipher): ").strip().lower()
if not query:
return
results = [
p for p in PROJECTS
if query in p["name"].lower()
or query in p["description"].lower()
or any(query in kw.lower() for kw in p["keywords"])
]
if not results:
input("\n⚠️ No matching projects found. Press Enter to return to main menu...")
return
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print_header()
print(f" 🔍 Search Results for '{query}':")
print("─" * 60)
for idx, item in enumerate(results, start=1):
cat_emoji = CATEGORY_EMOJIS.get(item["category"], "")
difficulty = DIFFICULTY_BADGES.get(item["difficulty"], item["difficulty"])
print(f" [{idx:2d}] {cat_emoji} {item['name']:30s} [{difficulty}]")
print(f" {item['description']}")
print()
print(f" [B] 🔙 Back to Main Menu")
print(f" [?] ❓ Help")
print_footer()
choice = input("👉 Select project number (or 'b' / '?'): ").strip().lower()
g_action = _handle_global_shortcuts(choice, allow_back=True)
if g_action == "back":
break
if g_action == "help_shown":
continue
if choice == 'b':
break
try:
val = int(choice)
if 1 <= val <= len(results):
launch_project(results[val - 1]["path"])
else:
input("\n⚠️ Number out of range. Press Enter to try again...")
except ValueError:
input("\n⚠️ Invalid input. Please enter a number, 'b', or '?'. Press Enter to try again...")
def list_all_menu():
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print_header()
print(" 📋 All Projects")
print("─" * 60)
sorted_all = sorted(PROJECTS, key=lambda p: (p["category"], p["name"]))
for idx, item in enumerate(sorted_all, start=1):
cat_emoji = CATEGORY_EMOJIS.get(item["category"], "")
difficulty = DIFFICULTY_BADGES.get(item["difficulty"], item["difficulty"])
print(f" [{idx:2d}] {cat_emoji} {item['name']:30s} [{difficulty}]")
print(f" {item['description']}")
print()
print(f" [B] 🔙 Back to Main Menu")
print(f" [?] ❓ Help")
print_footer()
choice = input("👉 Select project number (or 'b' / '?'): ").strip().lower()
g_action = _handle_global_shortcuts(choice, allow_back=True)
if g_action == "back":
break
if g_action == "help_shown":
continue
if choice == 'b':
break
try:
val = int(choice)
if 1 <= val <= len(sorted_all):
launch_project(sorted_all[val - 1]["path"])
else:
input("\n⚠️ Number out of range. Press Enter to try again...")
except ValueError:
input("\n⚠️ Invalid input. Please enter a number, 'b', or '?'. Press Enter to try again...")
if __name__ == "__main__":
try:
main_menu()
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")