-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttuy_test.go
340 lines (304 loc) · 6.76 KB
/
ttuy_test.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2023 Brandon Jordan
*/
package ttuy
import (
"fmt"
"math/rand"
"os"
"strings"
"testing"
"time"
"github.com/eiannone/keyboard"
)
func TestProgressBar(t *testing.T) {
for i := 0; i <= 100; i++ {
ProgressBar(i)
time.Sleep(100 * time.Millisecond)
}
}
func TestAsk(t *testing.T) {
input := []byte("Brandon")
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_, err = w.Write(input)
if err != nil {
t.Error(err)
}
err = w.Close()
if err != nil {
panic(err)
}
// Restore stdin right after the test.
defer func(v *os.File) { os.Stdin = v }(os.Stdin)
os.Stdin = r
var received string
Ask("Enter your name", &received)
fmt.Println("You entered:", received)
}
func TestYesNo(t *testing.T) {
input := []byte("y")
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_, err = w.Write(input)
if err != nil {
t.Error(err)
}
err = w.Close()
if err != nil {
panic(err)
}
// Restore stdin right after the test.
defer func(v *os.File) { os.Stdin = v }(os.Stdin)
os.Stdin = r
YesNo("Are you sure?")
fmt.Println("You entered:", string(input))
}
func TestAskSecret(t *testing.T) {
input := []byte("Password123")
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_, err = w.Write(input)
if err != nil {
t.Error(err)
}
err = w.Close()
if err != nil {
panic(err)
}
// Restore stdin right after the test.
defer func(v *os.File) { os.Stdin = v }(os.Stdin)
os.Stdin = r
var received string
AskSecret("Password", &received)
fmt.Println("You entered:", received)
}
func TestTypewriter(t *testing.T) {
Typewriter("Typed out one character at a time")
}
func TestTypewriterTimed(t *testing.T) {
TypewriterTimed(":^)", 1000)
}
func TestViewport(t *testing.T) {
var bytes, err = os.ReadFile("LICENSE")
if err != nil {
panic(err)
}
Viewport(string(bytes))
}
func TestTable(t *testing.T) {
var headers = []string{"Header 1", "Header 2", "Header 3", "Header 4"}
var rows []Row
for i := 0; i < 5; i++ {
var rowRows = []string{"Cell 1", "Cell 2", "Cell 3", "Cell 4"}
rows = append(rows, Row{Columns: rowRows})
}
Table(headers, rows)
}
func TestGrid(t *testing.T) {
var alphabet = strings.Split("abcdefghijklmnopqrstuvwxyz", "")
var rows []Row
for i := 0; i < 10; i++ {
var rowRows []string
for c := 1; c < 5; c++ {
var cellContent string
for w := 0; w < 26; w++ {
cellContent += alphabet[w]
}
rowRows = append(rowRows, cellContent)
}
rows = append(rows, Row{Columns: rowRows})
}
fmt.Println(Grid(rows))
}
func TestStyle(t *testing.T) {
fmt.Println(Foreground("Text", 176))
fmt.Println(Background("Text", 127))
fmt.Println(Style("Text", Bold))
fmt.Println(Style("Text", Italic))
fmt.Println(Style("Text", Underlined))
fmt.Println(Style("Text", Dim))
fmt.Println(Style("Text", Blink))
fmt.Println(Style("Text", Inverted))
fmt.Println(Style("Text", Hidden))
fmt.Println(Style("Text", Crossed))
fmt.Println(Style("Text", Bold, RedText))
fmt.Println(Style("Text", Bold, YellowText))
fmt.Println(Style("Text", Bold, GreenText))
fmt.Println(Style("Text", Bold, CyanText))
fmt.Println(Style("Text", Bold, BlueText))
fmt.Println(Style("Text", Bold, MagentaText))
fmt.Println(Style("Text", BlackText, Bold, RedBg))
fmt.Println(Style("Text", BlackText, Bold, YellowBg))
fmt.Println(Style("Text", BlackText, Bold, GreenBg))
fmt.Println(Style("Text", BlackText, Bold, CyanBg))
fmt.Println(Style("Text", BlackText, Bold, BlueBg))
fmt.Println(Style("Text", BlackText, Bold, MagentaBg))
}
func TestStylePersist(t *testing.T) {
fmt.Print(StylePersist(YellowText, GrayBg))
fmt.Println("This should be in the set style." + Reset)
fmt.Println("This should be reset.")
}
func TestSpinner(t *testing.T) {
go Spinner("Indeterminate Progress", Ticker)
for i := 0; i < 50; i++ {
time.Sleep(30 * time.Millisecond)
}
StopSpinner()
go Spinner("Loading", DotDotDot)
for i := 0; i < 50; i++ {
time.Sleep(30 * time.Millisecond)
}
StopSpinner()
go Spinner("Getting Ready", Throbber)
for i := 0; i < 50; i++ {
time.Sleep(30 * time.Millisecond)
}
StopSpinner()
go Spinner("Installing", Blinker)
for i := 0; i < 50; i++ {
time.Sleep(30 * time.Millisecond)
}
StopSpinner()
}
func TestPrompt(*testing.T) {
if Prompt("Install Program? This will take up 586kb.") {
fmt.Println("Installing...")
} else {
fmt.Println("Installation Canceled.")
}
}
func TestOutput(t *testing.T) {
// Success
Success("Done!")
var success = "Done!"
Successf("%s", success)
// Info
Info("FYI!")
var info = "FYI!"
Infof("%s", info)
// Warning
Warn("Unable to do something!")
var warning = "Unable to do something!"
Warnf("%s", warning)
// Error
// Fail("Unable to do something!")
// var _, err = os.ReadDir("doesntExist")
// FailErr("Label", err)
// var message = "Unable to do something!"
// Failf("%s", message)
}
func TestMenu(*testing.T) {
var chosen string
Menu("Title", []Option{
{
Label: "Option 1",
Callback: func() {
chosen = "You have chosen option 1."
},
},
{
Label: "Option 2",
Callback: func() {
chosen = "You chose option 2."
},
Disabled: true,
},
{
Label: "Option 3",
Callback: func() {
chosen = "You chose option 3."
},
},
})
fmt.Println(chosen)
Menu("Title", []Option{
{
Label: "Option 1",
Callback: func() {
chosen = "You chose option 1."
},
},
{
Label: "Option 2",
Callback: func() {
chosen = "You chose option 2."
},
},
{
Label: "Option 3",
Callback: func() {
chosen = "You chose option 3."
},
},
})
fmt.Println(chosen)
}
func TestPainter(*testing.T) {
var lastText string
go ReadKeys(func(key keyboard.Key) {
if key == keyboard.KeyCtrlC {
StopPainting()
}
})
Painter(func() (template string) {
var text string
switch rand.Intn(4) {
case 1:
text = "Painter Test - Press ^C to exit"
case 2:
text = "Painter Test\nPainter Test\nPress ^C to exit"
case 3:
text = "Painter Test\nPress ^C to exit"
}
if text != lastText {
template = text
lastText = template
} else {
template = lastText
}
return
})
}
func TestSegmented(t *testing.T) {
fmt.Print(Segmented(fmt.Sprintf("%d:%d:%d", rand.Intn(100), rand.Intn(100), rand.Intn(100))))
}
func TestBanner(t *testing.T) {
fmt.Print(BigBanner("TESTING"))
fmt.Print(Banner("TESTING"))
}
func TestKeybind(_ *testing.T) {
BindKey(keyboard.KeyCtrlC, KeyBinding{
descriptor: "Exit",
keyString: "^C",
handler: func() {
StopPainting()
fmt.Print("Exited.\n\n")
},
})
var highlight = false
BindKey(keyboard.KeyCtrlQ, KeyBinding{
descriptor: "Toggle highlight",
keyString: "^Q",
handler: func() {
highlight = !highlight
},
})
Painter(func() (template string) {
if highlight {
template += Style("Text", YellowText)
} else {
template += "Text"
}
template += "\n"
template += Hotkeys(" • ")
return
})
}