Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (47 sloc) 1.59 KB
import System.Environment
import System.Directory
import System.IO
import Data.List
import Control.Exception
dispatch::String -> [String] -> IO ()
dispatch "add" = add
dispatch "view" = view
dispatch "remove" = remove
dispatch command = doesntExist command
add::[String] -> IO ()
add [fileName, todoItem] = appendFile fileName (todoItem ++ "\n")
add _ = putStrLn "The add command takes exactly two arguments"
nbTasks::String -> IO [String]
nbTasks fileName = do
contents <- readFile fileName
let todoTasks = lines contents
numberedTasks = zipWith (\n line -> show n ++ " - " ++ line) [0..] todoTasks
return numberedTasks
view::[String] -> IO ()
view [fileName] = do
tasks <- nbTasks fileName
mapM_ putStrLn tasks
remove::[String] -> IO ()
remove [fileName, numberS] = do
putStrLn "There are your TO-DO items:"
todoTasks <- nbTasks fileName
mapM_ putStrLn todoTasks
let number = read numberS
newTodoItems = unlines $ delete (todoTasks !! number) todoTasks
bracketOnError (openTempFile "." "temp")
(\(tempName, tempHandle) -> do
hClose tempHandle
removeFile tempName)
(\(tempName, tempHandle) -> do
hPutStr tempHandle newTodoItems
hClose tempHandle
removeFile "todo.txt"
renameFile tempName "todo.txt")
doesntExist :: String -> [String] -> IO ()
doesntExist command _ =
putStrLn $ "The " ++ command ++ " command doesn't exist"
main = do
args <- getArgs
case args of
[] -> putStrLn "Please specifier command."
command:argList -> dispatch command argList
You can’t perform that action at this time.