diff --git a/app/Main.hs b/app/Main.hs index 2c14b12..6254ed5 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -27,10 +27,11 @@ main = do putStrLn "1. Display tasks" putStrLn "2. Add task" putStrLn "3. Add task (Manual)" - putStrLn "4. Filter tasks by tag" - putStrLn "5. Sort tasks by priority" - putStrLn "6. Remove task" - putStrLn "7. Exit" + putStrLn "4. Edit task" + putStrLn "5. Filter tasks by tag" + putStrLn "6. Sort tasks by priority" + putStrLn "7. Remove task" + putStrLn "8. Exit" putStr "Choose an option: " hFlush stdout option <- getLine @@ -38,12 +39,14 @@ main = do "1" -> displayTasks "2" -> addTask "3" -> addTaskWithManual - "4" -> filterTasksByTag - "5" -> sortTasksByPriority - "6" -> removeTask - "7" -> putStrLn "Goodbye!" + "4" -> editTask + "5" -> filterTasksByTag + "6" -> sortTasksByPriority + "7" -> removeTask + "8" -> putStrLn "Goodbye!" _ -> putStrLn "Invalid option" >> main + displayTasks :: IO () displayTasks = do exists <- doesFileExist todoFile @@ -109,6 +112,55 @@ addTaskWithManual = do "x" -> main _ -> addTask + +editTask :: IO () +editTask = do + exists <- doesFileExist todoFile + if exists + then do + contents <- catch (TIO.readFile todoFile) handleReadError + let taskMap = parseTask contents + putStrLn "Existing tasks: " + mapM_ (putStrLn . showTaskId) (Map.toList taskMap) + + putStr "Enter the ID of the task you wish to edit: " + hFlush stdout + taskIdStr <- getLine + let taskId = read taskIdStr :: Int + + putStrLn "" + + if Map.member taskId taskMap + then do + putStrLn "Enter the new task description: " + newTask <- getLine + let newTaskMap = Map.insert taskId (pack newTask) taskMap + catch (TIO.writeFile todoFile (serializeTask newTaskMap)) handleWriteError + putStrLn "Task edited." + else do + putStrLn "Task of this ID doesn't exist" + putStrLn "Do you wish to try again? Press Enter if yes or x to go back to menu" + option <- getLine + case option of + "x" -> main + _ -> editTask + + else putStrLn "No tasks found." + putStrLn " -------------- " + putStrLn "Press Enter" + option <- getLine + case option of + _ -> main + where + handleReadError :: IOException -> IO Text + handleReadError e = do + putStrLn $ "Error reading file: " ++ show e + return "" + + handleWriteError :: IOException -> IO () + handleWriteError e = putStrLn $ "Error writing file: " ++ show e + + filterTasksByTag :: IO () filterTasksByTag = do putStr "Enter the tag to filter by (e.g., +GarageSale): "