-
Notifications
You must be signed in to change notification settings - Fork 26
/
help.go
265 lines (236 loc) · 4.54 KB
/
help.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package view
import (
"context"
"fmt"
"runtime"
"sort"
"strings"
"github.com/derailed/tview"
"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloudlens/internal/model"
"github.com/one2nc/cloudlens/internal/ui"
)
const (
helpTitle = "Help"
helpTitleFmt = " [aqua::b]%s "
)
// HelpFunc processes menu hints.
type HelpFunc func() model.MenuHints
// Help presents a help viewer.
type Help struct {
*Table
hints HelpFunc
maxKey, maxDesc, maxRows int
}
// NewHelp returns a new help viewer.
func NewHelp(app *App) *Help {
return &Help{
Table: NewTable("help"),
hints: app.Content.Top().Hints,
}
}
// Init initializes the component.
func (h *Help) Init(ctx context.Context) error {
if err := h.Table.Init(ctx); err != nil {
return err
}
h.SetSelectable(false, false)
h.resetTitle()
h.SetBorder(true)
h.SetBorderPadding(0, 0, 1, 1)
h.bindKeys()
h.build()
return nil
}
func (h *Help) bindKeys() {
h.Actions().Delete(ui.KeySpace, tcell.KeyCtrlSpace, tcell.KeyCtrlS, ui.KeySlash)
h.Actions().Set(ui.KeyActions{
tcell.KeyEscape: ui.NewKeyAction("Back", h.app.PrevCmd, true),
})
}
func (h *Help) build() {
h.Clear()
sections := []string{"SERVICES", "GENERAL", "NAVIGATION"}
h.maxRows = len(h.showGeneral())
ff := []HelpFunc{
h.showServices,
h.hints,
h.showGeneral,
}
var col int
for i, section := range sections {
hh := ff[i]()
sort.Sort(hh)
h.addSection(col, section, hh)
col += 2
}
}
func (h *Help) showServices() model.MenuHints {
return model.MenuHints{
{
Mnemonic: "s3",
Description: "view s3",
},
{
Mnemonic: "ec2",
Description: "View Ec2",
},
{
Mnemonic: "ec2:i",
Description: "View EC2 snapshots",
},
{
Mnemonic: "ec2:s",
Description: "View EC2 images",
},
{
Mnemonic: "vpc",
Description: "View VPC",
},
{
Mnemonic: "subnet",
Description: "View Subnet",
},
{
Mnemonic: "iam:u",
Description: "View IAM User",
},
{
Mnemonic: "iam:r",
Description: "View IAM Role",
},
{
Mnemonic: "iam:g",
Description: "View IAM User group",
},
{
Mnemonic: "sg",
Description: "View Security Group",
},
{
Mnemonic: "ebs",
Description: "View EBS volumes",
},
{
Mnemonic: "sqs",
Description: "View sqs queues",
},
{
Mnemonic: "lambda",
Description: "View Lamda functions",
},
}
}
func (h *Help) showGeneral() model.MenuHints {
return model.MenuHints{
{
Mnemonic: "?",
Description: "Help",
},
{
Mnemonic: "esc",
Description: "Back/Clear",
},
{
Mnemonic: "tab",
Description: "toggle dropdown",
},
{
Mnemonic: "Ctrl-u",
Description: "Command Clear",
},
{
Mnemonic: "Ctrl-e",
Description: "Toggle Header",
},
{
Mnemonic: ":q",
Description: "Quit",
},
{
Mnemonic: "z",
Description: "Save csv",
},
{
Mnemonic: "g",
Description: "Goto Top",
},
{
Mnemonic: "Shift-g",
Description: "Goto Bottom",
},
{
Mnemonic: "Ctrl-b",
Description: "Page Up",
},
{
Mnemonic: "Ctrl-f",
Description: "Page Down",
},
{
Mnemonic: "h",
Description: "Left",
},
{
Mnemonic: "l",
Description: "Right",
},
{
Mnemonic: "k",
Description: "Up",
},
{
Mnemonic: "j",
Description: "Down",
},
}
}
func (h *Help) resetTitle() {
h.SetTitle(fmt.Sprintf(helpTitleFmt, helpTitle))
}
func (h *Help) addSpacer(c int) {
//logic to add space
}
func (h *Help) addSection(c int, title string, hh model.MenuHints) {
if len(hh) > h.maxRows {
h.maxRows = len(hh)
}
row := 0
h.SetCell(row, c, h.titleCell(title).SetTextColor(tcell.ColorSpringGreen))
h.addSpacer(c + 1)
row++
for _, hint := range hh {
col := c
h.SetCell(row, col, tview.NewTableCell(toMnemonic(hint.Mnemonic)).SetTextColor(tcell.ColorDodgerBlue))
col++
h.SetCell(row, col, tview.NewTableCell(hint.Description).SetTextColor(tcell.ColorPapayaWhip))
row++
}
if len(hh) >= h.maxRows {
return
}
}
// ----------------------------------------------------------------------------
// Helpers...
func toMnemonic(s string) string {
if len(s) == 0 {
return s
}
return "<" + keyConv(strings.ToLower(s)) + ">"
}
func keyConv(s string) string {
if !strings.Contains(s, "alt") {
return s
}
if runtime.GOOS != "darwin" {
return s
}
return strings.Replace(s, "alt", "opt", 1)
}
func (h *Help) titleCell(title string) *tview.TableCell {
c := tview.NewTableCell(title)
c.SetAttributes(tcell.AttrBold)
c.SetExpansion(1)
c.SetAlign(tview.AlignLeft)
return c
}