Compare commits
No commits in common. "770c751d14ad272846f552077a79a0740766b1d0" and "75c0b88d29274deecdf20a02e026e5d2cfdc9bfd" have entirely different histories.
770c751d14
...
75c0b88d29
171
app/Main.hs
171
app/Main.hs
@ -26,27 +26,20 @@ main = do
|
|||||||
putStrLn "Welcome to the Haskell Todo App"
|
putStrLn "Welcome to the Haskell Todo App"
|
||||||
putStrLn "1. Display tasks"
|
putStrLn "1. Display tasks"
|
||||||
putStrLn "2. Add task"
|
putStrLn "2. Add task"
|
||||||
putStrLn "3. Add task (Manual)"
|
putStrLn "3. Filter tasks by tag"
|
||||||
putStrLn "4. Edit task"
|
putStrLn "4. Sort tasks by priority"
|
||||||
putStrLn "5. Filter tasks by tag"
|
putStrLn "5. Exit"
|
||||||
putStrLn "6. Sort tasks by priority"
|
|
||||||
putStrLn "7. Remove task"
|
|
||||||
putStrLn "8. Exit"
|
|
||||||
putStr "Choose an option: "
|
putStr "Choose an option: "
|
||||||
hFlush stdout
|
hFlush stdout
|
||||||
option <- getLine
|
option <- getLine
|
||||||
case option of
|
case option of
|
||||||
"1" -> displayTasks
|
"1" -> displayTasks
|
||||||
"2" -> addTask
|
"2" -> addTask
|
||||||
"3" -> addTaskWithManual
|
"3" -> filterTasksByTag
|
||||||
"4" -> editTask
|
"4" -> sortTasksByPriority
|
||||||
"5" -> filterTasksByTag
|
"5" -> putStrLn "Goodbye!"
|
||||||
"6" -> sortTasksByPriority
|
|
||||||
"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
|
||||||
@ -57,16 +50,12 @@ displayTasks = do
|
|||||||
putStrLn "Tasks:"
|
putStrLn "Tasks:"
|
||||||
mapM_ (putStrLn . showTask) (Map.toList taskMap)
|
mapM_ (putStrLn . showTask) (Map.toList taskMap)
|
||||||
else putStrLn "No tasks found."
|
else putStrLn "No tasks found."
|
||||||
putStrLn " -------------- "
|
main
|
||||||
putStrLn "Press Enter"
|
where
|
||||||
option <- getLine
|
handleReadError :: IOException -> IO Text
|
||||||
case option of
|
handleReadError e = do
|
||||||
_ -> main
|
putStrLn $ "Error reading file: " ++ show e
|
||||||
where
|
return ""
|
||||||
handleReadError :: IOException -> IO Text
|
|
||||||
handleReadError e = do
|
|
||||||
putStrLn $ "Error reading file: " ++ show e
|
|
||||||
return ""
|
|
||||||
|
|
||||||
addTask :: IO ()
|
addTask :: IO ()
|
||||||
addTask = do
|
addTask = do
|
||||||
@ -83,11 +72,7 @@ addTask = do
|
|||||||
catch (TIO.writeFile todoFile (serializeTask newTaskMap)) handleWriteError
|
catch (TIO.writeFile todoFile (serializeTask newTaskMap)) handleWriteError
|
||||||
else catch (TIO.writeFile todoFile (pack task <> "\n")) handleWriteError
|
else catch (TIO.writeFile todoFile (pack task <> "\n")) handleWriteError
|
||||||
putStrLn "Task added."
|
putStrLn "Task added."
|
||||||
putStrLn " -------------- "
|
main
|
||||||
putStrLn "Press Enter"
|
|
||||||
option <- getLine
|
|
||||||
case option of
|
|
||||||
_ -> main
|
|
||||||
where
|
where
|
||||||
handleReadError :: IOException -> IO Text
|
handleReadError :: IOException -> IO Text
|
||||||
handleReadError e = do
|
handleReadError e = do
|
||||||
@ -97,70 +82,6 @@ addTask = do
|
|||||||
handleWriteError :: IOException -> IO ()
|
handleWriteError :: IOException -> IO ()
|
||||||
handleWriteError e = putStrLn $ "Error writing file: " ++ show e
|
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
|
|
||||||
|
|
||||||
|
|
||||||
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): "
|
||||||
@ -175,20 +96,13 @@ filterTasksByTag = do
|
|||||||
putStrLn "Filtered tasks:"
|
putStrLn "Filtered tasks:"
|
||||||
mapM_ (putStrLn . showTask) (Map.toList filteredTasks)
|
mapM_ (putStrLn . showTask) (Map.toList filteredTasks)
|
||||||
else putStrLn "No tasks found."
|
else putStrLn "No tasks found."
|
||||||
putStrLn " -------------- "
|
main
|
||||||
putStrLn "Press Enter"
|
|
||||||
option <- getLine
|
|
||||||
case option of
|
|
||||||
_ -> main
|
|
||||||
where
|
where
|
||||||
handleReadError :: IOException -> IO Text
|
handleReadError :: IOException -> IO Text
|
||||||
handleReadError e = do
|
handleReadError e = do
|
||||||
putStrLn $ "Error reading file: " ++ show e
|
putStrLn $ "Error reading file: " ++ show e
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
handleWriteError :: IOException -> IO ()
|
|
||||||
handleWriteError e = putStrLn $ "Error writing file: " ++ show e
|
|
||||||
|
|
||||||
sortTasksByPriority :: IO ()
|
sortTasksByPriority :: IO ()
|
||||||
sortTasksByPriority = do
|
sortTasksByPriority = do
|
||||||
exists <- doesFileExist todoFile
|
exists <- doesFileExist todoFile
|
||||||
@ -200,65 +114,13 @@ sortTasksByPriority = do
|
|||||||
putStrLn "Sorted tasks by priority:"
|
putStrLn "Sorted tasks by priority:"
|
||||||
mapM_ (putStrLn . showTask) sortedTasks
|
mapM_ (putStrLn . showTask) sortedTasks
|
||||||
else putStrLn "No tasks found."
|
else putStrLn "No tasks found."
|
||||||
putStrLn " -------------- "
|
main
|
||||||
putStrLn "Press Enter"
|
|
||||||
option <- getLine
|
|
||||||
case option of
|
|
||||||
_ -> main
|
|
||||||
where
|
where
|
||||||
handleReadError :: IOException -> IO Text
|
handleReadError :: IOException -> IO Text
|
||||||
handleReadError e = do
|
handleReadError e = do
|
||||||
putStrLn $ "Error reading file: " ++ show e
|
putStrLn $ "Error reading file: " ++ show e
|
||||||
return ""
|
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 :: Text -> Text -> Ordering
|
||||||
comparePriority t1 t2 = comparing getPriority t1 t2
|
comparePriority t1 t2 = comparing getPriority t1 t2
|
||||||
|
|
||||||
@ -275,6 +137,3 @@ serializeTask = Data.Text.unlines . map snd . Map.toList
|
|||||||
|
|
||||||
showTask :: (Int, Text) -> String
|
showTask :: (Int, Text) -> String
|
||||||
showTask (_, task) = unpack task
|
showTask (_, task) = unpack task
|
||||||
|
|
||||||
showTaskId :: (Int, Text) -> String
|
|
||||||
showTaskId (taskId, task) = show taskId ++ ": " ++ unpack task
|
|
Loading…
Reference in New Issue
Block a user