/
userinput.go
58 lines (45 loc) · 1.18 KB
/
userinput.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
package lib
// From https://github.com/InfinityBotList/Popplio/blob/main/cmd/kitehelper/common/userinput.go
import (
"fmt"
"github.com/fatih/color"
)
var (
StatusBoldBlue = color.New(color.Bold, color.FgBlue).PrintlnFunc()
StatusGood = color.New(color.Bold, color.FgCyan).PrintlnFunc()
StatusBoldYellow = color.New(color.Bold, color.FgYellow).PrintlnFunc()
StatusBoldYellowNoNewline = color.New(color.Bold, color.FgYellow).PrintfFunc()
)
func UserInputBoolean(prompt string) bool {
for {
var input string
StatusBoldYellow(prompt + " (y/n): ")
_, err := fmt.Scanln(&input)
if err != nil {
panic(err)
}
if input == "y" || input == "Y" {
return true
}
if input == "n" || input == "N" {
return false
}
StatusBoldYellow("Invalid input, please try again.")
}
}
func UserInput(prompt string) string {
for {
var input string
StatusBoldYellowNoNewline("\n" + prompt + ": ")
_, err := fmt.Scanf("%s", &input)
if err != nil {
StatusBoldYellow("Invalid input, please try again.", err.Error())
continue
}
if input == "" {
StatusBoldYellow("Invalid input, please try again.")
continue
}
return input
}
}