fix(keys): spacebar now sends a `KeySpace` (#289)

* fix: replace keySP with working KeySpace

* test: update test to meet new reqs

* fix: fix test looking for 'space'

* fix(keys): set type to KeySpace when sending a space

* docs(keys): comments

Co-authored-by: Christian Rocha <christian@rocha.is>
This commit is contained in:
bashbunni 2022-04-12 08:14:35 -07:00 committed by GitHub
parent 3795c036c4
commit 14e58aa1f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

19
key.go
View File

@ -137,7 +137,6 @@ const (
keyGS KeyType = 29 // group separator keyGS KeyType = 29 // group separator
keyRS KeyType = 30 // record separator keyRS KeyType = 30 // record separator
keyUS KeyType = 31 // unit separator keyUS KeyType = 31 // unit separator
keySP KeyType = 32 // space
keyDEL KeyType = 127 // delete. on most systems this is mapped to backspace, I hear keyDEL KeyType = 127 // delete. on most systems this is mapped to backspace, I hear
) )
@ -148,7 +147,6 @@ const (
KeyEnter KeyType = keyCR KeyEnter KeyType = keyCR
KeyBackspace KeyType = keyDEL KeyBackspace KeyType = keyDEL
KeyTab KeyType = keyHT KeyTab KeyType = keyHT
KeySpace KeyType = keySP
KeyEsc KeyType = keyESC KeyEsc KeyType = keyESC
KeyEscape KeyType = keyESC KeyEscape KeyType = keyESC
@ -200,6 +198,7 @@ const (
KeyPgUp KeyPgUp
KeyPgDown KeyPgDown
KeyDelete KeyDelete
KeySpace
KeyF1 KeyF1
KeyF2 KeyF2
KeyF3 KeyF3
@ -222,8 +221,9 @@ const (
KeyF20 KeyF20
) )
// Mapping for control keys to friendly consts. // Mappings for control keys and other special keys to friendly consts.
var keyNames = map[KeyType]string{ var keyNames = map[KeyType]string{
// Control keys.
keyNUL: "ctrl+@", // also ctrl+` keyNUL: "ctrl+@", // also ctrl+`
keySOH: "ctrl+a", keySOH: "ctrl+a",
keySTX: "ctrl+b", keySTX: "ctrl+b",
@ -256,13 +256,14 @@ var keyNames = map[KeyType]string{
keyGS: "ctrl+]", keyGS: "ctrl+]",
keyRS: "ctrl+^", keyRS: "ctrl+^",
keyUS: "ctrl+_", keyUS: "ctrl+_",
keySP: "space",
keyDEL: "backspace", keyDEL: "backspace",
// Other keys.
KeyRunes: "runes", KeyRunes: "runes",
KeyUp: "up", KeyUp: "up",
KeyDown: "down", KeyDown: "down",
KeyRight: "right", KeyRight: "right",
KeySpace: " ", // for backwards compatibility
KeyLeft: "left", KeyLeft: "left",
KeyShiftTab: "shift+tab", KeyShiftTab: "shift+tab",
KeyHome: "home", KeyHome: "home",
@ -480,7 +481,15 @@ func readInputs(input io.Reader) ([]Msg, error) {
}, nil }, nil
} }
// Welp, it's just a regular, ol' single rune // If it's a space, override the type with KeySpace (but still include the
// rune).
if len(runes) == 1 && runes[0] == ' ' {
return []Msg{
KeyMsg(Key{Type: KeySpace, Runes: runes}),
}, nil
}
// Welp, it's just a regular, ol' single rune.
return []Msg{ return []Msg{
KeyMsg(Key{Type: KeyRunes, Runes: runes}), KeyMsg(Key{Type: KeyRunes, Runes: runes}),
}, nil }, nil

View File

@ -8,10 +8,10 @@ import (
func TestKeyString(t *testing.T) { func TestKeyString(t *testing.T) {
t.Run("alt+space", func(t *testing.T) { t.Run("alt+space", func(t *testing.T) {
if got := KeyMsg(Key{ if got := KeyMsg(Key{
Type: keySP, Type: KeySpace,
Alt: true, Alt: true,
}).String(); got != "alt+space" { }).String(); got != "alt+ " {
t.Fatalf(`expected a "alt+space", got %q`, got) t.Fatalf(`expected a "alt+ ", got %q`, got)
} }
}) })
@ -35,8 +35,8 @@ func TestKeyString(t *testing.T) {
func TestKeyTypeString(t *testing.T) { func TestKeyTypeString(t *testing.T) {
t.Run("space", func(t *testing.T) { t.Run("space", func(t *testing.T) {
if got := keySP.String(); got != "space" { if got := KeySpace.String(); got != " " {
t.Fatalf(`expected a "space", got %q`, got) t.Fatalf(`expected a " ", got %q`, got)
} }
}) })