Skip to content

Commit

Permalink
binds: fix format of special keys
Browse files Browse the repository at this point in the history
Currently some bindings with modifiers are displayed with obscure names.
E.g.:

	<C-e>	->	enq
	<C-q>	->	dc1
	<C-z>	->	em

These do not make much sense to the user. Instead display the actual
binding verbatim (including the angle brackets, if it is a special key
or if it requires modifiers to be pressed).

Update unit tests accordingly.

Fixes: cdc90af ("aerc: replace tcell keys with vaxis keys")
Signed-off-by: Robin Jarry <[email protected]>
Tested-by: Bence Ferdinandy <[email protected]>
  • Loading branch information
rjarry committed Jan 24, 2025
1 parent d2ff2c3 commit b24c796
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
45 changes: 14 additions & 31 deletions config/binds.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,25 +471,34 @@ func FormatKeyStrokes(keystrokes []KeyStroke) string {
var sb strings.Builder

for _, stroke := range keystrokes {
special := false
s := ""
for name, ks := range keyNames {
if (ks.Modifiers == stroke.Modifiers || ks.Modifiers == vaxis.ModifierMask(0)) && ks.Key == stroke.Key {
switch name {
case "cr":
s = "<enter>"
special = true
s = "enter"
case "space":
s = " "
case "semicolon":
s = ";"
default:
s = fmt.Sprintf("<%s>", name)
special = true
s = name
}
// remove any modifiers this named key comes
// with so we format properly
stroke.Modifiers &^= ks.Modifiers
break
}
}
if stroke.Modifiers != vaxis.ModifierMask(0) {
special = true
}
if special {
sb.WriteString("<")
}
if stroke.Modifiers&vaxis.ModCtrl > 0 {
sb.WriteString("c-")
}
Expand All @@ -503,6 +512,9 @@ func FormatKeyStrokes(keystrokes []KeyStroke) string {
s = string(stroke.Key)
}
sb.WriteString(s)
if special {
sb.WriteString(">")
}
}

// replace leading & trailing spaces with explicit <space> keystrokes
Expand Down Expand Up @@ -609,38 +621,9 @@ var keyNames = map[string]KeyStroke{
"f61": {vaxis.ModifierMask(0), vaxis.KeyF61},
"f62": {vaxis.ModifierMask(0), vaxis.KeyF62},
"f63": {vaxis.ModifierMask(0), vaxis.KeyF63},
"nul": {vaxis.ModCtrl, ' '},
"soh": {vaxis.ModCtrl, 'a'},
"stx": {vaxis.ModCtrl, 'b'},
"etx": {vaxis.ModCtrl, 'c'},
"eot": {vaxis.ModCtrl, 'd'},
"enq": {vaxis.ModCtrl, 'e'},
"ack": {vaxis.ModCtrl, 'f'},
"bel": {vaxis.ModCtrl, 'g'},
"bs": {vaxis.ModCtrl, 'h'},
"tab": {vaxis.ModifierMask(0), vaxis.KeyTab},
"lf": {vaxis.ModCtrl, 'j'},
"vt": {vaxis.ModCtrl, 'k'},
"ff": {vaxis.ModCtrl, 'l'},
"cr": {vaxis.ModifierMask(0), vaxis.KeyEnter},
"so": {vaxis.ModCtrl, 'n'},
"si": {vaxis.ModCtrl, 'o'},
"dle": {vaxis.ModCtrl, 'p'},
"dc1": {vaxis.ModCtrl, 'q'},
"dc2": {vaxis.ModCtrl, 'r'},
"dc3": {vaxis.ModCtrl, 's'},
"dc4": {vaxis.ModCtrl, 't'},
"nak": {vaxis.ModCtrl, 'u'},
"syn": {vaxis.ModCtrl, 'v'},
"etb": {vaxis.ModCtrl, 'w'},
"can": {vaxis.ModCtrl, 'x'},
"em": {vaxis.ModCtrl, 'y'},
"sub": {vaxis.ModCtrl, 'z'},
"esc": {vaxis.ModifierMask(0), vaxis.KeyEsc},
"fs": {vaxis.ModCtrl, '\\'},
"gs": {vaxis.ModCtrl, ']'},
"rs": {vaxis.ModCtrl, '^'},
"us": {vaxis.ModCtrl, '_'},
"del": {vaxis.ModifierMask(0), vaxis.KeyDelete},
}

Expand Down
4 changes: 3 additions & 1 deletion config/binds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func TestKeyStrokeFormatting(t *testing.T) {
formatted string
}{
{KeyStroke{vaxis.ModifierMask(0), vaxis.KeyLeft}, "<left>"},
{KeyStroke{vaxis.ModCtrl, vaxis.KeyLeft}, "c-<left>"},
{KeyStroke{vaxis.ModCtrl, vaxis.KeyLeft}, "<c-left>"},
{KeyStroke{vaxis.ModCtrl, 'e'}, "<c-e>"},
{KeyStroke{vaxis.ModifierMask(0), vaxis.KeySpace}, "<space>"},
}

for _, test := range tests {
Expand Down

0 comments on commit b24c796

Please sign in to comment.