Compare commits

..

3 Commits

Author SHA1 Message Date
df16a5bded refactor: gleamify :) 2026-03-16 17:19:46 +01:00
f64af89e9b test: adapt to list.find usage 2026-03-16 17:07:51 +01:00
0e9c1dbb01 feat: use list.find() instead of building a dict 2026-03-16 17:07:37 +01:00
2 changed files with 8 additions and 18 deletions

View File

@@ -1,4 +1,3 @@
import gleam/dict
import gleam/io
import gleam/list
import gleam/string
@@ -32,7 +31,8 @@ fn history(_cmds: List(Command)) -> Nil {
fn help(cmds: List(Command)) -> Nil {
io.println("Available commands:")
list.each(cmds, fn(cmd) {
cmds
|> list.each(fn(cmd) {
io.println(" " <> cmd.name <> " - " <> cmd.description)
})
}
@@ -50,14 +50,6 @@ pub fn build_commands() -> List(Command) {
]
}
pub 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 {
case simplifile.append(history_file, cmd <> "\n") {
Ok(Nil) -> Nil
@@ -73,8 +65,8 @@ fn repl_loop(cmds: List(Command)) -> a {
"" -> Nil
_ -> {
input |> append_history
case dict.get(commands_dict(cmds), cmd) {
Ok(func) -> func(cmds)
case list.find(cmds, fn(c) { c.name == cmd }) {
Ok(command) -> command.run(cmds)
Error(_) -> io.println(input <> ": unknown command")
}
}

View File

@@ -1,19 +1,17 @@
import gleam/dict
import gleam/list
import gleeunit
import gleeunit/should
import simple_repl
pub fn command_lookup_test() {
let commands =
simple_repl.build_commands()
|> simple_repl.commands_dict
let commands = simple_repl.build_commands()
let has_help = case dict.get(commands, "help") {
let has_help = case list.find(commands, fn(c) { c.name == "help" }) {
Ok(_) -> True
Error(_) -> False
}
let has_unknown = case dict.get(commands, "foo") {
let has_unknown = case list.find(commands, fn(c) { c.name == "foo" }) {
Ok(_) -> True
Error(_) -> False
}