Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,13 @@ func (p *EventDecoder) parseOsc(b []byte) (int, Event) {
case ansi.CAN, ansi.SUB:
return i, ignoredEvent(b[:i])
case ansi.ESC:
if i >= len(b) || b[i] != '\\' {
if i >= len(b) {
// The input ends between the ESC and the backslash of an ST.
// A read boundary can fall there in any long sequence, so
// it's incomplete until more input says otherwise.
return i, incompleteEvent(b[:i])
}
if b[i] != '\\' {
if cmd == -1 || (start == 0 && end == 2) {
return 2, defaultKey()
}
Expand Down Expand Up @@ -909,7 +915,11 @@ func (p *EventDecoder) parseStTerminated(intro8, intro7 byte, fn func([]byte) Ev
case ansi.CAN, ansi.SUB:
return i, ignoredEvent(b[:i])
case ansi.ESC:
if i >= len(b) || b[i] != '\\' {
if i >= len(b) {
// Incomplete, as in parseOsc.
return i, incompleteEvent(b[:i])
}
if b[i] != '\\' {
if start == end {
return defaultKey(b)
}
Expand Down
7 changes: 7 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,10 @@ type ProgramStatusSupportEvent struct{}
// ignoredEvent represents a sequence event that is ignored by the terminal
// reader. This is used to ignore certain sequences that can be canceled.
type ignoredEvent string

// incompleteEvent represents a string sequence (OSC, APC, and the like) cut
// off between the ESC and the backslash of its ST terminator, which is
// where the input ends. The terminal reader waits for more input. If none
// comes before the escape timeout, the lone ESC cancelled the sequence
// after all, and it's ignored.
type incompleteEvent string
44 changes: 44 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2759,3 +2759,47 @@ func TestKeyStringMore(t *testing.T) {
})
}
}

// chunkedReader returns its chunks one Read at a time, the way a terminal's
// input arrives in pieces that needn't line up with the sequences in it.
type chunkedReader struct{ chunks [][]byte }

func (r *chunkedReader) Read(p []byte) (int, error) {
if len(r.chunks) == 0 {
return 0, io.EOF
}
n := copy(p, r.chunks[0])
if n < len(r.chunks[0]) {
r.chunks[0] = r.chunks[0][n:]
} else {
r.chunks = r.chunks[1:]
}
return n, nil
}

// A read that ends between the ESC and the backslash of a sequence's ST
// terminator leaves the sequence incomplete, not cancelled. It must come
// out whole once the backslash arrives, rather than be dropped with the
// backslash reported as a key. Large OSC replies, such as kitty's OSC 5522
// clipboard data sent in thousands of 5 KiB packets, cross read boundaries
// constantly, so this isn't rare in practice.
func TestReadInputSplitTerminator(t *testing.T) {
for _, seq := range []string{
"\x1b]5522;type=read:status=DATA;QUJD\x1b\\",
"\x1b_hello\x1b\\",
} {
cut := len(seq) - 1 // the first read ends on the terminator's ESC
in := &chunkedReader{chunks: [][]byte{[]byte(seq[:cut]), []byte(seq[cut:] + "a")}}
events := testReadInputs(t, in)
var want []Event
switch seq[1] {
case ']':
want = []Event{UnknownOscEvent(seq), KeyPressEvent{Code: 'a', Text: "a"}}
case '_':
want = []Event{UnknownApcEvent(seq), KeyPressEvent{Code: 'a', Text: "a"}}
}
if !reflect.DeepEqual(events, want) {
t.Errorf("%q split before its final backslash:\n got %#v\nwant %#v", seq, events, want)
}
}
}
9 changes: 8 additions & 1 deletion terminal_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (d *eventScanner) scanEvents(buf []byte, expired bool) (total int, events [
d.paste = append(d.paste, seq...)
}
}
case UnknownEvent:
case UnknownEvent, incompleteEvent:
if !expired {
// If the event is unknown and we are not expired, we
// return need to try to decode the buffer again.
Expand All @@ -337,6 +337,13 @@ func (d *eventScanner) scanEvents(buf []byte, expired bool) (total int, events [
case ignoredEvent:
// ignore this event
event = nil
case incompleteEvent:
// Wait for the rest of the sequence, unless the escape timeout
// has passed without it, when it counts as cancelled.
if !expired {
return total, events
}
event = nil
case UnknownEvent:
isUnknown = true
// Try to look up the event in the table.
Expand Down