Compare commits
5 Commits
216c10273b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| df16a5bded | |||
| f64af89e9b | |||
| 0e9c1dbb01 | |||
| 518373c2bb | |||
| 1d66f31e19 |
@@ -1,4 +1,3 @@
|
|||||||
import gleam/dict
|
|
||||||
import gleam/io
|
import gleam/io
|
||||||
import gleam/list
|
import gleam/list
|
||||||
import gleam/string
|
import gleam/string
|
||||||
@@ -12,7 +11,7 @@ fn get_line(prompt: String) -> String
|
|||||||
@external(erlang, "erlang", "halt")
|
@external(erlang, "erlang", "halt")
|
||||||
fn halt(code: Int) -> Nil
|
fn halt(code: Int) -> Nil
|
||||||
|
|
||||||
type Command {
|
pub type Command {
|
||||||
Command(name: String, description: String, run: fn(List(Command)) -> Nil)
|
Command(name: String, description: String, run: fn(List(Command)) -> Nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +31,8 @@ fn history(_cmds: List(Command)) -> Nil {
|
|||||||
|
|
||||||
fn help(cmds: List(Command)) -> Nil {
|
fn help(cmds: List(Command)) -> Nil {
|
||||||
io.println("Available commands:")
|
io.println("Available commands:")
|
||||||
list.each(cmds, fn(cmd) {
|
cmds
|
||||||
|
|> list.each(fn(cmd) {
|
||||||
io.println(" " <> cmd.name <> " - " <> cmd.description)
|
io.println(" " <> cmd.name <> " - " <> cmd.description)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@ fn exit(_cmds: List(Command)) -> Nil {
|
|||||||
halt(0)
|
halt(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_commands() -> List(Command) {
|
pub fn build_commands() -> List(Command) {
|
||||||
[
|
[
|
||||||
Command("clean", "delete history", clean),
|
Command("clean", "delete history", clean),
|
||||||
Command("help", "show this help", help),
|
Command("help", "show this help", help),
|
||||||
@@ -50,14 +50,6 @@ fn build_commands() -> List(Command) {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
case simplifile.append(history_file, cmd <> "\n") {
|
case simplifile.append(history_file, cmd <> "\n") {
|
||||||
Ok(Nil) -> Nil
|
Ok(Nil) -> Nil
|
||||||
@@ -73,8 +65,8 @@ fn repl_loop(cmds: List(Command)) -> a {
|
|||||||
"" -> Nil
|
"" -> Nil
|
||||||
_ -> {
|
_ -> {
|
||||||
input |> append_history
|
input |> append_history
|
||||||
case dict.get(commands_dict(cmds), cmd) {
|
case list.find(cmds, fn(c) { c.name == cmd }) {
|
||||||
Ok(func) -> func(cmds)
|
Ok(command) -> command.run(cmds)
|
||||||
Error(_) -> io.println(input <> ": unknown command")
|
Error(_) -> io.println(input <> ": unknown command")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,24 @@
|
|||||||
|
import gleam/list
|
||||||
import gleeunit
|
import gleeunit
|
||||||
|
import gleeunit/should
|
||||||
|
import simple_repl
|
||||||
|
|
||||||
|
pub fn command_lookup_test() {
|
||||||
|
let commands = simple_repl.build_commands()
|
||||||
|
|
||||||
|
let has_help = case list.find(commands, fn(c) { c.name == "help" }) {
|
||||||
|
Ok(_) -> True
|
||||||
|
Error(_) -> False
|
||||||
|
}
|
||||||
|
|
||||||
|
let has_unknown = case list.find(commands, fn(c) { c.name == "foo" }) {
|
||||||
|
Ok(_) -> True
|
||||||
|
Error(_) -> False
|
||||||
|
}
|
||||||
|
|
||||||
|
has_help |> should.equal(True)
|
||||||
|
has_unknown |> should.equal(False)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
gleeunit.main()
|
gleeunit.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user