change(keys): spacebar sends a `KeySpace` (#315)

* Revert "fix: update tests for space input"

This reverts commit bfb166822e.

* Revert "feat: obliterate type KeySpace"

This reverts commit d3fb1b707a.
This commit is contained in:
Christian Rocha 2022-05-13 17:24:16 +00:00 committed by GitHub
parent bfb166822e
commit d301ee0405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

10
key.go
View File

@ -198,6 +198,7 @@ const (
KeyPgUp KeyPgUp
KeyPgDown KeyPgDown
KeyDelete KeyDelete
KeySpace
KeyCtrlUp KeyCtrlUp
KeyCtrlDown KeyCtrlDown
KeyCtrlRight KeyCtrlRight
@ -274,6 +275,7 @@ var keyNames = map[KeyType]string{
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",
@ -552,6 +554,14 @@ func readInputs(input io.Reader) ([]Msg, error) {
}, nil }, nil
} }
// If it's a space, override the type with KeySpace (but still include the
// rune).
if runes[0] == ' ' {
return []Msg{
KeyMsg(Key{Type: KeySpace, Runes: runes}),
}, nil
}
// Welp, it's just a regular, ol' single rune. // 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}),

View File

@ -8,9 +8,8 @@ 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: KeyRunes, Type: KeySpace,
Runes: []rune{' '}, Alt: true,
Alt: true,
}).String(); got != "alt+ " { }).String(); got != "alt+ " {
t.Fatalf(`expected a "alt+ ", got %q`, got) t.Fatalf(`expected a "alt+ ", got %q`, got)
} }
@ -36,10 +35,7 @@ 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 := KeyMsg(Key{ if got := KeySpace.String(); got != " " {
Type: KeyRunes,
Runes: []rune{' '},
}).String(); got != " " {
t.Fatalf(`expected a " ", got %q`, got) t.Fatalf(`expected a " ", got %q`, got)
} }
}) })