-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cliedit: Integrate the "instant" addon.
- Loading branch information
Showing
5 changed files
with
148 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cliedit | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/elves/elvish/cli" | ||
"github.com/elves/elvish/cli/addons/instant" | ||
"github.com/elves/elvish/cli/el" | ||
"github.com/elves/elvish/eval" | ||
"github.com/elves/elvish/eval/vals" | ||
"github.com/elves/elvish/parse" | ||
) | ||
|
||
func initInstant(app cli.App, ev *eval.Evaler, ns eval.Ns) { | ||
bindingVar := newBindingVar(EmptyBindingMap) | ||
binding := newMapBinding(app, ev, bindingVar) | ||
ns.AddNs("-instant", | ||
eval.Ns{ | ||
"binding": bindingVar, | ||
}.AddGoFns("<edit:-instant>", map[string]interface{}{ | ||
"start": func() { instantStart(app, ev, binding) }, | ||
})) | ||
} | ||
|
||
func instantStart(app cli.App, ev *eval.Evaler, binding el.Handler) { | ||
execute := func(code string) ([]string, error) { | ||
src := eval.NewInteractiveSource(code) | ||
n, err := parse.AsChunk("[instant]", code) | ||
if err != nil { | ||
return nil, err | ||
} | ||
op, err := ev.Compile(n, src) | ||
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(strings.TrimSuffix(line, "\n")) | ||
} | ||
} | ||
err = fm.ExecWithOutputCallback(op, valuesCb, bytesCb) | ||
return output, err | ||
} | ||
instant.Start(app, instant.Config{Binding: binding, Execute: execute}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cliedit | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/elves/elvish/cli" | ||
"github.com/elves/elvish/cli/el/codearea" | ||
"github.com/elves/elvish/cli/term" | ||
) | ||
|
||
func TestInstantAddon_ValueOutput(t *testing.T) { | ||
f := setup() | ||
defer f.Cleanup() | ||
|
||
evals(f.Evaler, "edit:-instant:start") | ||
f.TestTTY(t, | ||
"~> ", term.DotHere, "\n", | ||
" INSTANT \n", Styles, | ||
"*********", | ||
) | ||
|
||
feedInput(f.TTYCtrl, "+") | ||
f.TestTTY(t, | ||
"~> +", Styles, | ||
" v", term.DotHere, "\n", | ||
" INSTANT \n", Styles, | ||
"*********", | ||
"▶ 0", | ||
) | ||
|
||
feedInput(f.TTYCtrl, " 1 2") | ||
f.TestTTY(t, | ||
"~> + 1 2", Styles, | ||
" v ", term.DotHere, "\n", | ||
" INSTANT \n", Styles, | ||
"*********", | ||
"▶ 3", | ||
) | ||
} | ||
|
||
func TestInstantAddon_ByteOutput(t *testing.T) { | ||
f := setup() | ||
defer f.Cleanup() | ||
|
||
// We don't want to trigger the evaluation of "e", "ec", and "ech", so we | ||
// start with a non-empty code buffer. | ||
cli.SetCodeBuffer(f.Editor.app, codearea.Buffer{Content: "echo ", Dot: 5}) | ||
evals(f.Evaler, "edit:-instant:start") | ||
f.TestTTY(t, | ||
"~> echo ", Styles, | ||
" vvvv ", term.DotHere, "\n", | ||
" INSTANT \n", Styles, | ||
"*********", | ||
) | ||
|
||
feedInput(f.TTYCtrl, "hello") | ||
f.TestTTY(t, | ||
"~> echo hello", Styles, | ||
" vvvv ", term.DotHere, "\n", | ||
" INSTANT \n", Styles, | ||
"*********", | ||
"hello", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters