shell: When stdin is not TTY, use a minimal editor.

The old behavior was to source /dev/stdin, meaning that Elvish can behave very
differently when the stdin is not TTY. By using a minimal editor, the
difference is reduced. This also removes the dependency on /dev/stdin, which
does not exist on Windows.
This commit is contained in:
Qi Xiao 2017-12-03 22:43:35 +01:00
parent 1b6cee7045
commit 5968460d61
3 changed files with 34 additions and 18 deletions

27
shell/editor.go Normal file
View File

@ -0,0 +1,27 @@
package shell
import (
"bufio"
"io"
"os"
)
type editor interface {
ReadLine() (string, error)
Close()
}
type minEditor struct {
in *bufio.Reader
out io.Writer
}
func newMinEditor(in, out *os.File) *minEditor {
return &minEditor{bufio.NewReader(in), out}
}
func (ed *minEditor) ReadLine() (string, error) {
return ed.in.ReadString('\n')
}
func (editor *minEditor) Close() {
}

View File

@ -53,8 +53,6 @@ func (sh *Shell) Run(args []string) int {
} else {
script(sh.ev, arg)
}
} else if !sys.IsATTY(0) {
script(sh.ev, "/dev/stdin")
} else {
interact(sh.ev, sh.daemon)
}
@ -137,7 +135,12 @@ func readFileUTF8(fname string) (string, error) {
func interact(ev *eval.Evaler, daemon *api.Client) {
// Build Editor.
ed := makeEditor(os.Stdin, os.Stderr, ev, daemon)
var ed editor
if sys.IsATTY(0) {
ed = makeEditor(os.Stdin, os.Stderr, ev, daemon)
} else {
ed = newMinEditor(os.Stdin, os.Stderr)
}
defer ed.Close()
// Source rc.elv.

View File

@ -1,25 +1,11 @@
package shell
import (
"bufio"
"io"
"os"
)
type minEditor struct {
in *bufio.Reader
out io.Writer
}
func makeEditor(in, out *os.File, _, _ interface{}) *minEditor {
return &minEditor{bufio.NewReader(in), out}
}
func (ed *minEditor) ReadLine() (string, error) {
return ed.in.ReadString('\n')
}
func (editor *minEditor) Close() {
return newMinEditor(in, out)
}
func handleSignal(_ os.Signal) {