diff --git a/key.go b/key.go index ae4bf1a6a3..a6452adde3 100644 --- a/key.go +++ b/key.go @@ -292,9 +292,9 @@ var hexes = map[string]Key{ "1b4f44": {Type: KeyLeft, Alt: false}, } -// readInput reads keypress and mouse input from a TTY and returns a message -// containing information about the key or mouse event accordingly. -func readInput(input io.Reader) (Msg, error) { +// readInput reads keypress and mouse inputs from a TTY and returns messages +// containing information about the key or mouse events accordingly. +func readInput(input io.Reader) ([]Msg, error) { var buf [256]byte // Read and block @@ -307,18 +307,26 @@ func readInput(input io.Reader) (Msg, error) { // only. mouseEvent, err := parseX10MouseEvent(buf[:numBytes]) if err == nil { - return MouseMsg(mouseEvent), nil + var m []Msg + for _, v := range mouseEvent { + m = append(m, MouseMsg(v)) + } + return m, nil } // Is it a special sequence, like an arrow key? if k, ok := sequences[string(buf[:numBytes])]; ok { - return KeyMsg(Key{Type: k}), nil + return []Msg{ + KeyMsg(Key{Type: k}), + }, nil } // Some of these need special handling hex := fmt.Sprintf("%x", buf[:numBytes]) if k, ok := hexes[hex]; ok { - return KeyMsg(k), nil + return []Msg{ + KeyMsg(k), + }, nil } // Is the alt key pressed? The buffer will be prefixed with an escape @@ -330,7 +338,9 @@ func readInput(input io.Reader) (Msg, error) { if c == utf8.RuneError { return nil, errors.New("could not decode rune after removing initial escape") } - return KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: []rune{c}}), nil + return []Msg{ + KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: []rune{c}}), + }, nil } var runes []rune @@ -353,15 +363,21 @@ func readInput(input io.Reader) (Msg, error) { } else if len(runes) > 1 { // We received multiple runes, so we know this isn't a control // character, sequence, and so on. - return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil + return []Msg{ + KeyMsg(Key{Type: KeyRunes, Runes: runes}), + }, nil } // Is the first rune a control character? r := KeyType(runes[0]) if numBytes == 1 && r <= keyUS || r == keyDEL { - return KeyMsg(Key{Type: r}), nil + return []Msg{ + KeyMsg(Key{Type: r}), + }, nil } // Welp, it's just a regular, ol' single rune - return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil + return []Msg{ + KeyMsg(Key{Type: KeyRunes, Runes: runes}), + }, nil } diff --git a/key_test.go b/key_test.go index ecb551c124..bbe6fb7fcc 100644 --- a/key_test.go +++ b/key_test.go @@ -58,14 +58,18 @@ func TestReadInput(t *testing.T) { "shift+tab": {'\x1b', '[', 'Z'}, } { t.Run(out, func(t *testing.T) { - msg, err := readInput(bytes.NewReader(in)) + msgs, err := readInput(bytes.NewReader(in)) if err != nil { t.Fatalf("unexpected error: %v", err) } - if m, ok := msg.(KeyMsg); ok && m.String() != out { + if len(msgs) == 0 { + t.Fatalf("unexpected empty message list") + } + + if m, ok := msgs[0].(KeyMsg); ok && m.String() != out { t.Fatalf(`expected a keymsg %q, got %q`, out, m) } - if m, ok := msg.(MouseMsg); ok && mouseEventTypes[m.Type] != out { + if m, ok := msgs[0].(MouseMsg); ok && mouseEventTypes[m.Type] != out { t.Fatalf(`expected a mousemsg %q, got %q`, out, mouseEventTypes[m.Type]) } }) diff --git a/mouse.go b/mouse.go index 6cca0b67d5..3028d083db 100644 --- a/mouse.go +++ b/mouse.go @@ -1,6 +1,9 @@ package tea -import "errors" +import ( + "bytes" + "errors" +) // MouseMsg contains information about a mouse event and are sent to a programs // update function when mouse activity occurs. Note that the mouse must first @@ -63,70 +66,84 @@ var mouseEventTypes = map[MouseEventType]string{ // ESC [M Cb Cx Cy // // See: http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking -func parseX10MouseEvent(buf []byte) (m MouseEvent, err error) { - if len(buf) != 6 || string(buf[:3]) != "\x1b[M" { - return m, errors.New("not an X10 mouse event") - } - - const byteOffset = 32 - - e := buf[3] - byteOffset +func parseX10MouseEvent(buf []byte) ([]MouseEvent, error) { + var r []MouseEvent - const ( - bitShift = 0b0000_0100 - bitAlt = 0b0000_1000 - bitCtrl = 0b0001_0000 - bitMotion = 0b0010_0000 - bitWheel = 0b0100_0000 - - bitsMask = 0b0000_0011 + seq := []byte("\x1b[M") + if !bytes.Contains(buf, seq) { + return r, errors.New("not an X10 mouse event") + } - bitsLeft = 0b0000_0000 - bitsMiddle = 0b0000_0001 - bitsRight = 0b0000_0010 - bitsRelease = 0b0000_0011 + for _, v := range bytes.Split(buf, seq) { + if len(v) == 0 { + continue + } + if len(v) != 3 { + return r, errors.New("not an X10 mouse event") + } - bitsWheelUp = 0b0000_0000 - bitsWheelDown = 0b0000_0001 - ) + var m MouseEvent + const byteOffset = 32 + e := v[0] - byteOffset + + const ( + bitShift = 0b0000_0100 + bitAlt = 0b0000_1000 + bitCtrl = 0b0001_0000 + bitMotion = 0b0010_0000 + bitWheel = 0b0100_0000 + + bitsMask = 0b0000_0011 + + bitsLeft = 0b0000_0000 + bitsMiddle = 0b0000_0001 + bitsRight = 0b0000_0010 + bitsRelease = 0b0000_0011 + + bitsWheelUp = 0b0000_0000 + bitsWheelDown = 0b0000_0001 + ) + + if e&bitWheel != 0 { + // Check the low two bits. + switch e & bitsMask { + case bitsWheelUp: + m.Type = MouseWheelUp + case bitsWheelDown: + m.Type = MouseWheelDown + } + } else { + // Check the low two bits. + // We do not separate clicking and dragging. + switch e & bitsMask { + case bitsLeft: + m.Type = MouseLeft + case bitsMiddle: + m.Type = MouseMiddle + case bitsRight: + m.Type = MouseRight + case bitsRelease: + if e&bitMotion != 0 { + m.Type = MouseMotion + } else { + m.Type = MouseRelease + } + } + } - if e&bitWheel != 0 { - // Check the low two bits. - switch e & bitsMask { - case bitsWheelUp: - m.Type = MouseWheelUp - case bitsWheelDown: - m.Type = MouseWheelDown + if e&bitAlt != 0 { + m.Alt = true } - } else { - // Check the low two bits. - // We do not separate clicking and dragging. - switch e & bitsMask { - case bitsLeft: - m.Type = MouseLeft - case bitsMiddle: - m.Type = MouseMiddle - case bitsRight: - m.Type = MouseRight - case bitsRelease: - if e&bitMotion != 0 { - m.Type = MouseMotion - } else { - m.Type = MouseRelease - } + if e&bitCtrl != 0 { + m.Ctrl = true } - } - if e&bitAlt != 0 { - m.Alt = true - } - if e&bitCtrl != 0 { - m.Ctrl = true - } + // (1,1) is the upper left. We subtract 1 to normalize it to (0,0). + m.X = int(v[1]) - byteOffset - 1 + m.Y = int(v[2]) - byteOffset - 1 - // (1,1) is the upper left. We subtract 1 to normalize it to (0,0). - m.X = int(buf[4]) - byteOffset - 1 - m.Y = int(buf[5]) - byteOffset - 1 + r = append(r, m) + } - return m, nil + return r, nil } diff --git a/mouse_test.go b/mouse_test.go index 9a4df8ae95..4e7aed3f82 100644 --- a/mouse_test.go +++ b/mouse_test.go @@ -122,209 +122,251 @@ func TestParseX10MouseEvent(t *testing.T) { tt := []struct { name string buf []byte - expected MouseEvent + expected []MouseEvent }{ // Position. { name: "zero position", buf: encode(0b0010_0000, 0, 0), - expected: MouseEvent{ - X: 0, - Y: 0, - Type: MouseLeft, + expected: []MouseEvent{ + { + X: 0, + Y: 0, + Type: MouseLeft, + }, }, }, { name: "max position", buf: encode(0b0010_0000, 222, 222), // Because 255 (max int8) - 32 - 1. - expected: MouseEvent{ - X: 222, - Y: 222, - Type: MouseLeft, + expected: []MouseEvent{ + { + X: 222, + Y: 222, + Type: MouseLeft, + }, }, }, // Simple. { name: "left", buf: encode(0b0000_0000, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseLeft, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseLeft, + }, }, }, { name: "left in motion", buf: encode(0b0010_0000, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseLeft, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseLeft, + }, }, }, { name: "middle", buf: encode(0b0000_0001, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseMiddle, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseMiddle, + }, }, }, { name: "middle in motion", buf: encode(0b0010_0001, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseMiddle, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseMiddle, + }, }, }, { name: "right", buf: encode(0b0000_0010, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseRight, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseRight, + }, }, }, { name: "right in motion", buf: encode(0b0010_0010, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseRight, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseRight, + }, }, }, { name: "motion", buf: encode(0b0010_0011, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseMotion, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseMotion, + }, }, }, { name: "wheel up", buf: encode(0b0100_0000, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseWheelUp, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseWheelUp, + }, }, }, { name: "wheel down", buf: encode(0b0100_0001, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseWheelDown, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseWheelDown, + }, }, }, { name: "release", buf: encode(0b0000_0011, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseRelease, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseRelease, + }, }, }, // Combinations. { name: "alt+right", buf: encode(0b0010_1010, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseRight, - Alt: true, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseRight, + Alt: true, + }, }, }, { name: "ctrl+right", buf: encode(0b0011_0010, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseRight, - Ctrl: true, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseRight, + Ctrl: true, + }, }, }, { name: "ctrl+alt+right", buf: encode(0b0011_1010, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseRight, - Alt: true, - Ctrl: true, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseRight, + Alt: true, + Ctrl: true, + }, }, }, { name: "alt+wheel down", buf: encode(0b0100_1001, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseWheelDown, - Alt: true, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseWheelDown, + Alt: true, + }, }, }, { name: "ctrl+wheel down", buf: encode(0b0101_0001, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseWheelDown, - Ctrl: true, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseWheelDown, + Ctrl: true, + }, }, }, { name: "ctrl+alt+wheel down", buf: encode(0b0101_1001, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseWheelDown, - Alt: true, - Ctrl: true, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseWheelDown, + Alt: true, + Ctrl: true, + }, }, }, // Unknown. { name: "wheel with unknown bit", buf: encode(0b0100_0010, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseUnknown, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseUnknown, + }, }, }, { name: "unknown with modifier", buf: encode(0b0100_1010, 32, 16), - expected: MouseEvent{ - X: 32, - Y: 16, - Type: MouseUnknown, - Alt: true, + expected: []MouseEvent{ + { + X: 32, + Y: 16, + Type: MouseUnknown, + Alt: true, + }, }, }, // Overflow position. { name: "overflow position", buf: encode(0b0010_0000, 250, 223), // Because 255 (max int8) - 32 - 1. - expected: MouseEvent{ - X: -6, - Y: -33, - Type: MouseLeft, + expected: []MouseEvent{ + { + X: -6, + Y: -33, + Type: MouseLeft, + }, }, }, } @@ -335,16 +377,20 @@ func TestParseX10MouseEvent(t *testing.T) { t.Run(tc.name, func(t *testing.T) { actual, err := parseX10MouseEvent(tc.buf) if err != nil { - t.Fatalf("unexpected error: %v", + t.Fatalf("unexpected error for test %d: %v", + i, err, ) } - if tc.expected != actual { - t.Fatalf("expected %#v but got %#v", - tc.expected, - actual, - ) + for i := range tc.expected { + if tc.expected[i] != actual[i] { + t.Fatalf("expected %#v for test %d but got %#v", + tc.expected[i], + i, + actual[i], + ) + } } }) } @@ -380,7 +426,7 @@ func TestParseX10MouseEvent_error(t *testing.T) { _, err := parseX10MouseEvent(tc.buf) if err == nil { - t.Fatalf("expected error but got nil") + t.Fatalf("expected error but got nil for test %d", i) } }) } diff --git a/tea.go b/tea.go index fdf29ee650..37d3c3671c 100644 --- a/tea.go +++ b/tea.go @@ -411,7 +411,7 @@ func (p *Program) StartReturningModel() (Model, error) { return } - msg, err := readInput(cancelReader) + msgs, err := readInput(cancelReader) if err != nil { if !errors.Is(err, io.EOF) && !errors.Is(err, errCanceled) { errs <- err @@ -420,7 +420,9 @@ func (p *Program) StartReturningModel() (Model, error) { return } - p.msgs <- msg + for _, msg := range msgs { + p.msgs <- msg + } } }() } else {