forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
68 lines (60 loc) · 1.18 KB
/
util.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
package util
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"github.com/fatih/color"
)
// CHA map
const CHA = "abcdefghijklmnopqrstuvwxyz0123456789"
// RandString n is the length. a-z 0-9
func RandString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = CHA[rand.Intn(len(CHA))]
}
return string(b)
}
// Scanline scan line
func Scanline() string {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
return scanner.Text()
}
fmt.Println("interrupted")
os.Exit(1)
return ""
}
// ScanlineTrim scan line and trim
func ScanlineTrim() string {
return strings.TrimSpace(Scanline())
}
// ChooseIndex return valid index in [0, maxLen)
func ChooseIndex(maxLen int) int {
color.Cyan("Please choose one(index): ")
for {
index := ScanlineTrim()
i, err := strconv.Atoi(index)
if err == nil && i >= 0 && i < maxLen {
return i
}
color.Red("Invalid index! Please try again: ")
}
}
// YesOrNo must choose one
func YesOrNo(note string) bool {
color.Cyan(note)
for {
tmp := ScanlineTrim()
if tmp == "y" || tmp == "Y" {
return true
}
if tmp == "n" || tmp == "N" {
return false
}
color.Red("Invalid input. Please input again: ")
}
}