| @@ -41,56 +41,58 @@ fn main() { |
| 41 | |
41 | |
| 42 | let file_path = "tasks.txt"; |
42 | let file_path = "tasks.txt"; |
| 43 | |
43 | |
| 44 | // Handle task completion |
| |
| 45 | if let Some(ids) = matches.get_many::<String>("done") { |
44 | if let Some(ids) = matches.get_many::<String>("done") { |
| 46 | let mut tasks = load_tasks(file_path); |
45 | mark_done(file_path, ids); |
| 47 | let mut completed_tasks = Vec::new(); |
46 | } |
| 48 | for id in ids { |
| |
| 49 | if let Ok(index) = id.parse::<usize>() { |
| |
| 50 | if index > 0 && index <= tasks.len() { |
| |
| 51 | tasks[index - 1].completed = true; // Mark task as complete |
| |
| 52 | } else { |
| |
| 53 | completed_tasks.push(id.clone()); // Collect invalid task IDs |
| |
| 54 | } |
| |
| 55 | } |
| |
| 56 | } |
| |
| 57 | save_tasks(file_path, &tasks); |
| |
| 58 | |
47 | |
| 59 | // Alert for nonexistent tasks |
48 | if let Some(new_tasks) = matches.get_many::<String>("task") { |
| 60 | if !completed_tasks.is_empty() { |
49 | add_tasks(file_path, new_tasks); |
| 61 | for id in completed_tasks { |
50 | } |
| 62 | println!("Task {} doesn't exist.", id); |
| |
| 63 | } |
| |
| 64 | } else { |
| |
| 65 | println!("Tasks marked as complete."); |
| |
| 66 | } |
| |
| 67 | |
51 | |
| 68 | // Display the updated task list |
52 | // Display saved tasks if no new tasks or done tasks were provided |
| |
53 | if !matches.contains_id("task") && !matches.contains_id("done") { |
| 69 | display_tasks(file_path); |
54 | display_tasks(file_path); |
| 70 | } |
55 | } |
| |
56 | } |
| 71 | |
57 | |
| 72 | // Handle new tasks |
58 | fn mark_done<'a>(file_path: &str, ids: impl Iterator<Item = &'a String>) { |
| 73 | if let Some(new_tasks) = matches.get_many::<String>("task") { |
59 | let mut tasks = load_tasks(file_path); |
| 74 | let mut tasks = load_tasks(file_path); |
60 | let mut missing = Vec::new(); |
| 75 | let next_id = tasks.len() + 1; // Determine the next task ID |
61 | for id in ids { |
| 76 | for task_desc in new_tasks { |
62 | if let Ok(index) = id.parse::<usize>() { |
| 77 | tasks.push(Task { |
63 | if index > 0 && index <= tasks.len() { |
| 78 | id: next_id, |
64 | tasks[index - 1].completed = true; |
| 79 | description: task_desc.to_string(), |
65 | } else { |
| 80 | completed: false, |
66 | missing.push(id.clone()); |
| 81 | }); |
67 | } |
| 82 | } |
68 | } |
| 83 | save_tasks(file_path, &tasks); |
69 | } |
| 84 | println!("Tasks saved to {}", file_path); |
70 | save_tasks(file_path, &tasks); |
| 85 | |
71 | |
| 86 | // Display the updated task list |
72 | if missing.is_empty() { |
| 87 | display_tasks(file_path); |
73 | println!("Tasks marked as complete."); |
| |
74 | } |
| |
75 | for id in missing { |
| |
76 | println!("Task {} doesn't exist.", id); |
| 88 | } |
77 | } |
| 89 | |
78 | |
| 90 | // Display saved tasks if no new tasks or done tasks were provided |
79 | display_tasks(file_path); |
| 91 | if !matches.contains_id("task") && !matches.contains_id("done") { |
80 | } |
| 92 | display_tasks(file_path); |
81 | |
| |
82 | fn add_tasks<'a>(file_path: &str, new_tasks: impl Iterator<Item = &'a String>) { |
| |
83 | let mut tasks = load_tasks(file_path); |
| |
84 | let next_id = tasks.len() + 1; |
| |
85 | for task_desc in new_tasks { |
| |
86 | tasks.push(Task { |
| |
87 | id: next_id, |
| |
88 | description: task_desc.to_string(), |
| |
89 | completed: false, |
| |
90 | }); |
| 93 | } |
91 | } |
| |
92 | save_tasks(file_path, &tasks); |
| |
93 | println!("Tasks saved to {}", file_path); |
| |
94 | |
| |
95 | display_tasks(file_path); |
| 94 | } |
96 | } |
| 95 | |
97 | |
| 96 | fn load_tasks(file_path: &str) -> Vec<Task> { |
98 | fn load_tasks(file_path: &str) -> Vec<Task> { |