forked from mirror/readline
Merge pull request #23 from lunixbochs/master
add stdin remapping support
This commit is contained in:
commit
94a70819f4
|
@ -4,7 +4,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
@ -330,7 +329,7 @@ func (o *Operation) Password(prompt string) ([]byte, error) {
|
|||
o.t.EnterRawMode()
|
||||
defer o.t.ExitRawMode()
|
||||
|
||||
b, err := terminal.ReadPassword(int(os.Stdin.Fd()))
|
||||
b, err := terminal.ReadPassword(int(o.cfg.Stdin.Fd()))
|
||||
fmt.Fprint(w, "\r\n")
|
||||
return b, err
|
||||
}
|
||||
|
|
14
readline.go
14
readline.go
|
@ -1,6 +1,9 @@
|
|||
package readline
|
||||
|
||||
import "io"
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
Config *Config
|
||||
|
@ -8,6 +11,11 @@ type Instance struct {
|
|||
Operation *Operation
|
||||
}
|
||||
|
||||
type FdReader interface {
|
||||
io.Reader
|
||||
Fd() uintptr
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
// prompt supports ANSI escape sequence, so we can color some characters even in windows
|
||||
Prompt string
|
||||
|
@ -30,6 +38,7 @@ type Config struct {
|
|||
InterruptPrompt string
|
||||
EOFPrompt string
|
||||
|
||||
Stdin FdReader
|
||||
Stdout io.Writer
|
||||
Stderr io.Writer
|
||||
|
||||
|
@ -46,6 +55,9 @@ func (c *Config) Init() error {
|
|||
return nil
|
||||
}
|
||||
c.inited = true
|
||||
if c.Stdin == nil {
|
||||
c.Stdin = os.Stdin
|
||||
}
|
||||
if c.Stdout == nil {
|
||||
c.Stdout = Stdout
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ func NewTerminal(cfg *Config) (*Terminal, error) {
|
|||
}
|
||||
|
||||
func (t *Terminal) EnterRawMode() (err error) {
|
||||
t.state, err = MakeRaw(StdinFd)
|
||||
t.state, err = MakeRaw(int(t.cfg.Stdin.Fd()))
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ func (t *Terminal) ExitRawMode() (err error) {
|
|||
if t.state == nil {
|
||||
return
|
||||
}
|
||||
err = Restore(StdinFd, t.state)
|
||||
err = Restore(int(t.cfg.Stdin.Fd()), t.state)
|
||||
if err == nil {
|
||||
t.state = nil
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ func (t *Terminal) ioloop() {
|
|||
expectNextChar bool
|
||||
)
|
||||
|
||||
buf := bufio.NewReader(Stdin)
|
||||
buf := bufio.NewReader(t.cfg.Stdin)
|
||||
for {
|
||||
if !expectNextChar {
|
||||
atomic.StoreInt32(&t.isReading, 0)
|
||||
|
|
Loading…
Reference in New Issue