Created
June 21, 2023 03:47
-
-
Save aglassman/5668157da8e4d0ee5a65cfa3068d5f8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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