Skip to content

Instantly share code, notes, and snippets.

@aglassman
Created June 21, 2023 03:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aglassman/5668157da8e4d0ee5a65cfa3068d5f8b to your computer and use it in GitHub Desktop.
Save aglassman/5668157da8e4d0ee5a65cfa3068d5f8b to your computer and use it in GitHub Desktop.
defmodule Todo do
import String, only: [split: 3, trim: 1];
@t :todos
@d :dets
def prompt() do IO.write("todo> "); split(trim(IO.binread(:line)), " ", parts: 2) end
def run(:start) do @d.open_file(@t, type: :set); run(">") end
def run(:goodbye), do: @d.close(@t)
def run(_) do
case prompt() do
[c] when c in ["", "ls"] -> @d.match_object(@t, {:"$1", false})
["ls", "done"] -> @d.match_object(@t, {:"$1", true})
["ls", "all"] -> @d.match_object(@t, {:"$1", :_})
["add", item] -> @d.insert(@t, {item, false})
["done", item] -> @d.insert(@t, {item, true})
["rm", item] -> @d.match_delete(@t, {item, :_})
["reset"] -> @d.delete_all_objects(@t)
["exit"] -> :goodbye
c -> {:invalid_command, c}
end
|> IO.inspect(pretty: true) |> run()
end
end
Todo.run(:start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment