Skip to content

Commit

Permalink
fix(keys): spacebar now sends a KeySpace (#289)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
bashbunni and meowgorithm committed Apr 12, 2022
1 parent 3795c03 commit 14e58aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
19 changes: 14 additions & 5 deletions key.go
Expand Up @@ -137,7 +137,6 @@ const (
keyGS KeyType = 29 // group separator
keyRS KeyType = 30 // record separator
keyUS KeyType = 31 // unit separator
keySP KeyType = 32 // space
keyDEL KeyType = 127 // delete. on most systems this is mapped to backspace, I hear
)

Expand All @@ -148,7 +147,6 @@ const (
KeyEnter KeyType = keyCR
KeyBackspace KeyType = keyDEL
KeyTab KeyType = keyHT
KeySpace KeyType = keySP
KeyEsc KeyType = keyESC
KeyEscape KeyType = keyESC

Expand Down Expand Up @@ -200,6 +198,7 @@ const (
KeyPgUp
KeyPgDown
KeyDelete
KeySpace
KeyF1
KeyF2
KeyF3
Expand All @@ -222,8 +221,9 @@ const (
KeyF20
)

// Mapping for control keys to friendly consts.
// Mappings for control keys and other special keys to friendly consts.
var keyNames = map[KeyType]string{
// Control keys.
keyNUL: "ctrl+@", // also ctrl+`
keySOH: "ctrl+a",
keySTX: "ctrl+b",
Expand Down Expand Up @@ -256,13 +256,14 @@ var keyNames = map[KeyType]string{
keyGS: "ctrl+]",
keyRS: "ctrl+^",
keyUS: "ctrl+_",
keySP: "space",
keyDEL: "backspace",

// Other keys.
KeyRunes: "runes",
KeyUp: "up",
KeyDown: "down",
KeyRight: "right",
KeySpace: " ", // for backwards compatibility
KeyLeft: "left",
KeyShiftTab: "shift+tab",
KeyHome: "home",
Expand Down Expand Up @@ -480,7 +481,15 @@ func readInputs(input io.Reader) ([]Msg, error) {
}, 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{
KeyMsg(Key{Type: KeyRunes, Runes: runes}),
}, nil
Expand Down
10 changes: 5 additions & 5 deletions key_test.go
Expand Up @@ -8,10 +8,10 @@ import (
func TestKeyString(t *testing.T) {
t.Run("alt+space", func(t *testing.T) {
if got := KeyMsg(Key{
Type: keySP,
Type: KeySpace,
Alt: true,
}).String(); got != "alt+space" {
t.Fatalf(`expected a "alt+space", got %q`, got)
}).String(); got != "alt+ " {
t.Fatalf(`expected a "alt+ ", got %q`, got)
}
})

Expand All @@ -35,8 +35,8 @@ func TestKeyString(t *testing.T) {

func TestKeyTypeString(t *testing.T) {
t.Run("space", func(t *testing.T) {
if got := keySP.String(); got != "space" {
t.Fatalf(`expected a "space", got %q`, got)
if got := KeySpace.String(); got != " " {
t.Fatalf(`expected a " ", got %q`, got)
}
})

Expand Down

0 comments on commit 14e58aa

Please sign in to comment.