mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-14 02:57:52 +08:00
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:
parent
1b6cee7045
commit
5968460d61
27
shell/editor.go
Normal file
27
shell/editor.go
Normal 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() {
|
||||
}
|
|
@ -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.
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user