refactor: introduce Command type

This commit is contained in:
2026-03-09 19:31:12 +01:00
parent 2489c30b5a
commit 216c10273b

View File

@@ -1,5 +1,6 @@
import gleam/dict import gleam/dict
import gleam/io import gleam/io
import gleam/list
import gleam/string import gleam/string
import simplifile import simplifile
@@ -11,38 +12,50 @@ fn get_line(prompt: String) -> String
@external(erlang, "erlang", "halt") @external(erlang, "erlang", "halt")
fn halt(code: Int) -> Nil fn halt(code: Int) -> Nil
fn clean() { type Command {
Command(name: String, description: String, run: fn(List(Command)) -> Nil)
}
fn clean(_cmds: List(Command)) -> Nil {
case simplifile.delete(history_file) { case simplifile.delete(history_file) {
Ok(Nil) -> Nil Ok(Nil) -> Nil
Error(e) -> io.println(simplifile.describe_error(e)) Error(e) -> io.println(simplifile.describe_error(e))
} }
} }
fn history() { fn history(_cmds: List(Command)) -> Nil {
case simplifile.read(history_file) { case simplifile.read(history_file) {
Ok(content) -> io.print(content) Ok(content) -> io.print(content)
Error(e) -> io.println(simplifile.describe_error(e)) Error(e) -> io.println(simplifile.describe_error(e))
} }
} }
fn help() -> Nil { fn help(cmds: List(Command)) -> Nil {
io.println("help: print this message") io.println("Available commands:")
io.println("history: dump history") list.each(cmds, fn(cmd) {
io.println("clean: delete history") io.println(" " <> cmd.name <> " - " <> cmd.description)
io.println("exit: quit the program") })
} }
fn exit() -> Nil { fn exit(_cmds: List(Command)) -> Nil {
halt(0) halt(0)
} }
fn commands() -> dict.Dict(String, fn() -> Nil) { fn build_commands() -> List(Command) {
dict.from_list([ [
#("clean", clean), Command("clean", "delete history", clean),
#("help", help), Command("help", "show this help", help),
#("history", history), Command("history", "show history", history),
#("exit", exit), Command("exit", "quit the program", exit),
]) ]
}
fn commands_dict(
cmds: List(Command),
) -> dict.Dict(String, fn(List(Command)) -> Nil) {
list.fold(cmds, dict.new(), fn(acc, cmd) {
dict.insert(acc, cmd.name, cmd.run)
})
} }
fn append_history(cmd: String) -> Nil { fn append_history(cmd: String) -> Nil {
@@ -52,7 +65,7 @@ fn append_history(cmd: String) -> Nil {
} }
} }
fn repl_loop(cmds: dict.Dict(String, fn() -> Nil)) -> a { fn repl_loop(cmds: List(Command)) -> a {
let input = get_line("repl> ") |> string.trim let input = get_line("repl> ") |> string.trim
let cmd = string.lowercase(input) let cmd = string.lowercase(input)
@@ -60,8 +73,8 @@ fn repl_loop(cmds: dict.Dict(String, fn() -> Nil)) -> a {
"" -> Nil "" -> Nil
_ -> { _ -> {
input |> append_history input |> append_history
case dict.get(cmds, cmd) { case dict.get(commands_dict(cmds), cmd) {
Ok(func) -> func() Ok(func) -> func(cmds)
Error(_) -> io.println(input <> ": unknown command") Error(_) -> io.println(input <> ": unknown command")
} }
} }
@@ -71,7 +84,7 @@ fn repl_loop(cmds: dict.Dict(String, fn() -> Nil)) -> a {
} }
pub fn main() -> Nil { pub fn main() -> Nil {
let cmds = commands() let cmds = build_commands()
case simplifile.is_file(history_file) { case simplifile.is_file(history_file) {
Ok(True) -> repl_loop(cmds) Ok(True) -> repl_loop(cmds)
_ -> _ ->