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
3 changes: 3 additions & 0 deletions emacs.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ func (rl *Shell) bracketedPasteBegin() {
key, empty := core.PopKey(rl.Keys)
if empty {
core.WaitAvailableKeys(rl.Keys, rl.Config)
if rl.Keys.IsEOF() || rl.Keys.ReadError() != nil {
return
}
continue
}

Expand Down
25 changes: 20 additions & 5 deletions internal/core/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ type Keys struct {
cursor chan []byte // Cursor coordinates has been read on stdin.
resize chan bool // Resize events on Windows are sent on stdin. USED IN WINDOWS

eof bool // EOF has been reached.
cfg *inputrc.Config // Configuration file used for meta key settings
mutex sync.RWMutex // Concurrency safety
eof bool // EOF has been reached.
readErr error // Non-EOF input failure, e.g. revoked tty.
cfg *inputrc.Config // Configuration file used for meta key settings
mutex sync.RWMutex // Concurrency safety
}

// WaitAvailableKeys waits until an input key is either read from standard input,
Expand Down Expand Up @@ -71,8 +72,14 @@ func WaitAvailableKeys(keys *Keys, cfg *inputrc.Config) {
// We will either read keyBuf from user, or an EOF
// send by ourselves, because we pause reading.
keyBuf, err := keys.readInputFiltered()
if err != nil && errors.Is(err, io.EOF) {
keys.eof = true
if err != nil {
keys.mutex.Lock()
if errors.Is(err, io.EOF) {
keys.eof = true
} else if keys.readErr == nil {
keys.readErr = err
}
keys.mutex.Unlock()
return
}

Expand Down Expand Up @@ -109,6 +116,14 @@ func (k *Keys) IsEOF() bool {
return k.eof
}

// ReadError returns the first non-EOF input error observed while reading keys.
func (k *Keys) ReadError() error {
k.mutex.RLock()
defer k.mutex.RUnlock()

return k.readErr
}

// PeekKey returns the first key in the stack, without removing it.
func PeekKey(keys *Keys) (key byte, empty bool) {
switch {
Expand Down
63 changes: 63 additions & 0 deletions internal/core/keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package core

import (
"errors"
"io"
"testing"

"github.com/reeflective/readline/inputrc"
)

type stubReadCloser struct {
read func([]byte) (int, error)
}

func (s stubReadCloser) Read(p []byte) (int, error) {
return s.read(p)
}

func (s stubReadCloser) Close() error {
return nil
}

func TestWaitAvailableKeysMarksEOF(t *testing.T) {
original := Stdin
Stdin = stubReadCloser{
read: func([]byte) (int, error) {
return 0, io.EOF
},
}
defer func() { Stdin = original }()

keys := &Keys{}
WaitAvailableKeys(keys, &inputrc.Config{})

if !keys.IsEOF() {
t.Fatal("expected EOF to be recorded")
}
if err := keys.ReadError(); err != nil {
t.Fatalf("expected no non-EOF read error, got %v", err)
}
}

func TestWaitAvailableKeysRecordsNonEOFReadError(t *testing.T) {
want := errors.New("read failed")

original := Stdin
Stdin = stubReadCloser{
read: func([]byte) (int, error) {
return 0, want
},
}
defer func() { Stdin = original }()

keys := &Keys{}
WaitAvailableKeys(keys, &inputrc.Config{})

if keys.IsEOF() {
t.Fatal("expected non-EOF read failure not to be marked as EOF")
}
if err := keys.ReadError(); !errors.Is(err, want) {
t.Fatalf("expected read error %v, got %v", want, err)
}
}
4 changes: 1 addition & 3 deletions internal/core/keys_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
package core

import (
"errors"
"fmt"
"io"
"os"
"strconv"

Expand Down Expand Up @@ -96,7 +94,7 @@ func (k *Keys) readInputFiltered() (keys []byte, err error) {
buf := make([]byte, keyScanBufSize)

read, err := Stdin.Read(buf)
if err != nil && errors.Is(err, io.EOF) {
if err != nil {
return
}

Expand Down
4 changes: 1 addition & 3 deletions internal/core/keys_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package core

import (
"errors"
"io"
"unsafe"

"github.com/reeflective/readline/inputrc"
Expand Down Expand Up @@ -72,7 +70,7 @@ func (k *Keys) readInputFiltered() (keys []byte, err error) {
buf := make([]byte, keyScanBufSize)

read, err := Stdin.Read(buf)
if err != nil && errors.Is(err, io.EOF) {
if err != nil {
return keys, err
}

Expand Down
4 changes: 4 additions & 0 deletions readline.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (rl *Shell) Readline() (string, error) {
// the macro engine has fed some keys in bulk when running one.
core.WaitAvailableKeys(rl.Keys, rl.Config)

if err := rl.Keys.ReadError(); err != nil {
return "", err
}

// If the input is closed, we must return the line
// and the error so that the caller can handle it.
if rl.Keys.IsEOF() {
Expand Down
Loading