forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.go
84 lines (71 loc) · 1.99 KB
/
password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package survey
import (
"os"
"gopkg.in/AlecAivazis/survey.v1/core"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
/*
Password is like a normal Input but the text shows up as *'s and there is no default. Response
type is a string.
password := ""
prompt := &survey.Password{ Message: "Please type your password" }
survey.AskOne(prompt, &password, nil)
*/
type Password struct {
core.Renderer
Message string
Help string
}
type PasswordTemplateData struct {
Password
ShowHelp bool
}
// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var PasswordQuestionTemplate = `
{{- if .ShowHelp }}{{- color "cyan"}}{{ HelpIcon }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color "green+hb"}}{{ QuestionIcon }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ HelpInputRune }} for help]{{color "reset"}} {{end}}`
func (p *Password) Prompt() (line interface{}, err error) {
// render the question template
out, err := core.RunTemplate(
PasswordQuestionTemplate,
PasswordTemplateData{Password: *p},
)
terminal.Print(out)
if err != nil {
return "", err
}
rr := terminal.NewRuneReader(os.Stdin)
rr.SetTermMode()
defer rr.RestoreTermMode()
// no help msg? Just return any response
if p.Help == "" {
line, err := rr.ReadLine('*')
return string(line), err
}
// process answers looking for help prompt answer
for {
line, err := rr.ReadLine('*')
if err != nil {
return string(line), err
}
if string(line) == string(core.HelpInputRune) {
// terminal will echo the \n so we need to jump back up one row
terminal.CursorPreviousLine(1)
err = p.Render(
PasswordQuestionTemplate,
PasswordTemplateData{Password: *p, ShowHelp: true},
)
if err != nil {
return "", err
}
continue
}
return string(line), err
}
}
// Cleanup hides the string with a fixed number of characters.
func (prompt *Password) Cleanup(val interface{}) error {
return nil
}