forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
137 lines (115 loc) · 2.65 KB
/
read.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
package cliutil
import (
"bufio"
"io"
"os"
"strings"
"github.com/gookit/color"
"golang.org/x/term"
)
// the global input output stream
var (
// Input global input stream
Input io.Reader = os.Stdin
// Output global output stream
Output io.Writer = os.Stdout
)
// ReadInput read user input form Stdin
func ReadInput(question string) (string, error) {
if len(question) > 0 {
color.Fprint(Output, question)
}
scanner := bufio.NewScanner(Input)
if !scanner.Scan() { // reading
return "", scanner.Err()
}
return strings.TrimSpace(scanner.Text()), nil
}
// ReadLine read one line from user input.
//
// Usage:
//
// in := cliutil.ReadLine("")
// ans, _ := cliutil.ReadLine("your name?")
func ReadLine(question string) (string, error) {
if len(question) > 0 {
color.Fprint(Output, question)
}
reader := bufio.NewReader(Input)
answer, _, err := reader.ReadLine()
return strings.TrimSpace(string(answer)), err
}
// ReadFirst read first char
//
// Usage:
//
// ans, _ := cliutil.ReadFirst("continue?[y/n] ")
func ReadFirst(question string) (string, error) {
answer, err := ReadFirstByte(question)
return string(answer), err
}
// ReadFirstByte read first byte char
//
// Usage:
//
// ans, _ := cliutil.ReadFirstByte("continue?[y/n] ")
func ReadFirstByte(question string) (byte, error) {
if len(question) > 0 {
color.Fprint(Output, question)
}
reader := bufio.NewReader(Input)
return reader.ReadByte()
}
// ReadFirstRune read first rune char
func ReadFirstRune(question string) (rune, error) {
if len(question) > 0 {
color.Fprint(Output, question)
}
reader := bufio.NewReader(Input)
answer, _, err := reader.ReadRune()
return answer, err
}
// ReadAsBool check user inputted answer is right
//
// Usage:
//
// ok := ReadAsBool("are you OK? [y/N]", false)
func ReadAsBool(tip string, defVal bool) bool {
fChar, err := ReadFirstByte(tip)
if err == nil && fChar != 0 {
return ByteIsYes(fChar)
}
return defVal
}
// Confirm with user input
func Confirm(tip string, defVal ...bool) bool {
var defV bool
mark := " [y/N]: "
if len(defVal) > 0 && defVal[0] {
defV = true
mark = " [Y/n]: "
}
return ReadAsBool(tip+mark, defV)
}
// InputIsYes answer: yes, y, Yes, Y
func InputIsYes(ans string) bool {
return len(ans) > 0 && (ans[0] == 'y' || ans[0] == 'Y')
}
// ByteIsYes answer: yes, y, Yes, Y
func ByteIsYes(ans byte) bool {
return ans == 'y' || ans == 'Y'
}
// ReadPassword from console terminal
func ReadPassword(question ...string) string {
if len(question) > 0 {
print(question[0])
} else {
print("Enter Password: ")
}
bs, err := term.ReadPassword(syscallStdinFd())
if err != nil {
return ""
}
println() // new line
return string(bs)
}