-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
158 lines (133 loc) · 3.76 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
package component
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/kopecmaciej/mongui/config"
"github.com/kopecmaciej/mongui/manager"
"github.com/kopecmaciej/tview"
)
const (
HelpComponent manager.Component = "Help"
)
// Help is a component that provides a help screen for keybindings
type Help struct {
*Component
*tview.Table
style *config.HelpStyle
}
// NewHelp creates a new Help component
func NewHelp() *Help {
h := &Help{
Component: NewComponent(HelpComponent),
Table: tview.NewTable(),
}
h.SetAfterInitFunc(h.init)
return h
}
func (h *Help) init() error {
h.setStyle()
h.setKeybindings()
return nil
}
func (h *Help) Render() error {
h.Table.Clear()
_, _, width, height := h.GetRect()
currectComponent := h.app.Manager.CurrentComponent()
cKeys, err := h.app.Keys.GetKeysForComponent(string(currectComponent))
if err != nil {
ShowErrorModal(h.app.Root, "Error while getting keys for component", err)
return err
}
h.fillWithEmptySpace(width, height)
pos, col := 0, 0
for _, keys := range cKeys {
if len(keys.Keys) > 0 {
if h.shouldMoveToNextColumn(height, pos+3+len(keys.Keys)) {
pos = 0
col += 3
}
h.addHeaderSection(keys.Component, pos, col)
pos += 2
h.AddKeySection(keys.Component, keys.Keys, &pos, col)
}
}
gKeys, err := h.app.Keys.GetKeysForComponent("Global")
for _, keys := range gKeys {
if h.shouldMoveToNextColumn(height, pos+3+len(keys.Keys)) {
pos = 0
col += 3
}
h.addHeaderSection(keys.Component, pos, col)
pos += 2
h.AddKeySection(keys.Component, keys.Keys, &pos, col)
}
hKeys, err := h.app.Keys.GetKeysForComponent("Help")
for _, keys := range hKeys {
if h.shouldMoveToNextColumn(height, pos+3+len(keys.Keys)) {
pos = 0
col += 3
}
h.addHeaderSection(keys.Component, pos, col)
pos += 2
h.AddKeySection(keys.Component, keys.Keys, &pos, col)
}
return nil
}
func (h *Help) shouldMoveToNextColumn(maxNumberOfRows int, currRows int) bool {
return currRows >= maxNumberOfRows
}
func (h *Help) addHeaderSection(name string, row, col int) {
h.Table.SetCell(row+0, col, tview.NewTableCell(name).SetTextColor(h.style.TitleColor.Color()))
h.Table.SetCell(row+1, col, tview.NewTableCell("-------").SetTextColor(h.style.DescriptionColor.Color()))
}
func (h *Help) fillWithEmptySpace(width, height int) {
for i := 0; i < height; i++ {
for j := 0; j < width; j++ {
h.Table.SetCell(i, j, tview.NewTableCell(" ").SetTextColor(h.style.BackgroundColor.Color()))
}
}
}
func (h *Help) AddKeySection(name string, keys []config.Key, pos *int, col int) {
for _, key := range keys {
var keyString string
var iter []string
if len(key.Keys) > 0 {
iter = key.Keys
} else {
iter = key.Runes
}
for i, k := range iter {
if i == 0 {
keyString = fmt.Sprintf("%s", k)
} else {
keyString = fmt.Sprintf("%s, %s", keyString, k)
}
}
h.Table.SetCell(*pos, col, tview.NewTableCell(keyString).SetTextColor(h.style.KeyColor.Color()))
h.Table.SetCell(*pos, col+1, tview.NewTableCell(" - ").SetTextColor(h.style.DescriptionColor.Color()))
h.Table.SetCell(*pos, col+2, tview.NewTableCell(key.Description).SetTextColor(h.style.DescriptionColor.Color()))
*pos += 1
}
}
func (h *Help) setStyle() {
h.style = &h.app.Styles.Help
h.SetBorder(true)
h.SetTitle(" Help ")
h.SetTitleAlign(tview.AlignLeft)
h.SetBorderPadding(0, 0, 1, 1)
h.SetSelectable(false, false)
h.SetBackgroundColor(h.style.BackgroundColor.Color())
h.SetBorderColor(h.style.BorderColor.Color())
}
// setKeybindings sets a key binding for the help Component
func (h *Help) setKeybindings() {
k := h.app.Keys
h.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch {
case k.Contains(k.Help.Close, event.Name()):
h.app.Root.RemovePage(HelpComponent)
return nil
}
return event
})
}