mirror of https://github.com/chzyer/readline.git
add readpassword
This commit is contained in:
parent
180b650b65
commit
3ccecf626d
|
@ -12,6 +12,7 @@ import (
|
||||||
|
|
||||||
func usage(w io.Writer) {
|
func usage(w io.Writer) {
|
||||||
io.WriteString(w, `
|
io.WriteString(w, `
|
||||||
|
login
|
||||||
setprompt <prompt>
|
setprompt <prompt>
|
||||||
say <hello>
|
say <hello>
|
||||||
bye
|
bye
|
||||||
|
@ -19,6 +20,7 @@ bye
|
||||||
}
|
}
|
||||||
|
|
||||||
var completer = readline.NewPrefixCompleter(
|
var completer = readline.NewPrefixCompleter(
|
||||||
|
readline.PcItem("login"),
|
||||||
readline.PcItem("say",
|
readline.PcItem("say",
|
||||||
readline.PcItem("hello"),
|
readline.PcItem("hello"),
|
||||||
readline.PcItem("bye"),
|
readline.PcItem("bye"),
|
||||||
|
@ -52,6 +54,12 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
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":
|
case line == "help":
|
||||||
usage(l.Stderr())
|
usage(l.Stderr())
|
||||||
case strings.HasPrefix(line, "setprompt"):
|
case strings.HasPrefix(line, "setprompt"):
|
||||||
|
|
18
operation.go
18
operation.go
|
@ -1,6 +1,12 @@
|
||||||
package readline
|
package readline
|
||||||
|
|
||||||
import "io"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
|
)
|
||||||
|
|
||||||
type Operation struct {
|
type Operation struct {
|
||||||
cfg *Config
|
cfg *Config
|
||||||
|
@ -240,6 +246,16 @@ func (o *Operation) Runes() ([]rune, error) {
|
||||||
return r, nil
|
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) {
|
func (o *Operation) SetTitle(t string) {
|
||||||
o.w.Write([]byte("\033[2;" + t + "\007"))
|
o.w.Write([]byte("\033[2;" + t + "\007"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,6 +59,10 @@ func (i *Instance) Stderr() io.Writer {
|
||||||
return i.o.Stderr()
|
return i.o.Stderr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Instance) ReadPassword(prompt string) ([]byte, error) {
|
||||||
|
return i.o.Password(prompt)
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Instance) Readline() (string, error) {
|
func (i *Instance) Readline() (string, error) {
|
||||||
return i.o.String()
|
return i.o.String()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue