-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
264 lines (207 loc) · 6.14 KB
/
Copy pathsample.py
File metadata and controls
264 lines (207 loc) · 6.14 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
import json
import os
import datetime
# --------- Constants ---------
TASKS_FILE = "tasks.json"
# --------- Utility Functions ---------
def clear_console():
os.system("cls" if os.name == "nt" else "clear")
def pause():
input("\nPress Enter to continue...")
def load_tasks():
if not os.path.exists(TASKS_FILE):
return []
with open(TASKS_FILE, "r") as f:
return json.load(f)
def save_tasks(tasks):
with open(TASKS_FILE, "w") as f:
json.dump(tasks, f, indent=4)
def generate_task_id(tasks):
if not tasks:
return 1
return max(task["id"] for task in tasks) + 1
def print_task(task):
print(f"ID: {task['id']}")
print(f"Title: {task['title']}")
print(f"Description: {task['description']}")
print(f"Due Date: {task['due_date']}")
print(f"Status: {task['status']}")
print("-" * 40)
def print_tasks(tasks):
if not tasks:
print("No tasks found.")
else:
for task in tasks:
print_task(task)
# --------- Core Task Operations ---------
def create_task(tasks):
clear_console()
print("Create New Task")
print("-" * 20)
title = input("Title: ").strip()
description = input("Description: ").strip()
due_date = input("Due Date (YYYY-MM-DD): ").strip()
status = "Pending"
task_id = generate_task_id(tasks)
task = {
"id": task_id,
"title": title,
"description": description,
"due_date": due_date,
"status": status
}
tasks.append(task)
save_tasks(tasks)
print("\nTask created successfully!")
pause()
def edit_task(tasks):
clear_console()
print("Edit Task")
print("-" * 20)
task_id = input("Enter Task ID to edit: ").strip()
if not task_id.isdigit():
print("Invalid ID.")
pause()
return
task_id = int(task_id)
for task in tasks:
if task["id"] == task_id:
print("\nLeave fields blank to keep current value.\n")
print_task(task)
title = input("New Title: ").strip()
description = input("New Description: ").strip()
due_date = input("New Due Date (YYYY-MM-DD): ").strip()
status = input("New Status (Pending/Done): ").strip()
if title:
task["title"] = title
if description:
task["description"] = description
if due_date:
task["due_date"] = due_date
if status in ["Pending", "Done"]:
task["status"] = status
save_tasks(tasks)
print("\nTask updated successfully!")
pause()
return
print("Task not found.")
pause()
def delete_task(tasks):
clear_console()
print("Delete Task")
print("-" * 20)
task_id = input("Enter Task ID to delete: ").strip()
if not task_id.isdigit():
print("Invalid ID.")
pause()
return
task_id = int(task_id)
for i, task in enumerate(tasks):
if task["id"] == task_id:
print("Deleting the following task:")
print_task(task)
confirm = input("Are you sure? (y/n): ").strip().lower()
if confirm == "y":
tasks.pop(i)
save_tasks(tasks)
print("Task deleted.")
else:
print("Deletion cancelled.")
pause()
return
print("Task not found.")
pause()
def mark_task_done(tasks):
clear_console()
print("Mark Task as Done")
print("-" * 20)
task_id = input("Enter Task ID: ").strip()
if not task_id.isdigit():
print("Invalid ID.")
pause()
return
task_id = int(task_id)
for task in tasks:
if task["id"] == task_id:
task["status"] = "Done"
save_tasks(tasks)
print("Task marked as done.")
pause()
return
print("Task not found.")
pause()
def search_tasks(tasks):
clear_console()
print("Search Tasks")
print("-" * 20)
keyword = input("Enter keyword to search: ").strip().lower()
found_tasks = [
task for task in tasks
if keyword in task["title"].lower() or keyword in task["description"].lower()
]
if not found_tasks:
print("No matching tasks found.")
else:
print(f"Found {len(found_tasks)} task(s):\n")
print_tasks(found_tasks)
pause()
def show_pending_tasks(tasks):
clear_console()
print("Pending Tasks")
print("-" * 20)
pending = [task for task in tasks if task["status"] == "Pending"]
print_tasks(pending)
pause()
def show_done_tasks(tasks):
clear_console()
print("Completed Tasks")
print("-" * 20)
done = [task for task in tasks if task["status"] == "Done"]
print_tasks(done)
pause()
# --------- Main Menu ---------
def main_menu():
tasks = load_tasks()
while True:
clear_console()
print("=== Task Manager CLI ===")
print("1. View All Tasks")
print("2. Create Task")
print("3. Edit Task")
print("4. Delete Task")
print("5. Mark Task as Done")
print("6. Search Tasks")
print("7. View Pending Tasks")
print("8. View Completed Tasks")
print("9. Exit")
print("=========================")
choice = input("Enter your choice: ").strip()
if choice == "1":
clear_console()
print("All Tasks")
print("-" * 20)
print_tasks(tasks)
pause()
elif choice == "2":
create_task(tasks)
elif choice == "3":
edit_task(tasks)
elif choice == "4":
delete_task(tasks)
elif choice == "5":
mark_task_done(tasks)
elif choice == "6":
search_tasks(tasks)
elif choice == "7":
show_pending_tasks(tasks)
elif choice == "8":
show_done_tasks(tasks)
elif choice == "9":
print("Exiting Task Manager. Goodbye!")
break
else:
print("Invalid choice.")
pause()
# --------- Run the App ---------
if __name__ == "__main__":
main_menu()