-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
132 lines (110 loc) · 3.02 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/spf13/cobra"
)
func emptyTableData() [][]string {
return [][]string{[]string{""}}
}
var (
ColorOrange ui.Color = 202
ColorPink ui.Color = 198
tabNames []string = []string{"Instances", "Pipelines", "Target groups", "Load balancers"}
)
func main() {
var rootCmd = &cobra.Command{
Use: "ser <region>",
Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) {
app("<deprecated>", args[0])
},
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func app(profile string, region string) {
if err := ui.Init(); err != nil {
log.Fatalf("Failed to initialize ser: %v", err)
}
defer ui.Close()
messagesCh := make(chan string)
codePipelinesCh := make(chan [][]string)
instancesCh := make(chan [][]string)
targetGroupsCh := make(chan [][]string)
loadBalancersCh := make(chan [][]string)
go awsPoolingLoop(profile, region, messagesCh, codePipelinesCh, instancesCh, targetGroupsCh, loadBalancersCh)
uiTables := make([]*widgets.Table, len(tabNames))
uiGrids := make([]*ui.Grid, len(tabNames))
termWidth, termHeight := ui.TerminalDimensions()
uiFooter := widgets.NewParagraph()
uiFooter.SetRect(0, termHeight-1, termWidth, termHeight)
uiFooter.Border = false
uiFooter.TextStyle.Fg = ColorPink
for i := 0; i < len(tabNames); i++ {
tab := widgets.NewTable()
tab.Border = true
tab.TextStyle = ui.NewStyle(ui.ColorWhite)
tab.RowSeparator = false
tab.FillRow = true
tab.Title = tabNames[i]
grid := ui.NewGrid()
grid.Border = false
grid.SetRect(0, 0, termWidth, termHeight-2)
grid.Set(
ui.NewRow(1.0, ui.NewCol(1.0, tab)),
)
uiGrids[i] = grid
uiTables[i] = tab
uiTables[i].Rows = emptyTableData()
}
for i := 0; i < len(tabNames); i++ {
uiTables[i].RowStyles[0] = ui.NewStyle(ui.ColorWhite, ColorOrange, ui.ModifierBold)
}
uiTabs := widgets.NewTabPane(tabNames...)
uiTabs.SetRect(0, 0, termWidth, termHeight-1)
uiTabs.Border = false
uiTabs.Block.Inner.Min.Y = termHeight - 2
uiTabs.ActiveTabStyle.Fg = ColorOrange
uiTabs.ActiveTabStyle.Modifier = ui.ModifierBold
uiRenderTab := func(i int) {
ui.Clear()
ui.Render(uiTabs, uiGrids[i], uiFooter)
}
uiEvents := ui.PollEvents()
for {
select {
case msg := <-messagesCh:
uiFooter.Text = msg
uiRenderTab(uiTabs.ActiveTabIndex)
case rows := <-instancesCh:
uiTables[0].Rows = rows
uiRenderTab(uiTabs.ActiveTabIndex)
case rows := <-codePipelinesCh:
uiTables[1].Rows = rows
uiRenderTab(uiTabs.ActiveTabIndex)
case rows := <-targetGroupsCh:
uiTables[2].Rows = rows
uiRenderTab(uiTabs.ActiveTabIndex)
case rows := <-loadBalancersCh:
uiTables[3].Rows = rows
uiRenderTab(uiTabs.ActiveTabIndex)
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "j", "<Left>":
uiTabs.FocusLeft()
uiRenderTab(uiTabs.ActiveTabIndex)
case ";", "<Right>":
uiTabs.FocusRight()
uiRenderTab(uiTabs.ActiveTabIndex)
}
}
}
}