package mode
import (
)
type Instant interface {
tk.Widget
}
type InstantSpec struct {
Bindings tk.Bindings
Execute func(code string) ([]string, error)
}
type instant struct {
InstantSpec
app cli.App
textView tk.TextView
lastCode string
lastErr error
}
func ( *instant) (, int) *term.Buffer {
:= term.NewBufferBuilder().
WriteStyled(modeLine(" INSTANT ", false)).SetDotHere()
if .lastErr != nil {
.Newline().Write(.lastErr.Error(), ui.FgRed)
}
:= .Buffer()
if len(.Lines) >= {
.TrimToLines(0, )
return
}
:= .textView.Render(, -len(.Lines))
.Extend(, false)
return
}
func ( *instant) () bool { return false }
func ( *instant) ( term.Event) bool {
:= .Bindings.Handle(, )
if ! {
:= .app.CodeArea()
= .Handle()
}
.update(false)
return
}
func ( *instant) ( bool) {
:= .app.CodeArea().CopyState().Buffer.Content
if == .lastCode && ! {
return
}
.lastCode =
, := .Execute()
.lastErr =
if == nil {
.textView.MutateState(func( *tk.TextViewState) {
* = tk.TextViewState{Lines: , First: 0}
})
}
}
var errExecutorIsRequired = errors.New("executor is required")
func ( cli.App, InstantSpec) (Instant, error) {
if .Execute == nil {
return nil, errExecutorIsRequired
}
if .Bindings == nil {
.Bindings = tk.DummyBindings{}
}
:= instant{
InstantSpec: ,
app: ,
textView: tk.NewTextView(tk.TextViewSpec{Scrollable: true}),
}
.update(true)
return &, nil
}