-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.hs
349 lines (301 loc) · 12.4 KB
/
t.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import Control.Applicative hiding (many,optional,(<|>))
import Control.Monad (when)
import Control.Monad.Error (throwError)
import Control.Exception (tryJust)
import Data.List (intercalate)
import Data.Time
import System.Directory (copyFile)
import System.Environment (getEnv, getArgs)
import System.IO.Error (isDoesNotExistError)
import System.Random (getStdGen, randoms)
import Text.Parsec hiding (spaces,newline)
import Text.Parsec.String (Parser, parseFromFile)
data TodoAtom = Description String |
Date Day |
Tags [String] |
Repeat Integer |
Active Bool |
Mark Bool
deriving Show
data Todo = Todo {
todoDescription :: String,
todoDate :: Day,
todoTags :: [String],
todoRepeat :: Integer,
todoActive :: Bool,
todoMark :: Bool
} deriving Eq
type TodoList = [Todo]
type Tag = String
type TodoError = Either String
parseDescription :: Parser TodoAtom
parseDescription = Description <$> manyTill anyChar (try (string " | "))
parseDate :: Parser TodoAtom
parseDate = Date <$> stringToDay <$> (string "date=" *> many1 (digit <|> char '-'))
stringToDay :: String -> Day
stringToDay str = fromGregorian (read y) (read m) (read d)
where (y:m:d:[]) = split '-' str
split :: Char -> String -> [String]
split delim = foldr split' [[]]
where split' x acc = if x == delim then []:acc else (x : head acc) : tail acc
parseTags :: Parser TodoAtom
parseTags = Tags <$> (string "tag=" *> sepBy (many1 letter) (char ','))
parseRepeat :: Parser TodoAtom
parseRepeat = Repeat <$> read <$> (string "repeat=" *> many1 digit)
parseActive :: Parser TodoAtom
parseActive = Active <$> (== '1') <$> (string "active=" *> digit)
parseMark :: Parser TodoAtom
parseMark = Mark <$> (== '1') <$> (string "mark=" *> digit)
parseTodo :: Parser Todo
parseTodo = atomListToTodo <$> ((:) <$> (parseDescription <* spaces) <*> (sepBy (choice [parseDate,parseTags,parseRepeat,parseActive,parseMark]) spaces <* (newline <|> eof)))
atomListToTodo :: [TodoAtom] -> Todo
atomListToTodo xs = Todo {
todoDescription = head desc,
todoDate = head date,
todoTags = head tags,
todoRepeat = if null repeat then 0 else head repeat,
todoActive = if null active then False else head active,
todoMark = if null mark then False else head mark
}
where desc = [x | Description x <- xs]
date = [x | Date x <- xs]
tags = [x | Tags x <- xs]
repeat = [x | Repeat x <- xs]
active = [x | Active x <- xs]
mark = [x | Mark x <- xs]
parseLines :: Parser TodoList
parseLines = many1 parseTodo
spaces :: Parser ()
spaces = () <$ many (char ' ')
newline :: Parser ()
newline = () <$ char '\n'
readTodoFile :: IO (TodoError TodoList)
readTodoFile = do
gen <- getStdGen
let randomStr = map (['A'..'Z'] !!) $ map (`mod` 26) $ take 5 (randoms gen :: [Int])
let tmpFile = "/tmp/todo-backup." ++ randomStr
todoFile' <- todoFile
copyFile todoFile' tmpFile
p <- parseFromFile parseLines tmpFile
case p of
Left x -> return $ throwError $ show x
Right x -> return $ return $ x
todoColor :: Day -> Todo -> String
todoColor today todo =
case () of _
| daysSince <= 7 -> blue
| daysSince <= 21 -> green
| otherwise -> red
where daysSince = diffDays today $ todoDate todo
underlinedRed = "\27[4;1;31m"
red = "\27[0;1;31m"
--orange = "\27[0;33m"
--yellow = "\27[0;1;33m"
green = "\27[0;1;32m"
blue = "\27[0;1;36m"
normalColor = "\27[0m"
-- printList, fileUpdateBool, printAllBool
data OutInfo = OutInfo {
outList :: [Bool],
outFile :: Bool,
outAll :: Bool
}
insert :: Todo -> TodoList -> TodoList
insert newTodo = fst . foldr f ([],False)
where f oldTodo (newList, inserted) = if inserted == False && (todoDate newTodo >= todoDate oldTodo)
then (oldTodo:newTodo:newList, True)
else (oldTodo:newList, inserted)
--TODO: maybe use a Writer monad or similar to store the stdout output
--TODO: insert sorted?
--TODO: use that compiler flag that allows alternate syntax to _ _ _
add :: [String] -> Day -> Tag -> TodoList -> TodoError (TodoList, OutInfo)
add [] _ _ _ = throwError "missing todo"
add args today tag todos = return (newTodoList, outInfo)
where newTodo = Todo {
todoDescription = intercalate " " args,
todoDate = today,
todoTags = [tag],
todoRepeat = 0,
todoActive = False,
todoMark = False
}
newTodoList = insert newTodo todos
outInfo = OutInfo {
outList = map (== newTodo) newTodoList,
outFile = True,
outAll = False
}
rm :: [String] -> Day -> Tag -> TodoList -> TodoError (TodoList, OutInfo)
rm [] _ _ _ = throwError "missing line #"
rm (_:_:_) _ _ _ = throwError "too many args"
rm args today tag todos
| arg >= length todos = throwError "bad line #"
| otherwise = return (newTodoList, outInfo)
where arg = read (head args) - 1
(xs,todo:ys) = splitAt arg todos
newTodoList = xs ++ ys ++ if todoRepeat todo == 0 then [] else [newTodo]
newTodo = todo { todoDate = newDay, todoMark = False }
newDay = addDays (todoRepeat todo) today
outInfo = OutInfo {
outList = mapRelevant today tag newTodoList,
outFile = True,
outAll = False
}
touch :: [String] -> Day -> Tag -> TodoList -> TodoError (TodoList, OutInfo)
touch [] _ _ _ = throwError "missing line #"
touch (_:_:_) _ _ _ = throwError "too many args"
touch args today tag todos
| arg >= length todos = throwError "bad line #"
| otherwise = return (newTodoList, outInfo)
where arg = read (head args) - 1
(xs,todo:ys) = splitAt arg todos
newTodo = todo { todoDate = today, todoActive = True, todoMark = False }
newTodoList = insert newTodo $ xs ++ ys
outInfo = OutInfo {
outList = mapRelevant today tag newTodoList,
outFile = True,
outAll = False
}
mv :: [String] -> TodoList -> TodoError (TodoList, OutInfo)
mv [] _ = throwError "missing line #"
mv [_] _ = throwError "missing description"
mv args todos
| index >= length todos = throwError "bad line #"
| otherwise = return (newTodoList, outInfo)
where index = read (head args) - 1
(xs,todo:ys) = splitAt index todos
newTodoList = xs ++ newTodo:ys
newTodo = todo { todoDescription = intercalate " " $ tail args }
outInfo = OutInfo {
outList = map (== newTodo) newTodoList,
outFile = True,
outAll = False
}
changeTags :: [String] -> TodoList -> TodoError (TodoList, OutInfo)
changeTags [] _ = throwError "missing line #"
changeTags [_] _ = throwError "missing tags"
changeTags (_:_:_:_) _ = throwError "too many args"
changeTags args todos
| index >= length todos = throwError "bad line #"
| otherwise = return (newTodoList, outInfo)
where index = read (head args) - 1
(xs,todo:ys) = splitAt index todos
newTags = split ',' $ last args
newTodo = todo { todoTags = newTags }
newTodoList = xs ++ newTodo:ys
outInfo = OutInfo {
outList = map (== newTodo) newTodoList,
outFile = True,
outAll = True
}
changeRepeat :: [String] -> TodoList -> TodoError (TodoList, OutInfo)
changeRepeat [] _ = throwError "missing line #"
changeRepeat [_] _ = throwError "missing repeat value"
changeRepeat (_:_:_:_) _ = throwError "too many args"
changeRepeat args todos
| index >= length todos = throwError "bad line #"
| otherwise = return (newTodoList, outInfo)
where index = read (head args) - 1
(xs,todo:ys) = splitAt index todos
newRepeat = read $ last args
newTodo = todo { todoRepeat = newRepeat }
newTodoList = xs ++ newTodo:ys
outInfo = OutInfo {
outList = map (== newTodo) newTodoList,
outFile = True,
outAll = True
}
--TODO: DRY it up, lots of repeating in these functions
mark :: [String] -> Tag -> TodoList -> TodoError (TodoList, OutInfo)
mark args tag todos
| null args = return (todos, outInfo)
| any (>= length todos) indexes = throwError "bad line #"
| otherwise = return (newTodoList, outInfo)
where indexes = map (pred . read) args
newTodoList = zipWith f [0..] todos
f index todo = if index `elem` indexes
then todo { todoMark = not $ todoMark todo }
else todo
outInfo = OutInfo {
outList = map (\x -> todoMark x && tag `elem` todoTags x) newTodoList,
outFile = not $ null args,
outAll = False
}
listRelevant :: Day -> Tag -> TodoList -> TodoError (TodoList, OutInfo)
listRelevant today tag todos = return (todos, outInfo)
where outInfo = OutInfo {
outList = mapRelevant today tag todos,
outFile = False,
outAll = False
}
listAll :: [String] -> TodoList -> TodoError (TodoList, OutInfo)
listAll args todos
| length args > 1 = throwError "too many args"
| index > length todos = throwError "bad line #"
| otherwise = return (todos, outInfo)
where index = if null args then 0 else read (head args) - 1
(_,t:_) = splitAt index todos
outInfo = OutInfo {
outList = if null args then map (const True) todos else map (== t) todos,
outFile = False,
outAll = True
}
--http://www.scs.stanford.edu/11au-cs240h/notes/zipper-slides.html#(23)
--TODO: what is a zipper and would it be useful. and alos look at common monads like reader, writer, state, lenses, etc
mapRelevant :: Day -> Tag -> TodoList -> [Bool]
mapRelevant today tag todos = map isRelevant todos
where isRelevant x = todoDate x <= today && tag `elem` (todoTags x)
--todoFile = "/Users/joshpoimboeuf/Dropbox/todo.txt"
todoFile :: IO String
todoFile = do
homeDir <- getEnv "HOME"
return $ homeDir ++ "/Dropbox/todo.txt"
todoToOutputStr :: Int -> Day -> Bool -> Todo -> String
todoToOutputStr index today doAll t =
(if todoRepeat t > 0 then underlinedRed else todoColor') ++
(if todoMark t then "*" else " ") ++
todoColor' ++ " " ++
(show index) ++ " " ++
(if doAll then todoToFileStr t else todoDescription t) ++
normalColor
where todoColor' = todoColor today t
todoToFileStr :: Todo -> String
todoToFileStr t =
(todoDescription t) ++
" | date=" ++ (showGregorian $ todoDate t) ++
" tag=" ++ (intercalate "," $ todoTags t) ++
(if todoRepeat t > 0 then " repeat=" ++ show (todoRepeat t) else "") ++
(if todoActive t then " active=1" else "") ++
(if todoMark t then " mark=1" else "")
outputTodos :: Day -> (TodoList, OutInfo) -> IO ()
outputTodos today (todos, outInfo) = do
todoFile' <- todoFile
putStr $ unlines $ map todoToOutputStr' $ filter snd3 $ zip3 [1..] (outList outInfo) todos
when (outFile outInfo) (writeFile todoFile' $ unlines $ map todoToFileStr todos)
where
todoToOutputStr' (index, _, todo) = todoToOutputStr index today (outAll outInfo) todo
snd3 (_, x, _) = x
processArgs :: [String] -> Day -> Tag -> TodoList -> TodoError (TodoList, OutInfo)
processArgs args today tag =
case args of
[] -> listRelevant today tag
("all":argsTail) -> listAll argsTail
("add":argsTail) -> add argsTail today tag
("rm":argsTail) -> rm argsTail today tag
("touch":argsTail) -> touch argsTail today tag
("mv":argsTail) -> mv argsTail
("tag":argsTail) -> changeTags argsTail
("repeat":argsTail) -> changeRepeat argsTail
("mark":argsTail) -> mark argsTail tag
_ -> const $ throwError "bad command"
main = do
now <- getZonedTime
let today = localDay $ zonedTimeToLocalTime $ now
tag' <- tryJust (return . isDoesNotExistError) (getEnv "TAG")
let tag = either (const "work") id tag'
oldTodos <- readTodoFile
args <- getArgs
let newTodos = oldTodos >>= processArgs args today tag
case newTodos of
Left x -> putStrLn x
Right x -> outputTodos today x