diff options
author | Robin Jarry <robin@jarry.cc> | 2022-02-02 20:47:54 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-02-02 22:12:08 +0100 |
commit | 77b3a141a4459a1e59583f0d9fee46e871466d74 (patch) | |
tree | 0111b42b5d5b6e3661445031f8adcc2de624ae17 /config | |
parent | 923e949c058289a70a98adaf881948bcb59dcc7e (diff) | |
download | aerc-77b3a141a4459a1e59583f0d9fee46e871466d74.tar.gz |
review: display actual bindings for commands
Parse the actual user bindings to determine what shortcuts are available
in the compose::review stage. Add a predefined list of commands for
which we want to display the keyboard shortcuts.
Fixes: https://todo.sr.ht/~rjarry/aerc/14
Signed-off-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'config')
-rw-r--r-- | config/bindings.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/config/bindings.go b/config/bindings.go index b704fa63..7ff0c24b 100644 --- a/config/bindings.go +++ b/config/bindings.go @@ -118,6 +118,53 @@ func (bindings *KeyBindings) GetBinding( return BINDING_NOT_FOUND, nil } +func (bindings *KeyBindings) GetReverseBindings(output []KeyStroke) [][]KeyStroke { + var inputs [][]KeyStroke + + for _, binding := range bindings.bindings { + if len(binding.Output) != len(output) { + continue + } + for i, stroke := range output { + if stroke.Modifiers != binding.Output[i].Modifiers { + goto next + } + if stroke.Key != binding.Output[i].Key { + goto next + } + if stroke.Key == tcell.KeyRune && stroke.Rune != binding.Output[i].Rune { + goto next + } + } + inputs = append(inputs, binding.Input) + next: + } + return inputs +} + +func FormatKeyStrokes(keystrokes []KeyStroke) string { + var sb strings.Builder + + for _, stroke := range keystrokes { + s := "" + for name, ks := range keyNames { + if ks.Modifiers == stroke.Modifiers && ks.Key == stroke.Key && ks.Rune == stroke.Rune { + if name == "cr" { + name = "enter" + } + s = fmt.Sprintf("<%s>", name) + break + } + } + if s == "" && stroke.Key == tcell.KeyRune { + s = string(stroke.Rune) + } + sb.WriteString(s) + } + + return sb.String() +} + var ( keyNames map[string]KeyStroke ) |