github.com/elves/elvish@v0.15.0/pkg/edit/instant.go (about) 1 package edit 2 3 import ( 4 "github.com/elves/elvish/pkg/cli" 5 "github.com/elves/elvish/pkg/cli/addons/instant" 6 "github.com/elves/elvish/pkg/eval" 7 "github.com/elves/elvish/pkg/parse" 8 ) 9 10 //elvdoc:var -instant:binding 11 // 12 // Binding for the instant mode. 13 14 //elvdoc:fn -instant:start 15 // 16 // Starts the instant mode. In instant mode, any text entered at the command 17 // line is evaluated immediately, with the output displayed. 18 // 19 // **WARNING**: Beware of unintended consequences when using destructive 20 // commands. For example, if you type `sudo rm -rf /tmp/*` in the instant mode, 21 // Elvish will attempt to evaluate `sudo rm -rf /` when you typed that far. 22 23 func initInstant(ed *Editor, ev *eval.Evaler, nb eval.NsBuilder) { 24 bindingVar := newBindingVar(EmptyBindingMap) 25 binding := newMapBinding(ed, ev, bindingVar) 26 nb.AddNs("-instant", 27 eval.NsBuilder{ 28 "binding": bindingVar, 29 }.AddGoFns("<edit:-instant>:", map[string]interface{}{ 30 "start": func() { instantStart(ed.app, ev, binding) }, 31 }).Ns()) 32 } 33 34 func instantStart(app cli.App, ev *eval.Evaler, binding cli.Handler) { 35 execute := func(code string) ([]string, error) { 36 outPort, collect, err := eval.StringCapturePort() 37 if err != nil { 38 return nil, err 39 } 40 err = ev.Eval( 41 parse.Source{Name: "[instant]", Code: code}, 42 eval.EvalCfg{ 43 Ports: []*eval.Port{nil, outPort}, 44 Interrupt: eval.ListenInterrupts}) 45 return collect(), err 46 } 47 instant.Start(app, instant.Config{Binding: binding, Execute: execute}) 48 }