This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
questions.go
142 lines (120 loc) · 2.48 KB
/
questions.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
package questions
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/mattn/go-isatty"
)
func PromptFormattedOptions(text string, def int, options ...string) (int, error) {
var newOptions []string
for i := range options {
newOptions = append(newOptions, fmt.Sprintf("%d. %s\n", i+1, options[i]))
}
return PromptOptions(text+"\n", def, newOptions...)
}
func PromptOptions(text string, def int, options ...string) (int, error) {
if len(options) == 1 {
return 0, nil
}
PrintToTerm(text)
for _, option := range options {
PrintToTerm(option)
}
defString := ""
if def >= 0 {
defString = strconv.Itoa(def + 1)
}
for {
ans, err := Prompt(fmt.Sprintf("Select Number [%s]: ", defString), defString)
if err != nil {
return 0, err
}
num, err := strconv.Atoi(ans)
if err != nil {
PrintfToTerm("Invalid number: %s\n", ans)
continue
}
num--
if num < 0 || num >= len(options) {
PrintlnToTerm("Select a number between 1 and", +len(options))
continue
}
return num, nil
}
}
func PromptBool(text string, def bool) (bool, error) {
msg := fmt.Sprintf("%s [y/N]: ", text)
defStr := "n"
if def {
msg = fmt.Sprintf("%s [Y/n]: ", text)
defStr = "y"
}
for {
yn, err := Prompt(msg, defStr)
if err != nil {
return false, err
}
switch strings.ToLower(yn) {
case "y":
return true, nil
case "n":
return false, nil
default:
fmt.Println("Enter y or n")
}
}
}
func PrintToTerm(text ...interface{}) {
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Print(text...)
} else {
fmt.Fprint(os.Stderr, text...)
}
}
func PrintlnToTerm(text ...interface{}) {
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Println(text...)
} else {
fmt.Fprintln(os.Stderr, text...)
}
}
func PrintfToTerm(msg string, format ...interface{}) {
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Printf(msg, format...)
} else {
fmt.Fprintf(os.Stderr, msg, format...)
}
}
func Prompt(text, def string) (string, error) {
for {
PrintToTerm(text)
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
answer = strings.TrimSpace(answer)
if answer == "" {
answer = def
}
if answer == "" {
continue
}
return answer, nil
}
}
func PromptOptional(text, def string) (string, error) {
for {
PrintToTerm(text)
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
answer = strings.TrimSpace(answer)
if answer == "" {
answer = def
}
return answer, nil
}
}