/
stdin.go
62 lines (53 loc) · 1.07 KB
/
stdin.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
package stdin
import (
"io"
"strings"
"github.com/chzyer/readline"
)
func Input() string {
lines := []string{}
l, err := readline.NewEx(&readline.Config{
Prompt: ">> ",
HistoryFile: "/tmp/readline.tmp",
AutoComplete: autoCompleter(),
InterruptPrompt: "^C",
EOFPrompt: "exit",
HistorySearchFold: true,
FuncFilterInputRune: filterInput,
})
if err != nil {
panic(err)
}
defer l.Close()
for {
ln, err := l.Readline()
if err == readline.ErrInterrupt {
if len(ln) == 0 {
break
} else {
continue
}
} else if err == io.EOF {
break
}
lines = append(lines, strings.TrimSpace(ln))
}
return strings.Join(lines, " ")
}
func autoCompleter() *readline.PrefixCompleter {
return readline.NewPrefixCompleter(
readline.PcItem("apiVersion"),
readline.PcItem("kind"),
readline.PcItem("metadata"),
readline.PcItem("spec"),
readline.PcItem("labels"),
)
}
func filterInput(r rune) (rune, bool) {
switch r {
// block CtrlZ feature
case readline.CharCtrlZ:
return r, false
}
return r, true
}