elvish/pkg/edit/instant.go

84 lines
2.2 KiB
Go
Raw Normal View History

package edit
import (
"bufio"
"io"
"os"
"sync"
2019-12-24 04:00:59 +08:00
"github.com/elves/elvish/pkg/cli"
"github.com/elves/elvish/pkg/cli/addons/instant"
"github.com/elves/elvish/pkg/eval"
"github.com/elves/elvish/pkg/eval/vals"
"github.com/elves/elvish/pkg/parse"
"github.com/elves/elvish/pkg/strutil"
)
//elvdoc:var -instant:binding
//
// Binding for the instant mode.
//elvdoc:fn -instant:start
//
// Starts the instant mode. In instant mode, any text entered at the command
// line is evaluated immediately, with the output displayed.
//
// **WARNING**: Beware of unintended consequences when using destructive
// commands. For example, if you type `sudo rm -rf /tmp/*` in the instant mode,
// Elvish will attempt to evaluate `sudo rm -rf /` when you typed that far.
func initInstant(ed *Editor, ev *eval.Evaler) {
bindingVar := newBindingVar(EmptyBindingMap)
binding := newMapBinding(ed, ev, bindingVar)
ed.ns.AddNs("-instant",
eval.Ns{
"binding": bindingVar,
}.AddGoFns("<edit:-instant>:", map[string]interface{}{
"start": func() { instantStart(ed.app, ev, binding) },
}))
}
2019-12-26 08:52:22 +08:00
func instantStart(app cli.App, ev *eval.Evaler, binding cli.Handler) {
execute := func(code string) ([]string, error) {
src := parse.Source{Name: "[instant]", Code: code}
op, err := ev.ParseAndCompile(src, nil)
if err != nil {
return nil, err
}
fm := eval.NewTopFrame(ev, src, []*eval.Port{
{File: eval.DevNull},
{}, // Will be replaced in CaptureOutput
{File: eval.DevNull},
})
var output []string
var outputMutex sync.Mutex
addLine := func(line string) {
outputMutex.Lock()
defer outputMutex.Unlock()
output = append(output, line)
}
valuesCb := func(ch <-chan interface{}) {
for v := range ch {
addLine("▶ " + vals.ToString(v))
}
}
bytesCb := func(r *os.File) {
bufr := bufio.NewReader(r)
for {
line, err := bufr.ReadString('\n')
if err != nil {
if err != io.EOF {
addLine("i/o error: " + err.Error())
}
break
}
addLine(strutil.ChopLineEnding(line))
}
}
err = fm.PipeOutput(
func(fm *eval.Frame) error { return fm.Eval(op) }, valuesCb, bytesCb)
return output, err
}
instant.Start(app, instant.Config{Binding: binding, Execute: execute})
}