forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
115 lines (102 loc) · 2.81 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package survey
import (
"strings"
)
/*
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)
*/
type Password struct {
Renderer
Message string
Help string
answer string
}
type PasswordTemplateData struct {
Password
ShowHelp bool
Config *PromptConfig
ShowAnswer bool
Answer string
}
// PasswordQuestionTemplate is a template with color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var PasswordQuestionTemplate = `
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- if .ShowAnswer}}
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
{{- else }}
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ .Config.HelpInput }} for help]{{color "reset"}} {{end}}
{{- end }}`
func (p *Password) Prompt(config *PromptConfig) (interface{}, error) {
// render the question template
err := p.Render(
PasswordQuestionTemplate,
PasswordTemplateData{
Password: *p,
Config: config,
},
)
rr := p.NewRuneReader()
_ = rr.SetTermMode()
defer func() {
_ = rr.RestoreTermMode()
}()
cursor := p.NewCursor()
// no help msg? Just return any response
if p.Help == "" {
line, err := rr.ReadLine(config.HideCharacter)
p.answer = string(line)
if err != nil {
return p.answer, err
}
cursor.PreviousLine(1)
p.AppendRenderedText(strings.Repeat(string(config.HideCharacter), len(p.answer)))
return p.answer, err
}
var line []rune
// process answers looking for help prompt answer
for {
line, err = rr.ReadLine(config.HideCharacter)
p.answer = string(line)
if err != nil {
return p.answer, err
}
if p.answer == config.HelpInput {
// terminal will echo the \n so we need to jump back up one row
cursor.PreviousLine(1)
err = p.Render(
PasswordQuestionTemplate,
PasswordTemplateData{
Password: *p,
ShowHelp: true,
Config: config,
},
)
if err != nil {
return "", err
}
continue
}
break
}
p.AppendRenderedText(strings.Repeat(string(config.HideCharacter), len(p.answer)))
cursor.PreviousLine(1)
return p.answer, err
}
// Cleanup re-generates the input as the hide character
func (prompt *Password) Cleanup(config *PromptConfig, val interface{}) error {
return prompt.Render(
PasswordQuestionTemplate,
PasswordTemplateData{
Password: *prompt,
ShowHelp: false,
Config: config,
ShowAnswer: true,
Answer: strings.Repeat(string(config.HideCharacter), len(prompt.answer)),
})
}