ucl/cmd/cmsh/main.go

55 lines
1.1 KiB
Go
Raw Normal View History

2024-04-10 10:45:58 +00:00
package main
import (
"context"
2024-12-11 10:16:08 +00:00
"fmt"
2024-04-10 10:45:58 +00:00
"github.com/chzyer/readline"
"log"
2024-12-11 09:47:05 +00:00
"ucl.lmika.dev/repl"
2024-04-27 01:00:48 +00:00
"ucl.lmika.dev/ucl"
2024-05-04 00:59:17 +00:00
"ucl.lmika.dev/ucl/builtins"
2024-04-10 10:45:58 +00:00
)
func main() {
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
2024-12-11 09:47:05 +00:00
instRepl := repl.New(
2024-05-04 00:59:17 +00:00
ucl.WithModule(builtins.OS()),
ucl.WithModule(builtins.FS(nil)),
2025-01-15 11:22:17 +00:00
ucl.WithModule(builtins.Log(nil)),
ucl.WithModule(builtins.Strs()),
2024-05-04 00:59:17 +00:00
)
2024-04-10 10:45:58 +00:00
ctx := context.Background()
2024-12-11 10:16:08 +00:00
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.
`,
})
2024-12-11 11:30:14 +00:00
instRepl.SetCommand("new-fancy", newFancy, newFancyDoc)
instRepl.SetCommand("many-fancies", manyFancies)
repl.AddTypePrinter(instRepl, displayFancy)
repl.AddTypePrinter(instRepl, displayFancies)
2024-12-11 10:16:08 +00:00
2024-04-10 10:45:58 +00:00
for {
line, err := rl.Readline()
if err != nil { // io.EOF
break
}
2024-12-11 10:16:08 +00:00
if err := instRepl.EvalAndDisplay(ctx, line); err != nil {
2024-04-10 10:45:58 +00:00
log.Printf("%T: %v", err, err)
}
}
}