-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interaction.go
222 lines (211 loc) · 4.59 KB
/
Interaction.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package hyper_interactive
import (
"bufio"
"bytes"
"fmt"
"os"
"strconv"
"strings"
"time"
)
var (
TermWidth = 80
)
func printSelect(options []string, startFrom int) {
optionDisplay := make([]string, len(options))
for idx, str := range options {
optionDisplay[idx] = fmt.Sprintf("(%d) %s", idx+startFrom, str)
}
longest := 0
for _, str := range optionDisplay {
if len(str)+2 > longest {
longest = len(str) + 2
}
}
columns := TermWidth / longest
rows := len(options) / columns
if len(options)%columns >= 0 {
rows++
}
cols := make([][]string, columns)
for idx, _ := range cols {
cols[idx] = make([]string, rows)
}
offset := 0
for cidx := 0; cidx < len(cols); cidx++ {
for ridx := 0; ridx < len(cols[cidx]); ridx++ {
if offset < len(optionDisplay) {
cols[cidx][ridx] = optionDisplay[offset]
}
offset++
}
}
for ridx := 0; ridx < rows; ridx++ {
for cidx := 0; cidx < columns; cidx++ {
fmt.Print(padString(cols[cidx][ridx], longest))
}
fmt.Println()
}
}
func Select(question string, options []string, startFrom, defaultOption int, confirm bool) int {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Printf("%s:\n", question)
printSelect(options, startFrom)
fmt.Printf("Choose from number above [default : (%d) %s] ? ", defaultOption, options[startFrom-defaultOption])
scanner.Scan()
text := scanner.Text()
if len(text) == 0 {
return defaultOption
}
if chosen, err := strconv.Atoi(text); err != nil {
fmt.Println("Invalid input")
continue
} else {
if chosen >= startFrom && chosen < len(options)+startFrom {
if confirm {
if Confirm(fmt.Sprintf("(%d) %s - Are you sure ? ", chosen, options[chosen-startFrom]), true) {
return chosen
}
} else {
return chosen
}
continue
} else {
fmt.Printf("Number %d is not valid option\n", chosen)
continue
}
}
}
}
func AskNumber(question string, from, to, def int, confirm bool) int {
scanner := bufio.NewScanner(os.Stdin)
var min, max int
if from < to {
min = from
max = to
} else {
min = to
max = from
}
for {
fmt.Printf("%s (%d - %d) [default %d] : ", question, min, max, def)
scanner.Scan()
text := scanner.Text()
if len(text) == 0 {
if confirm {
if Confirm(fmt.Sprintf("%d, are you sure?", def), true) {
return def
} else {
continue
}
} else {
return def
}
}
num, err := strconv.Atoi(text)
if err != nil {
fmt.Printf("%s is not a valid number\n", text)
continue
}
if num < min || num > max {
fmt.Printf("%d is not in range between %d and %d\n", num, min, max)
continue
}
if Confirm(fmt.Sprintf("%d, are you sure?", num), true) {
return num
} else {
continue
}
}
}
func AskTime(question string, def time.Time, confirm bool) time.Time {
format := "2006-01-02 15:04:05 -0700"
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Printf("%s [default \"%s\"] : ", question, def.Format(format))
scanner.Scan()
text := scanner.Text()
if len(text) == 0 {
if confirm {
if Confirm(fmt.Sprintf("\"%s\", are you sure?", def.Format(format)), true) {
return def
} else {
continue
}
} else {
return def
}
}
theTime, err := time.Parse(format, text)
if err != nil {
fmt.Printf("\"%s\" is not a valid time format\n", text)
continue
}
if Confirm(fmt.Sprintf("\"%s\", are you sure?", text), true) {
return theTime
} else {
continue
}
}
}
func Ask(question, defaultAnswer string, confirm bool) string {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Printf("%s [default \"%s\"] : ", question, defaultAnswer)
scanner.Scan()
text := scanner.Text()
if len(text) == 0 {
if confirm {
if Confirm(fmt.Sprintf("\"%s\", are you sure?", defaultAnswer), true) {
return defaultAnswer
} else {
continue
}
} else {
return defaultAnswer
}
}
if confirm {
if Confirm(fmt.Sprintf("\"%s\", are you sure?", text), true) {
return text
} else {
continue
}
} else {
return text
}
}
}
func Confirm(question string, def bool) bool {
scanner := bufio.NewScanner(os.Stdin)
var defText string
if def {
defText = "Y"
} else {
defText = "N"
}
for {
fmt.Printf("%s (y/n/Y/N) [default : %s] ? ", question, defText)
scanner.Scan()
text := scanner.Text()
if len(text) == 0 {
return def
}
if strings.ToUpper(text) == "Y" {
return true
}
if strings.ToUpper(text) == "N" {
return false
}
fmt.Printf("Invalid input")
}
}
func padString(str string, length int) string {
buff := &bytes.Buffer{}
buff.WriteString(str)
for buff.Len() < length {
buff.WriteString(" ")
}
return buff.String()
}