-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
executable file
·62 lines (54 loc) · 1.23 KB
/
prompt.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
/*
* Copyright (c) 2023 Brandon Jordan
*/
package ttuy
import (
"fmt"
"github.com/eiannone/keyboard"
"os"
"strings"
)
var proceed bool
var lastProceed = true
var lastPrompt string
// Prompt prints a prompt for the user to choose yes or no using the arrow keys then returns a boolean based on the option chosen
func Prompt(prompt string) bool {
fmt.Print(eol)
go ReadKeys(handlePromptKeys)
Painter(func() (template string) {
if proceed == lastProceed {
template = lastPrompt
return
}
template = Style(prompt, Bold, GreenText) + eol
if proceed {
template += Style(" Yes ", Inverted, Bold)
} else {
template += Style(" Yes ", Bold)
}
template += "\t"
if !proceed {
template += Style(" No ", Inverted, Bold)
} else {
template += Style(" No ", Bold)
}
template += strings.Repeat(eol, 2) + Style(leftArrow+" "+rightArrow+" Use Left & Right Arrow Keys to Choose.", Dim, Italic)
lastProceed = proceed
lastPrompt = template
return
})
fmt.Print(eol)
return proceed
}
func handlePromptKeys(key keyboard.Key) {
switch key {
case keyboard.KeyArrowLeft, keyboard.KeyArrowRight:
proceed = !proceed
case keyboard.KeyCtrlC:
CursorShow()
os.Exit(0)
case keyboard.KeyEnter:
StopPainting()
return
}
}