Created
April 28, 2013 20:13
-
-
Save lucassmagal/5478244 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Todotxt where | |
import Data.List | |
import Control.Monad | |
import System.IO | |
import System.IO.Unsafe | |
import System.Environment | |
import System.Directory | |
main = do | |
args <- getArgs | |
let command = args !! 0 | |
additional_data = drop 1 args | |
case args !! 0 of | |
"add" -> addTask additional_data | |
"delete"-> deleteTask | |
_ -> putStrLn "Unrecognized command. Please, try again." | |
addTask :: [String] -> IO () | |
addTask task = | |
let text = intercalate " " task ++ "\n" | |
in appendFile todoTxt text | |
deleteTask = do | |
let tasks = getTasks | |
numberedTasks = zipWith (\n task -> show n ++ " -- " ++ task) [1..] tasks | |
(tempName, tempFile) <- openTempFile "." "temp" | |
putStrLn ">>> These items are on your todo list:" | |
putStrLn $ unlines numberedTasks | |
putStr "What task you want to delete (0 to exit)? " | |
numberStr <- getLine | |
let number = read numberStr | |
newTasks = delete (tasks !! (number - 1)) tasks | |
hPutStr tempFile $ unlines newTasks | |
hClose tempFile | |
removeFile todoTxt | |
renameFile tempName todoTxt | |
putStrLn "Done." | |
getTasks = toList $ readFile todoTxt where | |
toList = unsafePerformIO . liftM lines | |
-- the canonical location of the todo.txt file: ~/todo.txt | |
todoTxt = home ++ "/todo.txt" where | |
home = unsafePerformIO getHomeDirectory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment