-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcuts.go
68 lines (58 loc) · 1.51 KB
/
shortcuts.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
package tui
import "github.com/charmbracelet/lipgloss"
const (
ShortCutKeyColor = "#47A4AC"
ShortCutDescColor = "#BAEBDA"
)
var ShortcutKeyStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color(ShortCutKeyColor))
var ShortcutDescStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color(ShortCutDescColor))
type Shortcut struct {
Key string
Desc string
KeyStyle lipgloss.Style
DescStyle lipgloss.Style
}
func NewShortcut(key, desc string) Shortcut {
return Shortcut{
Key: key,
Desc: desc,
KeyStyle: ShortcutKeyStyle,
DescStyle: ShortcutDescStyle,
}
}
func (s Shortcut) KeyLength() int {
return len(s.Key) + 2 // 2 brackets
}
func (s Shortcut) DescLength() int {
return len(s.Desc)
}
func (s Shortcut) Length() int {
return s.KeyLength() + s.DescLength() + 1 // 1 space
}
func (s Shortcut) String() string {
return s.KeyStyle.Render("<"+s.Key+">") + " " + s.DescStyle.Render(s.Desc)
}
func ShortcutRow(shortcuts []Shortcut) string {
var result string
var maxKeyLength int
for _, shortcut := range shortcuts {
if shortcut.KeyLength() > maxKeyLength {
maxKeyLength = shortcut.KeyLength()
}
}
n := len(shortcuts)
for i, shortcut := range shortcuts {
keyLen := shortcut.KeyLength()
keyStr := shortcut.KeyStyle.Render("<" + shortcut.Key + ">")
for j := 0; j < maxKeyLength-keyLen; j++ {
keyStr += " "
}
result += keyStr + " " + shortcut.DescStyle.Render(shortcut.Desc)
if i < n-1 {
result += "\n"
}
}
return lipgloss.NewStyle().PaddingLeft(1).Render(result)
}