ucl/cmd/cmsh/main.go
Leon Mika d937ee4b54
Some checks failed
Build / build (push) Failing after 1m43s
Added a log builtin
2025-01-15 22:22:17 +11:00

55 lines
1.1 KiB
Go

package main
import (
"context"
"fmt"
"github.com/chzyer/readline"
"log"
"ucl.lmika.dev/repl"
"ucl.lmika.dev/ucl"
"ucl.lmika.dev/ucl/builtins"
)
func main() {
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
instRepl := repl.New(
ucl.WithModule(builtins.OS()),
ucl.WithModule(builtins.FS(nil)),
ucl.WithModule(builtins.Log(nil)),
ucl.WithModule(builtins.Strs()),
)
ctx := context.Background()
instRepl.SetCommand("hello", func(ctx context.Context, args ucl.CallArgs) (any, error) {
fmt.Println("hello")
return nil, nil
}, repl.Doc{
Brief: "displays hello",
Detailed: `
This displays the message 'hello' to the terminal.
It then terminates.
`,
})
instRepl.SetCommand("new-fancy", newFancy, newFancyDoc)
instRepl.SetCommand("many-fancies", manyFancies)
repl.AddTypePrinter(instRepl, displayFancy)
repl.AddTypePrinter(instRepl, displayFancies)
for {
line, err := rl.Readline()
if err != nil { // io.EOF
break
}
if err := instRepl.EvalAndDisplay(ctx, line); err != nil {
log.Printf("%T: %v", err, err)
}
}
}