-
Notifications
You must be signed in to change notification settings - Fork 2
/
prompt.go
99 lines (82 loc) · 1.93 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
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
package repl
import (
"fmt"
"os"
"strings"
"syscall"
"github.com/c-bata/go-prompt"
)
var emptyComplete = func(prompt.Document) []prompt.Suggest { return []prompt.Suggest{} }
func runPrompt(executor func(string), completer func(prompt.Document) []prompt.Suggest,
firstTime func(), length uint16) {
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(prefix),
prompt.OptionPrefixTextColor(prompt.LightGray),
prompt.OptionMaxSuggestion(length),
prompt.OptionShowCompletionAtStart(),
prompt.OptionAddKeyBind(
prompt.KeyBind{prompt.ControlC, func(*prompt.Buffer){
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}}),
)
firstTime()
p.Run()
}
// executes prompt waiting for an input with y or n
func yesOrNoQuestion(msg string) string {
var input string
for {
input = prompt.Input(prefix+msg,
emptyComplete,
prompt.OptionPrefixTextColor(prompt.LightGray))
if input == "y" || input == "n" {
break
}
fmt.Println(printPrefix, "invalid command.")
}
return input
}
func multipleChoice(names []string) string {
var input string
accounts := make(map[string]struct{})
for _, name := range names {
accounts[name] = struct{}{}
}
if len(accounts) == 0 {
return ""
}
for {
for ac := range accounts {
fmt.Println(printPrefix, ac)
}
input = prompt.Input(prefix,
emptyComplete,
prompt.OptionPrefixTextColor(prompt.LightGray))
if _, ok := accounts[input]; ok {
return input
}
s := strings.TrimSpace(input)
if s == "quit" || s == "exit" {
fmt.Println("Bye!")
os.Exit(0)
return ""
}
fmt.Println(printPrefix, "invalid command.")
}
}
// executes prompt waiting an input not blank
func inputNotBlank(msg string) string {
var input string
for {
input = prompt.Input(prefix+msg,
emptyComplete,
prompt.OptionPrefixTextColor(prompt.LightGray))
if strings.TrimSpace(input) != "" {
break
}
fmt.Println(printPrefix, "please enter a value.")
}
return input
}