forked from segmentio/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt.go
167 lines (141 loc) · 2.81 KB
/
prompt.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package prompt
import (
"bufio"
"fmt"
"strconv"
"strings"
"github.com/howeyc/gopass"
)
// String prompt.
func String(msg string, args ...interface{}) string {
var s string
prompt(msg, args...)
fmt.Scanln(&s)
return s
}
// String prompt (required).
func StringRequired(msg string, args ...interface{}) (s string) {
for strings.Trim(s, " ") == "" {
s = String(msg, args...)
}
return s
}
// String default (required).
func StringDefault(msg string, dvalue string) (s string) {
fmt.Printf("%s (default value: %s)", msg, dvalue)
fmt.Scanln(&s)
if strings.Trim(s, " ") == "" {
return dvalue
}
return s
}
// Stringln reads multi spaced sentence until newline
func Stringln(scanner *bufio.Scanner, msg string) string {
prompt(msg)
scanner.Scan()
return scanner.Text()
}
// Integer prompt (required).
func IntegerRequired(msg string, args ...interface{}) int {
for {
n := Integer(msg, args...)
if n != 0 {
return n
}
}
}
// Integer prompt
func Integer(msg string, args ...interface{}) int {
s := String(msg, args...)
n, _ := strconv.Atoi(s)
return n
}
// Confirm continues prompting until the input is boolean-ish.
func Confirm(msg string, args ...interface{}) bool {
for {
switch String(msg, args...) {
case "Yes", "yes", "y", "Y":
return true
case "No", "no", "n", "N":
return false
}
}
}
// Choose prompts for a single selection from `list`, returning in the index.
func Choose(msg string, list []string) int {
fmt.Println()
for i, val := range list {
fmt.Printf(" %d) %s\n", i+1, val)
}
fmt.Println()
i := -1
for {
s := String(msg)
// index
n, err := strconv.Atoi(s)
if err == nil {
if n > 0 && n <= len(list) {
i = n - 1
break
} else {
continue
}
}
// value
i = indexOf(s, list)
if i != -1 {
break
}
}
return i
}
// Choose prompts for a single selection from `list`, returning in the index.
func ChooseInterface(msg string, list []interface{}) int {
fmt.Println()
for i, val := range list {
fmt.Printf(" %d) %+v\n", i+1, val)
}
fmt.Println()
i := -1
for {
s := String(msg)
// index
n, err := strconv.Atoi(s)
if err != nil {
continue
}
if n > 0 && n <= len(list) {
i = n - 1
break
} else {
continue
}
}
return i
}
// Password prompt.
func Password(msg string, args ...interface{}) string {
prompt(msg, args...)
password, _ := gopass.GetPasswd()
s := string(password[0:])
return s
}
// Password prompt with mask.
func PasswordMasked(msg string, args ...interface{}) string {
prompt(msg, args...)
password, _ := gopass.GetPasswdMasked()
s := string(password[0:])
return s
}
// index of `s` in `list`.
func indexOf(s string, list []string) int {
for i, val := range list {
if val == s {
return i
}
}
return -1
}
func prompt(msg string, args ...interface{}) {
fmt.Printf(msg+": ", args...)
}