This commit is contained in:
s464971 2024-05-26 23:20:11 +02:00
parent 6bbe5f60f5
commit 5c766c5e89
1 changed files with 60 additions and 8 deletions

View File

@ -27,10 +27,11 @@ main = do
putStrLn "1. Display tasks" putStrLn "1. Display tasks"
putStrLn "2. Add task" putStrLn "2. Add task"
putStrLn "3. Add task (Manual)" putStrLn "3. Add task (Manual)"
putStrLn "4. Filter tasks by tag" putStrLn "4. Edit task"
putStrLn "5. Sort tasks by priority" putStrLn "5. Filter tasks by tag"
putStrLn "6. Remove task" putStrLn "6. Sort tasks by priority"
putStrLn "7. Exit" putStrLn "7. Remove task"
putStrLn "8. Exit"
putStr "Choose an option: " putStr "Choose an option: "
hFlush stdout hFlush stdout
option <- getLine option <- getLine
@ -38,12 +39,14 @@ main = do
"1" -> displayTasks "1" -> displayTasks
"2" -> addTask "2" -> addTask
"3" -> addTaskWithManual "3" -> addTaskWithManual
"4" -> filterTasksByTag "4" -> editTask
"5" -> sortTasksByPriority "5" -> filterTasksByTag
"6" -> removeTask "6" -> sortTasksByPriority
"7" -> putStrLn "Goodbye!" "7" -> removeTask
"8" -> putStrLn "Goodbye!"
_ -> putStrLn "Invalid option" >> main _ -> putStrLn "Invalid option" >> main
displayTasks :: IO () displayTasks :: IO ()
displayTasks = do displayTasks = do
exists <- doesFileExist todoFile exists <- doesFileExist todoFile
@ -109,6 +112,55 @@ addTaskWithManual = do
"x" -> main "x" -> main
_ -> addTask _ -> 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 :: IO ()
filterTasksByTag = do filterTasksByTag = do
putStr "Enter the tag to filter by (e.g., +GarageSale): " putStr "Enter the tag to filter by (e.g., +GarageSale): "