Updated UCL and added an interactive mode
This commit is contained in:
parent
29d425c77e
commit
94b58e2168
2
go.mod
2
go.mod
|
@ -117,5 +117,5 @@ require (
|
||||||
golang.org/x/text v0.9.0 // indirect
|
golang.org/x/text v0.9.0 // indirect
|
||||||
google.golang.org/appengine v1.6.7 // indirect
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
ucl.lmika.dev v0.0.0-20240501110514-25594c80d273 // indirect
|
ucl.lmika.dev v0.0.0-20240504013531-0dc9fd3c3281 // indirect
|
||||||
)
|
)
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -5,6 +5,8 @@ github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwS
|
||||||
github.com/Sereal/Sereal v0.0.0-20220220040404-e0d1e550e879 h1:M5ptEKnqKqpFTKbe+p5zEf3ro1deJ6opUz5j3g3/ErQ=
|
github.com/Sereal/Sereal v0.0.0-20220220040404-e0d1e550e879 h1:M5ptEKnqKqpFTKbe+p5zEf3ro1deJ6opUz5j3g3/ErQ=
|
||||||
github.com/Sereal/Sereal v0.0.0-20220220040404-e0d1e550e879/go.mod h1:D0JMgToj/WdxCgd30Kc1UcA9E+WdZoJqeVOuYW7iTBM=
|
github.com/Sereal/Sereal v0.0.0-20220220040404-e0d1e550e879/go.mod h1:D0JMgToj/WdxCgd30Kc1UcA9E+WdZoJqeVOuYW7iTBM=
|
||||||
github.com/alecthomas/assert/v2 v2.0.3 h1:WKqJODfOiQG0nEJKFKzDIG3E29CN2/4zR9XGJzKIkbg=
|
github.com/alecthomas/assert/v2 v2.0.3 h1:WKqJODfOiQG0nEJKFKzDIG3E29CN2/4zR9XGJzKIkbg=
|
||||||
|
github.com/alecthomas/participle v0.7.1 h1:2bN7reTw//5f0cugJcTOnY/NYZcWQOaajW+BwZB5xWs=
|
||||||
|
github.com/alecthomas/participle v0.7.1/go.mod h1:HfdmEuwvr12HXQN44HPWXR0lHmVolVYe4dyL6lQ3duY=
|
||||||
github.com/alecthomas/participle/v2 v2.0.0-beta.5 h1:y6dsSYVb1G5eK6mgmy+BgI3Mw35a3WghArZ/Hbebrjo=
|
github.com/alecthomas/participle/v2 v2.0.0-beta.5 h1:y6dsSYVb1G5eK6mgmy+BgI3Mw35a3WghArZ/Hbebrjo=
|
||||||
github.com/alecthomas/participle/v2 v2.0.0-beta.5/go.mod h1:RC764t6n4L8D8ITAJv0qdokritYSNR3wV5cVwmIEaMM=
|
github.com/alecthomas/participle/v2 v2.0.0-beta.5/go.mod h1:RC764t6n4L8D8ITAJv0qdokritYSNR3wV5cVwmIEaMM=
|
||||||
github.com/alecthomas/participle/v2 v2.1.1 h1:hrjKESvSqGHzRb4yW1ciisFJ4p3MGYih6icjJvbsmV8=
|
github.com/alecthomas/participle/v2 v2.1.1 h1:hrjKESvSqGHzRb4yW1ciisFJ4p3MGYih6icjJvbsmV8=
|
||||||
|
@ -430,3 +432,7 @@ ucl.lmika.dev v0.0.0-20240427010304-6315afc54287 h1:llPHrjca54duvQx9PgMTFDhOW2VQ
|
||||||
ucl.lmika.dev v0.0.0-20240427010304-6315afc54287/go.mod h1:T6V4jIUxlWvMTgn4J752VDHNA8iyVrEX6v98EvDj8G4=
|
ucl.lmika.dev v0.0.0-20240427010304-6315afc54287/go.mod h1:T6V4jIUxlWvMTgn4J752VDHNA8iyVrEX6v98EvDj8G4=
|
||||||
ucl.lmika.dev v0.0.0-20240501110514-25594c80d273 h1:+JpKw02VTAcOjJw7Q6juun/9hk9ypNSdTRlf+E4M5Nw=
|
ucl.lmika.dev v0.0.0-20240501110514-25594c80d273 h1:+JpKw02VTAcOjJw7Q6juun/9hk9ypNSdTRlf+E4M5Nw=
|
||||||
ucl.lmika.dev v0.0.0-20240501110514-25594c80d273/go.mod h1:T6V4jIUxlWvMTgn4J752VDHNA8iyVrEX6v98EvDj8G4=
|
ucl.lmika.dev v0.0.0-20240501110514-25594c80d273/go.mod h1:T6V4jIUxlWvMTgn4J752VDHNA8iyVrEX6v98EvDj8G4=
|
||||||
|
ucl.lmika.dev v0.0.0-20240504001444-cf3a12bf0d4d h1:OqGmR0Y+OG6aFIOlXy2QwEHtuUNasYCh/6cxHokYQj4=
|
||||||
|
ucl.lmika.dev v0.0.0-20240504001444-cf3a12bf0d4d/go.mod h1:T6V4jIUxlWvMTgn4J752VDHNA8iyVrEX6v98EvDj8G4=
|
||||||
|
ucl.lmika.dev v0.0.0-20240504013531-0dc9fd3c3281 h1:/M7phiv/0XVp3wKkOxEnGQysf8+RS6NOaBQZyUEoSsA=
|
||||||
|
ucl.lmika.dev v0.0.0-20240504013531-0dc9fd3c3281/go.mod h1:T6V4jIUxlWvMTgn4J752VDHNA8iyVrEX6v98EvDj8G4=
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package commandctrl
|
package commandctrl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -11,6 +9,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"ucl.lmika.dev/ucl"
|
"ucl.lmika.dev/ucl"
|
||||||
|
"ucl.lmika.dev/ucl/builtins"
|
||||||
|
|
||||||
"github.com/lmika/dynamo-browse/internal/common/ui/events"
|
"github.com/lmika/dynamo-browse/internal/common/ui/events"
|
||||||
"github.com/lmika/shellwords"
|
"github.com/lmika/shellwords"
|
||||||
|
@ -25,6 +24,7 @@ type CommandController struct {
|
||||||
lookupExtensions []CommandLookupExtension
|
lookupExtensions []CommandLookupExtension
|
||||||
completionProvider CommandCompletionProvider
|
completionProvider CommandCompletionProvider
|
||||||
msgChan chan tea.Msg
|
msgChan chan tea.Msg
|
||||||
|
interactive bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCommandController(historyProvider IterProvider) *CommandController {
|
func NewCommandController(historyProvider IterProvider) *CommandController {
|
||||||
|
@ -33,10 +33,13 @@ func NewCommandController(historyProvider IterProvider) *CommandController {
|
||||||
commandList: nil,
|
commandList: nil,
|
||||||
lookupExtensions: nil,
|
lookupExtensions: nil,
|
||||||
msgChan: make(chan tea.Msg),
|
msgChan: make(chan tea.Msg),
|
||||||
|
interactive: true,
|
||||||
}
|
}
|
||||||
cc.uclInst = ucl.New(
|
cc.uclInst = ucl.New(
|
||||||
ucl.WithOut(ucl.LineHandler(cc.printLine)),
|
ucl.WithOut(ucl.LineHandler(cc.printLine)),
|
||||||
ucl.WithMissingBuiltinHandler(cc.cmdInvoker),
|
ucl.WithMissingBuiltinHandler(cc.cmdInvoker),
|
||||||
|
ucl.WithModule(builtins.OS()),
|
||||||
|
ucl.WithModule(builtins.FS(nil)),
|
||||||
)
|
)
|
||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
@ -136,6 +139,12 @@ func (c *CommandController) lookupCommand(name string) Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommandController) ExecuteFile(filename string) error {
|
func (c *CommandController) ExecuteFile(filename string) error {
|
||||||
|
oldInteractive := c.interactive
|
||||||
|
c.interactive = false
|
||||||
|
defer func() {
|
||||||
|
c.interactive = oldInteractive
|
||||||
|
}()
|
||||||
|
|
||||||
baseFilename := filepath.Base(filename)
|
baseFilename := filepath.Base(filename)
|
||||||
|
|
||||||
if rcFile, err := os.ReadFile(filename); err == nil {
|
if rcFile, err := os.ReadFile(filename); err == nil {
|
||||||
|
@ -149,27 +158,14 @@ func (c *CommandController) ExecuteFile(filename string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommandController) executeFile(file []byte, filename string) error {
|
func (c *CommandController) executeFile(file []byte, filename string) error {
|
||||||
scnr := bufio.NewScanner(bytes.NewReader(file))
|
msg := c.execute(ExecContext{FromFile: true}, string(file))
|
||||||
|
|
||||||
lineNo := 0
|
|
||||||
for scnr.Scan() {
|
|
||||||
lineNo++
|
|
||||||
line := strings.TrimSpace(scnr.Text())
|
|
||||||
if line == "" {
|
|
||||||
continue
|
|
||||||
} else if line[0] == '#' {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := c.execute(ExecContext{FromFile: true}, line)
|
|
||||||
switch m := msg.(type) {
|
switch m := msg.(type) {
|
||||||
case events.ErrorMsg:
|
case events.ErrorMsg:
|
||||||
log.Printf("%v:%v: error - %v", filename, lineNo, m.Error())
|
log.Printf("%v: error - %v", filename, m.Error())
|
||||||
case events.StatusMsg:
|
case events.StatusMsg:
|
||||||
log.Printf("%v:%v: %v", filename, lineNo, string(m))
|
log.Printf("%v: %v", filename, string(m))
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
return scnr.Err()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommandController) cmdInvoker(ctx context.Context, name string, args ucl.CallArgs) (any, error) {
|
func (c *CommandController) cmdInvoker(ctx context.Context, name string, args ucl.CallArgs) (any, error) {
|
||||||
|
@ -186,6 +182,11 @@ func (c *CommandController) cmdInvoker(ctx context.Context, name string, args uc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommandController) printLine(s string) {
|
func (c *CommandController) printLine(s string) {
|
||||||
|
if c.msgChan == nil || !c.interactive {
|
||||||
|
log.Println(s)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case c.msgChan <- events.StatusMsg(s):
|
case c.msgChan <- events.StatusMsg(s):
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in a new issue