summaryrefslogtreecommitdiff
path: root/todo.hs
diff options
context:
space:
mode:
authorJoe Zhao <ztuowen@gmail.com>2014-08-09 10:58:03 +0800
committerJoe Zhao <ztuowen@gmail.com>2014-08-09 10:58:03 +0800
commit5bdaa1e4ffe40add10f000ee993e6b500c419a37 (patch)
tree46a624488ece8d37fa3580422234ed2b4acc6728 /todo.hs
parent01aa73b269f7ad780233be338affdf3c9288b1ed (diff)
downloadhaskbox-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.hs52
1 files changed, 52 insertions, 0 deletions
diff --git a/todo.hs b/todo.hs
new file mode 100644
index 0000000..2cb21c1
--- /dev/null
+++ b/todo.hs
@@ -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)
+