forked from CanopyTax/ckube
/
prompt.go
68 lines (57 loc) · 1.53 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
package util
import (
"fmt"
"strings"
"github.com/manifoldco/promptui"
)
func CreatePodInfos(pods []string) []PodInfo {
var podInfos []PodInfo
for _, p := range pods[1:] {
if p != "" {
var parts []string
splits := strings.Split(p, " ")
for _, s := range splits {
if s != "" {
parts = append(parts, s)
}
}
podInfos = append(podInfos, PodInfo{Name: parts[0], Ready: parts[1], Status: parts[2], Restarts: parts[3], Age: parts[4]})
}
}
return podInfos
}
type PodInfo struct {
Name string
Ready string
Status string
Restarts string
Age string
}
func (p PodInfo) Print() string {
return fmt.Sprintf("%v %v %v %v %v", p.Name, p.Ready, p.Status, p.Restarts, p.Age)
}
func GetPodPrompt(podInfos []PodInfo, helpMessage string) promptui.Select {
oMan := &OutputManager{HeaderColumns: []string{"NAME", "READY", "STATUS", "RESTARTS", "AGE"}}
for _, pInfo := range podInfos {
oMan.Append(pInfo.Print())
}
formattedOutput := oMan.FormattedStringSlice()
templates := &promptui.SelectTemplates{
Active: "{{ . | yellow | underline }}",
Inactive: "{{ . }}",
Help: helpMessage,
}
searcher := func(input string, index int) bool {
text := strings.Replace(strings.ToLower(formattedOutput[1:][index]), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(text, input)
}
prompt := promptui.Select{
Label: formattedOutput[0],
Items: formattedOutput[1:],
Size: 20,
Templates: templates,
Searcher: searcher,
}
return prompt
}