add readpassword

This commit is contained in:
Cheney 2015-09-30 14:16:46 +08:00
parent 180b650b65
commit 3ccecf626d
3 changed files with 29 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import (
func usage(w io.Writer) {
io.WriteString(w, `
login
setprompt <prompt>
say <hello>
bye
@ -19,6 +20,7 @@ bye
}
var completer = readline.NewPrefixCompleter(
readline.PcItem("login"),
readline.PcItem("say",
readline.PcItem("hello"),
readline.PcItem("bye"),
@ -52,6 +54,12 @@ func main() {
}
switch {
case line == "login":
pswd, err := l.ReadPassword("please enter your password: ")
if err != nil {
break
}
println("you enter:", strconv.Quote(string(pswd)))
case line == "help":
usage(l.Stderr())
case strings.HasPrefix(line, "setprompt"):

View File

@ -1,6 +1,12 @@
package readline
import "io"
import (
"fmt"
"io"
"os"
"golang.org/x/crypto/ssh/terminal"
)
type Operation struct {
cfg *Config
@ -240,6 +246,16 @@ func (o *Operation) Runes() ([]rune, error) {
return r, nil
}
func (o *Operation) Password(prompt string) ([]byte, error) {
w := o.Stdout()
if prompt != "" {
fmt.Fprintf(w, prompt)
}
b, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Fprint(w, "\r\n")
return b, err
}
func (o *Operation) SetTitle(t string) {
o.w.Write([]byte("\033[2;" + t + "\007"))
}

View File

@ -59,6 +59,10 @@ func (i *Instance) Stderr() io.Writer {
return i.o.Stderr()
}
func (i *Instance) ReadPassword(prompt string) ([]byte, error) {
return i.o.Password(prompt)
}
func (i *Instance) Readline() (string, error) {
return i.o.String()
}