2019-12-18 18:38:21 +08:00
|
|
|
package edit
|
2019-11-03 09:22:56 +08:00
|
|
|
|
|
|
|
import (
|
2024-04-11 06:11:21 +08:00
|
|
|
"errors"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/cli"
|
2021-02-12 02:49:00 +08:00
|
|
|
"src.elv.sh/pkg/cli/tk"
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval"
|
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
"src.elv.sh/pkg/eval/vars"
|
2019-11-03 09:22:56 +08:00
|
|
|
)
|
|
|
|
|
2024-04-11 06:11:21 +08:00
|
|
|
var errDotOutOfBoundary = errors.New("dot out of command boundary")
|
|
|
|
|
2019-11-04 07:27:16 +08:00
|
|
|
func insertAtDot(app cli.App, text string) {
|
2021-09-02 05:41:39 +08:00
|
|
|
codeArea, ok := focusedCodeArea(app)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
codeArea.MutateState(func(s *tk.CodeAreaState) {
|
2019-11-04 07:27:16 +08:00
|
|
|
s.Buffer.InsertAtDot(text)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func replaceInput(app cli.App, text string) {
|
2021-09-02 05:41:39 +08:00
|
|
|
codeArea, ok := focusedCodeArea(app)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
codeArea.MutateState(func(s *tk.CodeAreaState) {
|
2021-02-12 05:44:41 +08:00
|
|
|
s.Buffer = tk.CodeBuffer{Content: text, Dot: len(text)}
|
|
|
|
})
|
2019-11-04 07:27:16 +08:00
|
|
|
}
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
func initStateAPI(app cli.App, nb eval.NsBuilder) {
|
2021-09-02 05:41:39 +08:00
|
|
|
// State API always operates on the root CodeArea widget
|
|
|
|
codeArea := app.ActiveWidget().(tk.CodeArea)
|
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
nb.AddGoFns(map[string]any{
|
2019-11-04 07:27:16 +08:00
|
|
|
"insert-at-dot": func(s string) { insertAtDot(app, s) },
|
|
|
|
"replace-input": func(s string) { replaceInput(app, s) },
|
|
|
|
})
|
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
setDot := func(v any) error {
|
2019-11-03 09:22:56 +08:00
|
|
|
var dot int
|
|
|
|
err := vals.ScanToGo(v, &dot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-02 05:41:39 +08:00
|
|
|
codeArea.MutateState(func(s *tk.CodeAreaState) {
|
2024-04-22 23:01:52 +08:00
|
|
|
if dot < 0 || dot > len(s.Buffer.Content) {
|
|
|
|
err = errDotOutOfBoundary
|
|
|
|
} else {
|
|
|
|
s.Buffer.Dot = dot
|
|
|
|
}
|
2019-11-03 09:22:56 +08:00
|
|
|
})
|
2024-04-22 23:01:52 +08:00
|
|
|
return err
|
2019-11-03 09:22:56 +08:00
|
|
|
}
|
2022-03-20 23:50:25 +08:00
|
|
|
getDot := func() any {
|
2021-09-02 05:41:39 +08:00
|
|
|
return vals.FromGo(codeArea.CopyState().Buffer.Dot)
|
2019-11-03 09:22:56 +08:00
|
|
|
}
|
2021-10-24 04:44:11 +08:00
|
|
|
nb.AddVar("-dot", vars.FromSetGet(setDot, getDot))
|
2019-11-03 09:22:56 +08:00
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
setCurrentCommand := func(v any) error {
|
2019-11-03 09:22:56 +08:00
|
|
|
var content string
|
|
|
|
err := vals.ScanToGo(v, &content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-04 07:27:16 +08:00
|
|
|
replaceInput(app, content)
|
2019-11-03 09:22:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-20 23:50:25 +08:00
|
|
|
getCurrentCommand := func() any {
|
2021-09-02 05:41:39 +08:00
|
|
|
return vals.FromGo(codeArea.CopyState().Buffer.Content)
|
2019-11-03 09:22:56 +08:00
|
|
|
}
|
2021-10-24 04:44:11 +08:00
|
|
|
nb.AddVar("current-command", vars.FromSetGet(setCurrentCommand, getCurrentCommand))
|
2019-11-03 09:22:56 +08:00
|
|
|
}
|