Skip to content

Commit

Permalink
cli/addons/instant: Add new addon for "instant mode".
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaq committed Dec 5, 2019
1 parent c610bf4 commit 32f0714
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
90 changes: 90 additions & 0 deletions cli/addons/instant/instant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Package instant implements an addon that executes code whenever it changes
// and shows the result.
package instant

import (
"github.com/elves/elvish/cli"
"github.com/elves/elvish/cli/el"
"github.com/elves/elvish/cli/el/layout"
"github.com/elves/elvish/cli/el/textview"
"github.com/elves/elvish/cli/term"
"github.com/elves/elvish/ui"
)

// Config keeps the configuration for the instant addon.
type Config struct {
// Keybinding.
Binding el.Handler
// The function to execute code and returns the output.
Execute func(code string) ([]string, error)
}

type widget struct {
Config
app cli.App
textView textview.Widget
lastCode string
lastErr error
}

func (w *widget) Render(width, height int) *term.Buffer {
bb := term.NewBufferBuilder(width).
WriteStyled(layout.ModeLine(" INSTANT ", false)).SetDotHere()
if w.lastErr != nil {
bb.Newline().Write(w.lastErr.Error(), ui.Red)
}
buf := bb.Buffer()
if len(buf.Lines) >= height {
buf.TrimToLines(0, height)
return buf
}
bufTextView := w.textView.Render(width, height-len(buf.Lines))
buf.Extend(bufTextView, false)
return buf
}

func (w *widget) Focus() bool { return false }

func (w *widget) Handle(event term.Event) bool {
handled := w.Binding.Handle(event)
if !handled {
codeArea := w.app.CodeArea()
handled = codeArea.Handle(event)
}
w.update(false)
return handled
}

func (w *widget) update(force bool) {
code := w.app.CodeArea().CopyState().Buffer.Content
if code == w.lastCode && !force {
return
}
w.lastCode = code
output, err := w.Execute(code)
w.lastErr = err
if err == nil {
w.textView.MutateState(func(s *textview.State) {
*s = textview.State{Lines: output, First: 0}
})
}
}

// Start starts the addon.
func Start(app cli.App, cfg Config) {
if cfg.Execute == nil {
app.Notify("executor is required")
return
}
if cfg.Binding == nil {
cfg.Binding = el.DummyHandler{}
}
w := widget{
Config: cfg,
app: app,
textView: textview.New(textview.Spec{Scrollable: true}),
}
app.MutateState(func(s *cli.State) { s.Addon = &w })
w.update(true)
app.Redraw()
}
78 changes: 78 additions & 0 deletions cli/addons/instant/instant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package instant

import (
"errors"
"testing"

. "github.com/elves/elvish/cli/apptest"
"github.com/elves/elvish/cli/term"
"github.com/elves/elvish/ui"
)

var styles = ui.RuneStylesheet{
'*': ui.Stylings(ui.Bold, ui.LightGray, ui.BgMagenta),
'!': ui.Red,
}

func setupStarted(t *testing.T) *Fixture {
f := Setup()
Start(f.App, Config{
Execute: func(code string) ([]string, error) {
var err error
if code == "!" {
err = errors.New("error")
}
return []string{"result of", code}, err
},
})
f.TestTTY(t,
term.DotHere, "\n",
" INSTANT \n", styles,
"*********",
"result of\n",
"",
)
return f
}

func TestUpdate(t *testing.T) {
f := setupStarted(t)
defer f.Stop()

f.TTY.Inject(term.K('a'))
bufA := f.MakeBuffer(
"a", term.DotHere, "\n",
" INSTANT \n", styles,
"*********",
"result of\n",
"a",
)
f.TTY.TestBuffer(t, bufA)

f.TTY.Inject(term.K(ui.Right))
f.TTY.TestBuffer(t, bufA)
}

func TestError(t *testing.T) {
f := setupStarted(t)
defer f.Stop()

f.TTY.Inject(term.K('!'))
f.TestTTY(t,
"!", term.DotHere, "\n",
" INSTANT \n", styles,
"*********",
// Error shown.
"error\n", styles,
"!!!!!",
// Buffer not updated.
"result of\n",
"",
)
}

func TestStart_NoExecutor(t *testing.T) {
f := Setup()
Start(f.App, Config{})
f.TestTTYNotes(t, "executor is required")
}

0 comments on commit 32f0714

Please sign in to comment.