Add exceptions
This commit is contained in:
parent
c6745f5b35
commit
75c0b88d29
42
app/Main.hs
42
app/Main.hs
@ -4,7 +4,7 @@ module Main(main, parseTask, serializeTask, getPriority, comparePriority, TaskMa
|
|||||||
|
|
||||||
import System.IO
|
import System.IO
|
||||||
import System.Directory
|
import System.Directory
|
||||||
import Data.Text (Text, pack, unpack, isInfixOf, lines, unlines, strip)
|
import Data.Text (Text, pack, unpack, isInfixOf, lines, unlines, strip, splitOn)
|
||||||
import qualified Data.Text.IO as TIO
|
import qualified Data.Text.IO as TIO
|
||||||
import qualified Data.ByteString as BS
|
import qualified Data.ByteString as BS
|
||||||
import qualified Data.ByteString.Char8 as BSC
|
import qualified Data.ByteString.Char8 as BSC
|
||||||
@ -14,6 +14,7 @@ import Data.Ord (comparing)
|
|||||||
import Data.Char (isAlpha)
|
import Data.Char (isAlpha)
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Function (on)
|
import Data.Function (on)
|
||||||
|
import Control.Exception (catch, IOException)
|
||||||
|
|
||||||
todoFile :: FilePath
|
todoFile :: FilePath
|
||||||
todoFile = "todo.txt"
|
todoFile = "todo.txt"
|
||||||
@ -44,12 +45,17 @@ displayTasks = do
|
|||||||
exists <- doesFileExist todoFile
|
exists <- doesFileExist todoFile
|
||||||
if exists
|
if exists
|
||||||
then do
|
then do
|
||||||
contents <- BSC.readFile todoFile
|
contents <- catch (TIO.readFile todoFile) handleReadError
|
||||||
let taskMap = parseTask (pack (BSC.unpack contents))
|
let taskMap = parseTask contents
|
||||||
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."
|
||||||
main
|
main
|
||||||
|
where
|
||||||
|
handleReadError :: IOException -> IO Text
|
||||||
|
handleReadError e = do
|
||||||
|
putStrLn $ "Error reading file: " ++ show e
|
||||||
|
return ""
|
||||||
|
|
||||||
addTask :: IO ()
|
addTask :: IO ()
|
||||||
addTask = do
|
addTask = do
|
||||||
@ -59,14 +65,22 @@ addTask = do
|
|||||||
exists <- doesFileExist todoFile
|
exists <- doesFileExist todoFile
|
||||||
if exists
|
if exists
|
||||||
then do
|
then do
|
||||||
contents <- BSC.readFile todoFile
|
contents <- catch (TIO.readFile todoFile) handleReadError
|
||||||
let taskMap = parseTask (pack (BSC.unpack contents))
|
let taskMap = parseTask contents
|
||||||
newId = if Map.null taskMap then 1 else fst (Map.findMax taskMap) + 1
|
newId = if Map.null taskMap then 1 else fst (Map.findMax taskMap) + 1
|
||||||
newTaskMap = Map.insert newId (pack task) taskMap
|
newTaskMap = Map.insert newId (pack task) taskMap
|
||||||
BSC.writeFile todoFile (BSC.pack (unpack (serializeTask newTaskMap)))
|
catch (TIO.writeFile todoFile (serializeTask newTaskMap)) handleWriteError
|
||||||
else BSC.writeFile todoFile (BSC.pack (task <> "\n"))
|
else catch (TIO.writeFile todoFile (pack task <> "\n")) handleWriteError
|
||||||
putStrLn "Task added."
|
putStrLn "Task added."
|
||||||
main
|
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
|
||||||
@ -76,26 +90,36 @@ filterTasksByTag = do
|
|||||||
exists <- doesFileExist todoFile
|
exists <- doesFileExist todoFile
|
||||||
if exists
|
if exists
|
||||||
then do
|
then do
|
||||||
contents <- TIO.readFile todoFile
|
contents <- catch (TIO.readFile todoFile) handleReadError
|
||||||
let taskMap = parseTask contents
|
let taskMap = parseTask contents
|
||||||
filteredTasks = Map.filter (isInfixOf (pack tag)) taskMap
|
filteredTasks = Map.filter (isInfixOf (pack tag)) taskMap
|
||||||
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."
|
||||||
main
|
main
|
||||||
|
where
|
||||||
|
handleReadError :: IOException -> IO Text
|
||||||
|
handleReadError e = do
|
||||||
|
putStrLn $ "Error reading file: " ++ show e
|
||||||
|
return ""
|
||||||
|
|
||||||
sortTasksByPriority :: IO ()
|
sortTasksByPriority :: IO ()
|
||||||
sortTasksByPriority = do
|
sortTasksByPriority = do
|
||||||
exists <- doesFileExist todoFile
|
exists <- doesFileExist todoFile
|
||||||
if exists
|
if exists
|
||||||
then do
|
then do
|
||||||
contents <- TIO.readFile todoFile
|
contents <- catch (TIO.readFile todoFile) handleReadError
|
||||||
let taskMap = parseTask contents
|
let taskMap = parseTask contents
|
||||||
sortedTasks = sortBy (comparePriority `on` snd) (Map.toList taskMap)
|
sortedTasks = sortBy (comparePriority `on` snd) (Map.toList taskMap)
|
||||||
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."
|
||||||
main
|
main
|
||||||
|
where
|
||||||
|
handleReadError :: IOException -> IO Text
|
||||||
|
handleReadError e = do
|
||||||
|
putStrLn $ "Error reading file: " ++ show e
|
||||||
|
return ""
|
||||||
|
|
||||||
comparePriority :: Text -> Text -> Ordering
|
comparePriority :: Text -> Text -> Ordering
|
||||||
comparePriority t1 t2 = comparing getPriority t1 t2
|
comparePriority t1 t2 = comparing getPriority t1 t2
|
||||||
|
Loading…
Reference in New Issue
Block a user