Skip to content

Commit

Permalink
Merge pull request #23 from lunixbochs/master
Browse files Browse the repository at this point in the history
add stdin remapping support
  • Loading branch information
chzyer committed Dec 24, 2015
2 parents 76dfcd2 + 05d3fbc commit 94a7081
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
3 changes: 1 addition & 2 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"os"

"golang.org/x/crypto/ssh/terminal"
)
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 13 additions & 1 deletion readline.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package readline

import "io"
import (
"io"
"os"
)

type Instance struct {
Config *Config
Terminal *Terminal
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
Expand All @@ -30,6 +38,7 @@ type Config struct {
InterruptPrompt string
EOFPrompt string

Stdin FdReader
Stdout io.Writer
Stderr io.Writer

Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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
}

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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 94a7081

Please sign in to comment.