Debian Codegraph @

app/keyhint.go

raw
1 package app
2
3 import (
4 "sort"
5 "strings"
6
7 "git.sr.ht/~rjarry/aerc/config"
8 "git.sr.ht/~rjarry/aerc/lib/ui"
9 "go.rockorager.dev/vaxis"
10 )
11
12 const keyHintSep = " - "
13
14 type keyHintEntry struct {
15 key string
16 desc string
17 }
18
19 // KeyHint displays available keybindings that share a common prefix in
20 // a floating popover. It implements ui.DrawableInteractive.
21 type KeyHint struct {
22 entries []keyHintEntry
23 prefix []config.KeyStroke
24 maxKeyW int
25 maxDescW int
26 requiredW int
27 requiredH int
28 }
29
30 // NewKeyHint creates a KeyHint populated with bindings that match the
31 // given prefix keystrokes. Duplicate keys are de-duplicated. Entries
32 // are sorted alphabetically by key.
33 func NewKeyHint(bindings []*config.Binding, prefix []config.KeyStroke) *KeyHint {
34 seen := make(map[string]bool)
35 var entries []keyHintEntry
36 for _, b := range bindings {
37 if len(b.Input) <= len(prefix) {
38 continue
39 }
40 match := true
41 for i, s := range prefix {
42 if s != b.Input[i] {
43 match = false
44 break
45 }
46 }
47 if !match {
48 continue
49 }
50 remaining := config.FormatKeyStrokes(b.Input[len(prefix):])
51 if seen[remaining] {
52 continue
53 }
54 seen[remaining] = true
55 desc := b.Annotation
56 if desc == "" {
57 desc = formatCommand(b.Output)
58 }
59 entries = append(entries, keyHintEntry{key: remaining, desc: desc})
60 }
61 sort.Slice(entries, func(i, j int) bool {
62 return entries[i].key < entries[j].key
63 })
64 maxKeyW := 0
65 maxDescW := 0
66 for _, e := range entries {
67 if len(e.key) > maxKeyW {
68 maxKeyW = len(e.key)
69 }
70 if len(e.desc) > maxDescW {
71 maxDescW = len(e.desc)
72 }
73 }
74 if maxDescW > 40 {
75 maxDescW = 40
76 }
77 if maxDescW < 20 {
78 maxDescW = 20
79 }
80 sepW := len(keyHintSep)
81 contentW := maxKeyW + sepW + maxDescW
82 return &KeyHint{
83 entries: entries,
84 prefix: prefix,
85 maxKeyW: maxKeyW,
86 maxDescW: maxDescW,
87 requiredW: contentW + 1,
88 requiredH: len(entries),
89 }
90 }
91
92 func formatCommand(strokes []config.KeyStroke) string {
93 s := config.FormatKeyStrokes(strokes)
94 s = strings.TrimPrefix(s, ":")
95 s = strings.TrimSuffix(s, "<enter>")
96 s = strings.TrimSuffix(s, "<Enter>")
97 s = strings.TrimSpace(s)
98 return s
99 }
100
101 func (kh *KeyHint) Draw(ctx *ui.Context) {
102 if len(kh.entries) == 0 {
103 return
104 }
105
106 uiConf := SelectedAccountUiConfig()
107 defaultStyle := uiConf.GetStyle(config.STYLE_KEYHINT_DEFAULT)
108 keyStyle := uiConf.GetStyle(config.STYLE_KEYHINT_KEY)
109 descStyle := uiConf.GetStyle(config.STYLE_KEYHINT_DESC)
110
111 w := ctx.Width()
112 h := ctx.Height()
113
114 sepW := len(keyHintSep)
115 descWidth := w - 1 - kh.maxKeyW - sepW
116 if descWidth > kh.maxDescW {
117 descWidth = kh.maxDescW
118 }
119 if descWidth < 1 {
120 descWidth = 1
121 }
122
123 maxRows := len(kh.entries)
124 if maxRows > h {
125 maxRows = h
126 }
127
128 ctx.Fill(0, 0, w, maxRows, ' ', defaultStyle)
129
130 for i := 0; i < maxRows; i++ {
131 e := kh.entries[i]
132 ctx.Printf(1, i, keyStyle, "%s", e.key)
133 ctx.Printf(1+len(e.key), i, defaultStyle, keyHintSep)
134 desc := e.desc
135 if len(desc) > descWidth {
136 desc = desc[:descWidth]
137 }
138 ctx.Printf(1+len(e.key)+sepW, i, descStyle, "%s", desc)
139 }
140 }
141
142 func (kh *KeyHint) Invalidate() {
143 ui.Invalidate()
144 }
145
146 func (kh *KeyHint) Event(event vaxis.Event) bool {
147 return false
148 }
149
150 func (kh *KeyHint) Focus(f bool) {}
151
152 func (kh *KeyHint) HasEntries() bool {
153 return len(kh.entries) > 0
154 }
155
156 func (kh *KeyHint) Prefix() []config.KeyStroke {
157 return kh.prefix
158 }
159
160 // RequiredHeight returns the number of content rows needed.
161 func (kh *KeyHint) RequiredHeight() int {
162 return kh.requiredH
163 }
164
165 // RequiredWidth returns the number of content columns needed.
166 func (kh *KeyHint) RequiredWidth() int {
167 return kh.requiredW
168 }