Skip to content

Instantly share code, notes, and snippets.

@finnp
Last active December 22, 2015 00:38
Show Gist options
  • Save finnp/6390631 to your computer and use it in GitHub Desktop.
Save finnp/6390631 to your computer and use it in GitHub Desktop.
Simple terminal task manager
module Main where
import System.Environment( getArgs )
import System.IO
import Control.Monad
import Data.List.Split
main = do
args <- getArgs
mapM execute_command (split_commands args)
execute_command :: (String, [String]) -> IO ()
execute_command ("-add", args) = todo_add_item args
execute_command ("-show", args) = todo_show args
execute_command ("-rm", args) = todo_remove args
execute_command (_, _) = return ()
split_commands :: [String] -> [(String, [String])]
split_commands [] = []
split_commands (x:xs) = (x, takeWhile is_not_command xs):(split_commands $ dropWhile is_not_command xs)
where
is_not_command a = head a /= '-'
todo_add_item :: [String] -> IO ()
todo_add_item [] = return ()
todo_add_item (x:xs) = do
appendFile "todo.txt" (x ++ "\n")
todo_add_item xs
todo_show :: [String] -> IO ()
todo_show _ = do
contents <- readFile "todo.txt"
let output = zipWith (\t n -> (show n) ++ ") " ++ t) (lines contents) [1..]
putStr $ unlines output
return ()
todo_remove :: [String] -> IO ()
todo_remove xs = do
let ns = map (\a -> read a :: Int) xs
contents <- readFile "todo.txt"
length contents `seq` (writeFile "todo.txt" $ remove_all ns contents)
return ()
remove_one :: Int -> String -> String
remove_one n contents = unlines $ take (n-1) (lines contents) ++ (drop n (lines contents))
remove_all :: [Int] -> String -> String
remove_all [] contents = contents
remove_all (n:ns) contents = remove_one n (remove_all ns contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment