ucl/cmd/cmsh/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

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