fix(key): support very long buffered input (#570)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Raphael 'kena' Poss · committed by GitHub · parent 6d07f4a · Browse files · 2 files·+54 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+54 added−3 removed
- other:+54 added−3 removed
Provenance
- Commit
5536bca- Tree
1a722fb- Author
Raphael 'kena' Poss <knz@thaumogen.net>- Committer
GitHub <noreply@github.com>- Parent
6d07f4a
2 files changed+54 added−3 removed
modified:key_test.go+20 added-1 removed
198 unmodified lines199 }200 201 for _, tc := range td {202 t.Run(fmt.Sprintf("%q", string(tc.seq)), func(t *testing.T) {203- width, msg := detectOneMsg(tc.seq)203+ width, msg := detectOneMsg(tc.seq, false /* canHaveMoreData */)204 if width != len(tc.seq) {205 t.Errorf("parser did not consume the entire input: got %d, expected %d", width, len(tc.seq))206 }207 if !reflect.DeepEqual(tc.msg, msg) {2 unmodified lines210 })211 }212 }213 214+func TestReadLongInput(t *testing.T) {215+ input := strings.Repeat("a", 1000)216+ msgs := testReadInputs(t, bytes.NewReader([]byte(input)))217+ if len(msgs) != 1 {218+ t.Errorf("expected 1 messages, got %d", len(msgs))219+ }220+ km := msgs[0]221+ k := Key(km.(KeyMsg))222+ if k.Type != KeyRunes {223+ t.Errorf("expected key runes, got %d", k.Type)224+ }225+ if len(k.Runes) != 1000 || !reflect.DeepEqual(k.Runes, []rune(input)) {226+ t.Errorf("unexpected runes: %+v", k)227+ }228+ if k.Alt {229+ t.Errorf("unexpected alt")230+ }231+}232+233 func TestReadInput(t *testing.T) {234 type test struct {235 keyname string236 in []byte445 unmodified linesmodified:key.go+34 added-2 removed
541 unmodified lines542 // containing information about the key or mouse events accordingly.543 func readInputs(ctx context.Context, msgs chan<- Msg, input io.Reader) error {544 var buf [256]byte545 546+ var leftOverFromPrevIteration []byte547+loop:548 for {549 // Read and block.550 numBytes, err := input.Read(buf[:])551 if err != nil {552 return fmt.Errorf("error reading input: %w", err)553 }554 b := buf[:numBytes]555+ if leftOverFromPrevIteration != nil {556+ b = append(leftOverFromPrevIteration, b...)557+ }558 559+ // If we had a short read (numBytes < len(buf)), we're sure that560+ // the end of this read is an event boundary, so there is no doubt561+ // if we are encountering the end of the buffer while parsing a message.562+ // However, if we've succeeded in filling up the buffer, there may563+ // be more data in the OS buffer ready to be read in, to complete564+ // the last message in the input. In that case, we will retry with565+ // the left over data in the next iteration.566+ canHaveMoreData := numBytes == len(buf)567+568 var i, w int569 for i, w = 0, 0; i < len(b); i += w {570 var msg Msg557- w, msg = detectOneMsg(b[i:])571+ w, msg = detectOneMsg(b[i:], canHaveMoreData)572+ if w == 0 {573+ // Expecting more bytes beyond the current buffer. Try waiting574+ // for more input.575+ leftOverFromPrevIteration = make([]byte, 0, len(b[i:])+len(buf))576+ leftOverFromPrevIteration = append(leftOverFromPrevIteration, b[i:]...)577+ continue loop578+ }579+580 select {581 case msgs <- msg:582 case <-ctx.Done():583 err := ctx.Err()2 unmodified lines586 }587 return err588 }589 }590+ leftOverFromPrevIteration = nil591 }592 }593 594 var unknownCSIRe = regexp.MustCompile(`^\x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]`)595 573-func detectOneMsg(b []byte) (w int, msg Msg) {596+func detectOneMsg(b []byte, canHaveMoreData bool) (w int, msg Msg) {597 // Detect mouse events.598 const mouseEventLen = 6599 if len(b) >= mouseEventLen && b[0] == '\x1b' && b[1] == '[' && b[2] == 'M' {600 return mouseEventLen, MouseMsg(parseX10MouseEvent(b))39 unmodified lines640 i += rw641 break642 }643 }644+ if i >= len(b) && canHaveMoreData {645+ // We have encountered the end of the input buffer. Alas, we can't646+ // be sure whether the data in the remainder of the buffer is647+ // complete (maybe there was a short read). Instead of sending anything648+ // dumb to the message channel, do a short read. The outer loop will649+ // handle this case by extending the buffer as necessary.650+ return 0, nil651+ }652+653 // If we found at least one rune, we report the bunch of them as654 // a single KeyRunes or KeySpace event.655 if len(runes) > 0 {656 k := Key{Type: KeyRunes, Runes: runes, Alt: alt}17 unmodified lines