-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.go
155 lines (139 loc) · 3.63 KB
/
Console.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
package wiz
import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/howeyc/gopass" //TODO: dev seems to recommend to switching to package terminal
"os"
)
// Colorful console input and output. Also does passwords.
// * * * * * * * * * * * * * * * *
// * * * * * * * * * * * * * * * *
// Exposed functions:
// SilentPrompt(prompt string) string
// Prompt(prompt string) string
// Prompt console for input - silent hides it (like a password)
// Red(items ...interface{})
// Yellow(items ...interface{})
// Green(items ...interface{})
// Blue(items ...interface{})
// Purple(items ...interface{})
// White(items ...interface{})
// All of these print in a similar (bold first arg, newline each after)
// Print(items ...interface{})
// Does not mess set or unset color at all - just prints.
// * * * * * * * * * * * * * * * *
// * * * * * * * * * * * * * * * *
// Displays a prompt to the interface and returns what the user enters.
// Hides user input from the console. Use for password entry for example.
func SilentPrompt(prompt string) string {
defer color.Unset()
color.Set(color.FgWhite, color.Bold)
fmt.Println(prompt)
silentPassword, err := gopass.GetPasswd() // Silent
if err != nil {
panic(err)
}
return string(silentPassword)
}
// Displays a prompt to the interface and returns what the user enters
func Prompt(prompt string) string {
defer color.Unset()
color.Set(color.FgWhite, color.Bold)
scanner := bufio.NewScanner(os.Stdin)
fmt.Println(prompt)
scanner.Scan()
text := scanner.Text()
return text
}
// Prints blue. The first item passed is bolded. Each item gets a new line.
func Blue(items ...interface{}) {
defer color.Unset()
color.Set(color.FgBlue, color.Bold)
first := true
for _, element := range items {
fmt.Println(element)
if first {
color.Unset()
color.Set(color.FgBlue)
first = false
}
}
}
// Prints red. The first item passed is bolded. Each item gets a new line.
func Red(items ...interface{}) {
defer color.Unset()
color.Set(color.FgRed, color.Bold)
first := true
for _, element := range items {
fmt.Println(element)
if first {
color.Unset()
color.Set(color.FgRed)
first = false
}
}
}
// Prints green. The first item passed is bolded. Each item gets a new line.
func Green(items ...interface{}) {
defer color.Unset()
color.Set(color.FgGreen, color.Bold)
first := true
for _, element := range items {
fmt.Println(element)
if first {
color.Unset()
color.Set(color.FgGreen)
first = false
}
}
}
// Prints yellow. The first item passed is bolded. Each item gets a new line.
func Yellow(items ...interface{}) {
defer color.Unset()
color.Set(color.FgYellow, color.Bold)
first := true
for _, element := range items {
fmt.Println(element)
if first {
color.Unset()
color.Set(color.FgYellow)
first = false
}
}
}
// Prints magenta. The first item passed is bolded. Each item gets a new line.
func Purple(items ...interface{}) {
defer color.Unset()
color.Set(color.FgMagenta, color.Bold)
first := true
for _, element := range items {
fmt.Println(element)
if first {
color.Unset()
color.Set(color.FgMagenta)
first = false
}
}
}
// Prints white. The first item passed is bolded. Each item gets a new line.
func White(items ...interface{}) {
defer color.Unset()
color.Set(color.FgWhite, color.Bold)
first := true
for _, element := range items {
fmt.Println(element)
if first {
color.Unset()
color.Set(color.FgWhite)
first = false
}
}
}
// Prints normally - does not set or unset any color parameters.
func Print(items ...interface{}) {
for _, element := range items {
fmt.Print(element, " ")
}
fmt.Print("\n")
}