Skip to content

Instantly share code, notes, and snippets.

@trescenzi
Last active April 28, 2022 19:07
Show Gist options
  • Save trescenzi/8b98c81a46653a8a407ed654aadce78a to your computer and use it in GitHub Desktop.
Save trescenzi/8b98c81a46653a8a407ed654aadce78a to your computer and use it in GitHub Desktop.
(fn ripairs [list]
(var index (length list))
(fn []
(match index
0 nil
_ (do
(let [ret (. list index)]
(set index (- index 1))
(values index ret))))))
(fn reduce [list start reducer]
(var acc start)
(each [_ x (ipairs list)]
(set acc (reducer acc x)))
acc)
(fn mapFile [filename descriptor mapFn]
(match (io.open filename descriptor)
f (do
(mapFn f)
(f:close))
(nil err-msg) (print "Could not open file:" err-msg)))
(fn file_to_table [filename descriptor mapFn]
(let [tasks {}]
(mapFile filename :r
(fn [f]
(each [line (f:lines)]
(table.insert tasks line))))
tasks))
(fn trim [str]
(str:gsub "^%s*(.-)%s*$" "%1"))
(let [[command] arg
filename (.. (os.getenv :HOME) :/bulb.do)]
(match command
:push (do
(table.remove arg 1)
(let [task (trim (reduce arg ""
(fn [acc x]
(.. acc " " x))))]
(print :Pushing task)
(mapFile filename :a
(fn [f]
(f:write (.. task "\n"))))))
:pop (let [tasks {}]
(mapFile filename :r
(fn [f]
(each [line (f:lines)]
(table.insert tasks line))))
(print "Done:" (table.remove tasks))
(if (= 0 (length tasks))
(print "You're all done!")
(do
(print "Onto:" (. tasks (length tasks)))
(mapFile filename :w
(fn [f]
(each [_ task (ipairs tasks)]
(f:write (.. task "\n"))))))))
:peek (let [tasks (file_to_table filename :r)]
(let [(_ task) ((ripairs tasks))]
(print task)))
:print (let [tasks (file_to_table filename :r)]
(each [i task (ripairs tasks)]
(if (= i (- (length tasks) 1))
(print "> " task)
(print " " task))))
:clear (mapFile filename :w (fn [f]
(f:write "")))
_ (print "push [task] pushes the task onto the task stack
pop removes the top task and displays the next one
print lists tasks in order
peek displays your current task
clear removes all tasks")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment