-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
menu.go
203 lines (170 loc) · 4.49 KB
/
menu.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
package ui
import (
"fmt"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/tview"
runewidth "github.com/mattn/go-runewidth"
)
const (
menuIndexFmt = " [key:-:b]<%d> [fg:-:d]%s "
maxRows = 6
)
var menuRX = regexp.MustCompile(`\d`)
// Menu presents menu options.
type Menu struct {
*tview.Table
styles *config.Styles
}
// NewMenu returns a new menu.
func NewMenu(styles *config.Styles) *Menu {
m := Menu{
Table: tview.NewTable(),
styles: styles,
}
m.SetBackgroundColor(styles.BgColor())
styles.AddListener(&m)
return &m
}
// StylesChanged notifies skin changed.
func (m *Menu) StylesChanged(s *config.Styles) {
m.styles = s
m.SetBackgroundColor(s.BgColor())
}
// StackPushed notifies a component was added.
func (m *Menu) StackPushed(c model.Component) {
m.HydrateMenu(c.Hints())
}
// StackPopped notifies a component was removed.
func (m *Menu) StackPopped(o, top model.Component) {
if top != nil {
m.HydrateMenu(top.Hints())
} else {
m.Clear()
}
}
// StackTop notifies the top component.
func (m *Menu) StackTop(t model.Component) {
m.HydrateMenu(t.Hints())
}
// HydrateMenu populate menu ui from hints.
func (m *Menu) HydrateMenu(hh model.MenuHints) {
m.Clear()
sort.Sort(hh)
table := make([]model.MenuHints, maxRows+1)
colCount := (len(hh) / maxRows) + 1
if m.hasDigits(hh) {
colCount++
}
for row := 0; row < maxRows; row++ {
table[row] = make(model.MenuHints, colCount)
}
t := m.buildMenuTable(hh, table, colCount)
for row := 0; row < len(t); row++ {
for col := 0; col < len(t[row]); col++ {
c := tview.NewTableCell(t[row][col])
if len(t[row][col]) == 0 {
c = tview.NewTableCell("")
}
c.SetBackgroundColor(m.styles.BgColor())
m.SetCell(row, col, c)
}
}
}
func (m *Menu) hasDigits(hh model.MenuHints) bool {
for _, h := range hh {
if !h.Visible {
continue
}
if menuRX.MatchString(h.Mnemonic) {
return true
}
}
return false
}
func (m *Menu) buildMenuTable(hh model.MenuHints, table []model.MenuHints, colCount int) [][]string {
var row, col int
firstCmd := true
maxKeys := make([]int, colCount)
for _, h := range hh {
if !h.Visible {
continue
}
if !menuRX.MatchString(h.Mnemonic) && firstCmd {
row, col, firstCmd = 0, col+1, false
if table[0][0].IsBlank() {
col = 0
}
}
if maxKeys[col] < len(h.Mnemonic) {
maxKeys[col] = len(h.Mnemonic)
}
table[row][col] = h
row++
if row >= maxRows {
row, col = 0, col+1
}
}
out := make([][]string, len(table))
for r := range out {
out[r] = make([]string, len(table[r]))
}
m.layout(table, maxKeys, out)
return out
}
func (m *Menu) layout(table []model.MenuHints, mm []int, out [][]string) {
for r := range table {
for c := range table[r] {
out[r][c] = keyConv(m.formatMenu(table[r][c], mm[c]))
}
}
}
func (m *Menu) formatMenu(h model.MenuHint, size int) string {
if h.Mnemonic == "" || h.Description == "" {
return ""
}
i, err := strconv.Atoi(h.Mnemonic)
if err == nil {
return formatNSMenu(i, h.Description, m.styles.Frame())
}
return formatPlainMenu(h, size, m.styles.Frame())
}
// ----------------------------------------------------------------------------
// Helpers...
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)
}
// Truncate a string to the given l and suffix ellipsis if needed.
func Truncate(str string, width int) string {
return runewidth.Truncate(str, width, string(tview.SemigraphicsHorizontalEllipsis))
}
func toMnemonic(s string) string {
if len(s) == 0 {
return s
}
return "<" + keyConv(strings.ToLower(s)) + ">"
}
func formatNSMenu(i int, name string, styles config.Frame) string {
fmat := strings.Replace(menuIndexFmt, "[key", "["+styles.Menu.NumKeyColor.String(), 1)
fmat = strings.Replace(fmat, ":bg:", ":"+styles.Title.BgColor.String()+":", -1)
fmat = strings.Replace(fmat, "[fg", "["+styles.Menu.FgColor.String(), 1)
return fmt.Sprintf(fmat, i, name)
}
func formatPlainMenu(h model.MenuHint, size int, styles config.Frame) string {
menuFmt := " [key:-:b]%-" + strconv.Itoa(size+2) + "s [fg:-:d]%s "
fmat := strings.Replace(menuFmt, "[key", "["+styles.Menu.KeyColor.String(), 1)
fmat = strings.Replace(fmat, "[fg", "["+styles.Menu.FgColor.String(), 1)
fmat = strings.Replace(fmat, ":bg:", ":"+styles.Title.BgColor.String()+":", -1)
return fmt.Sprintf(fmat, toMnemonic(h.Mnemonic), h.Description)
}