diff options
author | Joe Zhao <ztuowen@gmail.com> | 2014-08-09 10:58:03 +0800 |
---|---|---|
committer | Joe Zhao <ztuowen@gmail.com> | 2014-08-09 10:58:03 +0800 |
commit | 5bdaa1e4ffe40add10f000ee993e6b500c419a37 (patch) | |
tree | 46a624488ece8d37fa3580422234ed2b4acc6728 /todo.hs | |
parent | 01aa73b269f7ad780233be338affdf3c9288b1ed (diff) | |
download | haskbox-old-5bdaa1e4ffe40add10f000ee993e6b500c419a37.tar.gz haskbox-old-5bdaa1e4ffe40add10f000ee993e6b500c419a37.tar.bz2 haskbox-old-5bdaa1e4ffe40add10f000ee993e6b500c419a37.zip |
add files from home for previous chapters and sandboxes
Diffstat (limited to 'todo.hs')
-rw-r--r-- | todo.hs | 52 |
1 files changed, 52 insertions, 0 deletions
@@ -0,0 +1,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) + |