-
Notifications
You must be signed in to change notification settings - Fork 278
/
dashboard.go
212 lines (175 loc) · 5.41 KB
/
dashboard.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package termui
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/buildpacks/pack/pkg/dist"
)
type Dashboard struct {
app app
buildpackInfo []dist.ModuleInfo
appTree *tview.TreeView
builderTree *tview.TreeView
planList *tview.List
logsView *tview.TextView
screen *tview.Flex
leftPane *tview.Flex
nodes map[string]*tview.TreeNode
logs string
}
func NewDashboard(app app, appName string, bldr buildr, runImageName string, buildpackInfo []dist.ModuleInfo, logs []string) *Dashboard {
d := &Dashboard{}
appTree, builderTree := initTrees(appName, bldr, runImageName)
planList, logsView := d.initDashboard(buildpackInfo)
imagesView := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(appTree, 0, 1, false).
AddItem(builderTree, 0, 1, true)
imagesView.
SetBorder(true).
SetTitleAlign(tview.AlignLeft).
SetTitle("| [::b]images[::-] |").
SetBackgroundColor(backgroundColor)
leftPane := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(imagesView, 11, 0, false).
AddItem(planList, 0, 1, true)
screen := tview.NewFlex().
SetDirection(tview.FlexColumn).
AddItem(leftPane, 0, 1, true).
AddItem(logsView, 0, 1, false)
d.app = app
d.buildpackInfo = buildpackInfo
d.appTree = appTree
d.builderTree = builderTree
d.planList = planList
d.leftPane = leftPane
d.logsView = logsView
d.screen = screen
for _, txt := range logs {
d.logs = d.logs + txt + "\n"
}
d.handleToggle()
d.setScreen()
return d
}
func (d *Dashboard) Handle(txt string) {
d.app.QueueUpdateDraw(func() {
d.logs = d.logs + txt + "\n"
d.logsView.SetText(tview.TranslateANSI(d.logs))
})
}
func (d *Dashboard) Stop() {
// no-op
// This method is a side effect of the ill-fitting 'page interface'
// Trying to create a cleaner interface between the main termui controller
// and child pages like this one is currently a work-in-progress
}
func (d *Dashboard) SetNodes(nodes map[string]*tview.TreeNode) {
d.nodes = nodes
// activate plan list buttons
d.planList.SetMainTextColor(tcell.ColorMediumTurquoise).
SetSelectedTextColor(tcell.ColorMediumTurquoise)
idx := d.planList.GetCurrentItem()
d.planList.Clear()
for _, buildpackInfo := range d.buildpackInfo {
bp := buildpackInfo
d.planList.AddItem(
bp.FullName(),
info(bp),
'✔',
func() {
NewDive(d.app, d.buildpackInfo, bp, d.nodes, func() {
d.setScreen()
})
},
)
}
d.planList.SetCurrentItem(idx)
d.app.Draw()
}
func (d *Dashboard) handleToggle() {
d.planList.SetDoneFunc(func() {
screen := tview.NewFlex().
SetDirection(tview.FlexColumn).
AddItem(d.leftPane, 0, 1, false).
AddItem(d.logsView, 0, 1, true)
d.app.SetRoot(screen, true)
})
d.logsView.SetDoneFunc(func(key tcell.Key) {
screen := tview.NewFlex().
SetDirection(tview.FlexColumn).
AddItem(d.leftPane, 0, 1, true).
AddItem(d.logsView, 0, 1, false)
d.app.SetRoot(screen, true)
})
}
func (d *Dashboard) setScreen() {
d.app.SetRoot(d.screen, true)
}
func (d *Dashboard) initDashboard(buildpackInfos []dist.ModuleInfo) (*tview.List, *tview.TextView) {
planList := tview.NewList()
planList.SetMainTextColor(tcell.ColorDarkGrey).
SetSelectedTextColor(tcell.ColorDarkGrey).
SetSelectedBackgroundColor(tcell.ColorDarkSlateGray).
SetSecondaryTextColor(tcell.ColorDimGray).
SetBorder(true).
SetBorderPadding(1, 1, 1, 1).
SetTitle("| [::b]plan[::-] |").
SetTitleAlign(tview.AlignLeft).
SetBackgroundColor(backgroundColor)
for _, buildpackInfo := range buildpackInfos {
bp := buildpackInfo
planList.AddItem(
bp.FullName(),
info(bp),
' ',
func() {},
)
}
logsView := tview.NewTextView()
logsView.SetDynamicColors(true).
SetTextAlign(tview.AlignLeft).
SetBorderPadding(1, 1, 3, 1).
SetTitleAlign(tview.AlignLeft).
SetBackgroundColor(backgroundColor)
return planList, logsView
}
func initTrees(appName string, bldr buildr, runImageName string) (*tview.TreeView, *tview.TreeView) {
var (
appImage = tview.NewTreeNode(fmt.Sprintf("app: [white::b]%s", appName)).SetColor(tcell.ColorDimGray)
appRunImage = tview.NewTreeNode(fmt.Sprintf(" run: [white::b]%s", runImageName)).SetColor(tcell.ColorDimGray)
builderImage = tview.NewTreeNode(fmt.Sprintf("builder: [white::b]%s", bldr.BaseImageName())).SetColor(tcell.ColorDimGray)
lifecycle = tview.NewTreeNode(fmt.Sprintf(" lifecycle: [white::b]%s", bldr.LifecycleDescriptor().Info.Version.String())).SetColor(tcell.ColorDimGray)
runImage = tview.NewTreeNode(fmt.Sprintf(" run: [white::b]%s", bldr.Stack().RunImage.Image)).SetColor(tcell.ColorDimGray)
buildpacks = tview.NewTreeNode(" [mediumturquoise::b]buildpacks")
)
appImage.AddChild(appRunImage)
builderImage.AddChild(lifecycle)
builderImage.AddChild(runImage)
builderImage.AddChild(buildpacks)
appTree := tview.NewTreeView()
appTree.
SetRoot(appImage).
SetGraphics(true).
SetGraphicsColor(tcell.ColorMediumTurquoise).
SetBorderPadding(1, 0, 4, 0).
SetBackgroundColor(backgroundColor)
builderTree := tview.NewTreeView()
builderTree.
SetRoot(builderImage).
SetGraphics(true).
SetGraphicsColor(tcell.ColorMediumTurquoise).
SetBorderPadding(0, 0, 4, 0).
SetBackgroundColor(backgroundColor)
return appTree, builderTree
}
func info(buildpackInfo dist.ModuleInfo) string {
if buildpackInfo.Description != "" {
return buildpackInfo.Description
}
if buildpackInfo.Homepage != "" {
return buildpackInfo.Homepage
}
return "-"
}