Skip to content

Commit

Permalink
fix: remove the now-unused hexes key mapping
Browse files Browse the repository at this point in the history
This became unnecessary when we fixed the support for the Alt modifier
on control characters.
  • Loading branch information
knz authored and muesli committed Oct 3, 2022
1 parent b074f6f commit 7e7a729
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
24 changes: 5 additions & 19 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tea

import (
"errors"
"fmt"
"io"
"unicode/utf8"

Expand Down Expand Up @@ -540,18 +539,12 @@ var sequences = map[string]Key{
"\x1b\x1b[32~": {Type: KeyF18, Alt: true}, // urxvt
"\x1b\x1b[33~": {Type: KeyF19, Alt: true}, // urxvt
"\x1b\x1b[34~": {Type: KeyF20, Alt: true}, // urxvt
}

// Hex code mappings.
var hexes = map[string]Key{
"1b0d": {Type: KeyEnter, Alt: true},
"1b7f": {Type: KeyBackspace, Alt: true},

// Powershell
"1b4f41": {Type: KeyUp, Alt: false},
"1b4f42": {Type: KeyDown, Alt: false},
"1b4f43": {Type: KeyRight, Alt: false},
"1b4f44": {Type: KeyLeft, Alt: false},
// Powershell sequences.
"\x1bOA": {Type: KeyUp, Alt: false},
"\x1bOB": {Type: KeyDown, Alt: false},
"\x1bOC": {Type: KeyRight, Alt: false},
"\x1bOD": {Type: KeyLeft, Alt: false},
}

// readInputs reads keypress and mouse inputs from a TTY and returns messages
Expand Down Expand Up @@ -617,13 +610,6 @@ func readInputs(input io.Reader) ([]Msg, error) {
continue
}

// Some of these need special handling.
hex := fmt.Sprintf("%x", runes)
if k, ok := hexes[hex]; ok {
msgs = append(msgs, KeyMsg(k))
continue
}

// Is this an unrecognized CSI sequence? If so, ignore it.
if len(runes) > 2 && runes[0] == 0x1b && (runes[1] == '[' ||
(len(runes) > 3 && runes[1] == 0x1b && runes[2] == '[')) {
Expand Down
65 changes: 46 additions & 19 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tea

import (
"bytes"
"fmt"
"testing"
)

Expand Down Expand Up @@ -49,11 +50,12 @@ func TestKeyTypeString(t *testing.T) {

func TestReadInput(t *testing.T) {
type test struct {
in []byte
out []Msg
keyname string
in []byte
out []Msg
}
for out, td := range map[string]test{
"a": {
for i, td := range []test{
{"a",
[]byte{'a'},
[]Msg{
KeyMsg{
Expand All @@ -62,7 +64,7 @@ func TestReadInput(t *testing.T) {
},
},
},
" ": {
{" ",
[]byte{' '},
[]Msg{
KeyMsg{
Expand All @@ -71,15 +73,15 @@ func TestReadInput(t *testing.T) {
},
},
},
"ctrl+a": {
{"ctrl+a",
[]byte{byte(keySOH)},
[]Msg{
KeyMsg{
Type: KeyCtrlA,
},
},
},
"alt+a": {
{"alt+a",
[]byte{byte(0x1b), 'a'},
[]Msg{
KeyMsg{
Expand All @@ -89,7 +91,7 @@ func TestReadInput(t *testing.T) {
},
},
},
"abcd": {
{"abcd",
[]byte{'a', 'b', 'c', 'd'},
[]Msg{
KeyMsg{
Expand All @@ -110,31 +112,31 @@ func TestReadInput(t *testing.T) {
},
},
},
"up": {
{"up",
[]byte("\x1b[A"),
[]Msg{
KeyMsg{
Type: KeyUp,
},
},
},
"wheel up": {
{"wheel up",
[]byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},
[]Msg{
MouseMsg{
Type: MouseWheelUp,
},
},
},
"shift+tab": {
{"shift+tab",
[]byte{'\x1b', '[', 'Z'},
[]Msg{
KeyMsg{
Type: KeyShiftTab,
},
},
},
"alt+enter": {
{"alt+enter",
[]byte{'\x1b', '\r'},
[]Msg{
KeyMsg{
Expand All @@ -143,7 +145,7 @@ func TestReadInput(t *testing.T) {
},
},
},
"alt+ctrl+a": {
{"alt+ctrl+a",
[]byte{'\x1b', byte(keySOH)},
[]Msg{
KeyMsg{
Expand All @@ -152,12 +154,37 @@ func TestReadInput(t *testing.T) {
},
},
},
"unrecognized": {
{"unrecognized CSI",
[]byte{'\x1b', '[', '-', '-', '-', '-', 'X'},
[]Msg{},
},
// Powershell sequences.
{"up",
[]byte{'\x1b', 'O', 'A'},
[]Msg{KeyMsg{Type: KeyUp}},
},
{"down",
[]byte{'\x1b', 'O', 'B'},
[]Msg{KeyMsg{Type: KeyDown}},
},
{"right",
[]byte{'\x1b', 'O', 'C'},
[]Msg{KeyMsg{Type: KeyRight}},
},
{"left",
[]byte{'\x1b', 'O', 'D'},
[]Msg{KeyMsg{Type: KeyLeft}},
},
{"alt+enter",
[]byte{'\x1b', '\x0d'},
[]Msg{KeyMsg{Type: KeyEnter, Alt: true}},
},
{"alt+backspace",
[]byte{'\x1b', '\x7f'},
[]Msg{KeyMsg{Type: KeyBackspace, Alt: true}},
},
} {
t.Run(out, func(t *testing.T) {
t.Run(fmt.Sprintf("%d: %s", i, td.keyname), func(t *testing.T) {
msgs, err := readInputs(bytes.NewReader(td.in))
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -167,8 +194,8 @@ func TestReadInput(t *testing.T) {
}

if len(msgs) == 1 {
if m, ok := msgs[0].(KeyMsg); ok && m.String() != out {
t.Fatalf(`expected a keymsg %q, got %q`, out, m)
if m, ok := msgs[0].(KeyMsg); ok && m.String() != td.keyname {
t.Fatalf(`expected a keymsg %q, got %q`, td.keyname, m)
}
}

Expand All @@ -178,9 +205,9 @@ func TestReadInput(t *testing.T) {
t.Fatalf(`expected a keymsg %q, got %q`, td.out[i].(KeyMsg), m)
}
if m, ok := v.(MouseMsg); ok &&
(mouseEventTypes[m.Type] != out || m.Type != td.out[i].(MouseMsg).Type) {
(mouseEventTypes[m.Type] != td.keyname || m.Type != td.out[i].(MouseMsg).Type) {
t.Fatalf(`expected a mousemsg %q, got %q`,
out,
td.keyname,
mouseEventTypes[td.out[i].(MouseMsg).Type])
}
}
Expand Down

0 comments on commit 7e7a729

Please sign in to comment.