blob: 2cb21c1e4d3bc733e6fb4d2e28ddea529afb6892 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import System.Environment
import System.Directory
import System.IO
import Data.List
import Control.Exception
import Control.Monad(when)
fileName = "todo.txt"
dispatch :: String -> String -> IO ()
dispatch file "add" = add file
dispatch file "view" = view file
dispatch file "rm" = remove file
dispatch file _ = do
putStrLn "Operations supported:\nadd - Append Item\nview - Inspect Item\nrm - Remove Item"
view file = do
contents <- readFile file
let todoTasks = lines contents
putStr $ unlines $ number todoTasks
add file = do
todoItem <- getLine
when (not $ null todoItem) (do
appendFile file todoItem)
remove file = do
contents <- readFile file
numberString <- getLine
let todoTasks = lines contents
number = read numberString
newTodoList = unlines $ delete (todoTasks !! number) todoTasks
bracketOnError (openTempFile "." "temp")
(\(tempName,tempHandle) -> do
hClose tempHandle
removeFile tempName)
(\(tempName,tempHandle) -> do
hPutStr tempHandle newTodoList
hClose tempHandle
removeFile file
renameFile tempName file)
number task = zipWith (\n line -> show n ++ " - " ++ line) [0..] task
main = do
line <- getLine
if null line then return ()
else (do
dispatch fileName line
main)
|