{-# LANGUAGE OverloadedStrings #-} module Main where import System.IO import System.Directory import Data.Text (Text, pack, unpack, isInfixOf, lines, unlines) import qualified Data.Text.IO as TIO import Control.Monad (when) import Data.List (sortBy) import Data.Ord (comparing) import Data.Char (isAlpha) todoFile :: FilePath todoFile = "todo.txt" main :: IO () 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" putStr "Choose an option: " hFlush stdout option <- getLine case option of "1" -> displayTasks "2" -> addTask "3" -> filterTasksByTag "4" -> sortTasksByPriority "5" -> putStrLn "Goodbye!" _ -> putStrLn "Invalid option" >> main displayTasks :: IO () displayTasks = do exists <- doesFileExist todoFile if exists then do contents <- TIO.readFile todoFile putStrLn "Tasks:" TIO.putStrLn contents else putStrLn "No tasks found." main addTask :: IO () addTask = do putStr "Enter the task: " hFlush stdout task <- getLine TIO.appendFile todoFile (pack task <> "\n") putStrLn "Task added." main filterTasksByTag :: IO () filterTasksByTag = do putStr "Enter the tag to filter by (e.g., +chapelShelving): " hFlush stdout tag <- getLine exists <- doesFileExist todoFile if exists then do contents <- TIO.readFile todoFile let tasks = Data.Text.lines contents filteredTasks = filter (isInfixOf (pack tag)) tasks putStrLn "Filtered tasks:" TIO.putStrLn (Data.Text.unlines filteredTasks) else putStrLn "No tasks found." main sortTasksByPriority :: IO () sortTasksByPriority = do exists <- doesFileExist todoFile if exists then do contents <- TIO.readFile todoFile let tasks = Data.Text.lines contents sortedTasks = sortBy comparePriority tasks putStrLn "Sorted tasks by priority:" TIO.putStrLn (Data.Text.unlines sortedTasks) else putStrLn "No tasks found." main comparePriority :: Text -> Text -> Ordering comparePriority t1 t2 = comparing getPriority t1 t2 getPriority :: Text -> Maybe Char getPriority t = case unpack t of ('(':p:')':_) | isAlpha p -> Just p _ -> Nothing