From 6bbe5f60f5a3091758f6156211abe722256da012 Mon Sep 17 00:00:00 2001 From: s464971 Date: Sun, 26 May 2024 22:39:58 +0200 Subject: [PATCH] add --- app/Main.hs | 121 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 105 insertions(+), 16 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index f120ca9..2c14b12 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -26,18 +26,22 @@ main = do putStrLn "Welcome to the Haskell Todo App" putStrLn "1. Display tasks" putStrLn "2. Add task" - putStrLn "3. Filter tasks by tag" - putStrLn "4. Sort tasks by priority" - putStrLn "5. Exit" + putStrLn "3. Add task (Manual)" + putStrLn "4. Filter tasks by tag" + putStrLn "5. Sort tasks by priority" + putStrLn "6. Remove task" + putStrLn "7. Exit" putStr "Choose an option: " hFlush stdout option <- getLine case option of "1" -> displayTasks "2" -> addTask - "3" -> filterTasksByTag - "4" -> sortTasksByPriority - "5" -> putStrLn "Goodbye!" + "3" -> addTaskWithManual + "4" -> filterTasksByTag + "5" -> sortTasksByPriority + "6" -> removeTask + "7" -> putStrLn "Goodbye!" _ -> putStrLn "Invalid option" >> main displayTasks :: IO () @@ -50,12 +54,16 @@ displayTasks = do putStrLn "Tasks:" mapM_ (putStrLn . showTask) (Map.toList taskMap) else putStrLn "No tasks found." - main - where - handleReadError :: IOException -> IO Text - handleReadError e = do - putStrLn $ "Error reading file: " ++ show e - return "" + 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 "" addTask :: IO () addTask = do @@ -72,7 +80,11 @@ addTask = do catch (TIO.writeFile todoFile (serializeTask newTaskMap)) handleWriteError else catch (TIO.writeFile todoFile (pack task <> "\n")) handleWriteError putStrLn "Task added." - main + putStrLn " -------------- " + putStrLn "Press Enter" + option <- getLine + case option of + _ -> main where handleReadError :: IOException -> IO Text handleReadError e = do @@ -82,6 +94,21 @@ addTask = do handleWriteError :: IOException -> IO () handleWriteError e = putStrLn $ "Error writing file: " ++ show e +addTaskWithManual :: IO () +addTaskWithManual = do + putStrLn "First, optionally you can add priority to the task you want to add. To do so, put letter inside '()'" + putStrLn "Tasks without priority will be displayed first." + putStrLn "Second, write your task." + putStrLn "Third, optionally you can add tags to your task. To do so, put '+'and write your tag." + putStrLn "Tasks can have more than one tag." + putStrLn "Example: (C) go shopping +shop +sunday" + putStrLn " -------------- " + putStrLn "Press Enter to add your task or x to go back to menu" + option <- getLine + case option of + "x" -> main + _ -> addTask + filterTasksByTag :: IO () filterTasksByTag = do putStr "Enter the tag to filter by (e.g., +GarageSale): " @@ -96,13 +123,20 @@ filterTasksByTag = do putStrLn "Filtered tasks:" mapM_ (putStrLn . showTask) (Map.toList filteredTasks) else putStrLn "No tasks found." - main + 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 + sortTasksByPriority :: IO () sortTasksByPriority = do exists <- doesFileExist todoFile @@ -114,13 +148,65 @@ sortTasksByPriority = do putStrLn "Sorted tasks by priority:" mapM_ (putStrLn . showTask) sortedTasks else putStrLn "No tasks found." - main + 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 "" +removeTask :: IO () +removeTask = 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 remove: " + hFlush stdout + taskIdStr <- getLine + let taskId = read taskIdStr :: Int + + putStrLn "" + + if Map.member taskId taskMap + then do + let newTaskMap = Map.delete taskId taskMap + catch (TIO.writeFile todoFile (serializeTask newTaskMap)) handleWriteError + putStrLn "Task removed." + 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 + _ -> removeTask + + 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 + + + comparePriority :: Text -> Text -> Ordering comparePriority t1 t2 = comparing getPriority t1 t2 @@ -136,4 +222,7 @@ serializeTask :: TaskMap -> Text serializeTask = Data.Text.unlines . map snd . Map.toList showTask :: (Int, Text) -> String -showTask (_, task) = unpack task \ No newline at end of file +showTask (_, task) = unpack task + +showTaskId :: (Int, Text) -> String +showTaskId (taskId, task) = show taskId ++ ": " ++ unpack task \ No newline at end of file