Skip to content

Instantly share code, notes, and snippets.

@Detegr
Created March 23, 2012 15:34
Show Gist options
  • Save Detegr/2171849 to your computer and use it in GitHub Desktop.
Save Detegr/2171849 to your computer and use it in GitHub Desktop.
Small todo-program in haskell.
module Todo where
import Data.List
import System.Environment
import System.IO
import System.Directory
configPath :: String
configPath = "todotest.txt"
getTasks :: IO(String)
getTasks = do
str <- readFile configPath
if (length str) > 0 then
return $ "Todo: *" ++ (intercalate " *" $ lines str)
else return "Nothing to do :)"
newLine :: String -> String
newLine str = str ++ "\n"
remove :: [(Int,String)] -> [String] -> String -> Handle -> IO()
remove [] _ tmp _ = do
putStrLn "Invalid task number."
removeFile tmp
remove argnum c tmp tmph
| num > length c || num <= 0 = do
putStrLn $ "Task " ++ show num ++ " does not exist!"
removeFile tmp
| otherwise = do
mapM_ (\s -> hPutStr tmph $ newLine s) $ take (num-1) c ++ drop num c
removeFile configPath
renameFile tmp configPath
where num = fst $ head argnum
handleArg :: String -> IO()
handleArg arg
| head arg == '-' = do
contents <- (readFile configPath)
(tmp, tmph) <- openTempFile "." "temp"
remove (reads $ drop 1 arg :: [(Int,String)]) (lines contents) tmp tmph
hClose tmph
| otherwise = appendFile configPath $ newLine arg
main = do
args <- getArgs
if length args == 0 then do
tasks <- getTasks
putStrLn tasks
else if length args > 1
then putStrLn "Too many arguments."
else handleArg $ head args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment