forked from riteshpradhan/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
164 lines (139 loc) · 2.84 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
package prompt
import (
"bufio"
"fmt"
"strconv"
"strings"
"github.com/howeyc/gopass"
)
// String prompt.
func String(prompt string, args ...interface{}) string {
var s string
fmt.Printf(prompt+": ", args...)
fmt.Scanln(&s)
return s
}
// String prompt (required).
func StringRequired(prompt string, args ...interface{}) (s string) {
for strings.Trim(s, " ") == "" {
s = String(prompt, args...)
}
return s
}
// String default (required).
func StringDefault(prompt string, dvalue string) (s string) {
fmt.Println()
fmt.Printf("%s (default value: %s)", prompt, dvalue)
fmt.Scanln(&s)
if strings.Trim(s, " ") == "" {
return dvalue
}
return s
}
// Stringln reads multi spaced sentence until newline
func Stringln(scanner *bufio.Scanner, prompt string) string {
fmt.Printf(prompt + ": ")
scanner.Scan()
return scanner.Text()
}
// Integer prompt (required).
func IntegerRequired(prompt string, args ...interface{}) int {
for {
n := Integer(prompt, args...)
if n != 0 {
return n
}
}
}
// Integer prompt
func Integer(prompt string, args ...interface{}) int {
s := String(prompt, args...)
n, _ := strconv.Atoi(s)
return n
}
// Confirm continues prompting until the input is boolean-ish.
func Confirm(prompt string, args ...interface{}) bool {
for {
switch String(prompt, 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(prompt 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(prompt)
// 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(prompt 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(prompt)
// 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(prompt string, args ...interface{}) string {
fmt.Printf(prompt+": ", args...)
password, _ := gopass.GetPasswd()
s := string(password[0:])
return s
}
// Password prompt with mask.
func PasswordMasked(prompt string, args ...interface{}) string {
fmt.Printf(prompt+": ", 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
}