Skip to content
Myr

fix: Windows: first character input lost on successive programs (#1368)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Fix: Windows: first character input lost on successive programs * lint G115 * intToUint32OrDie * avoid max check math.MaxUint32 (untyped int constant 4294967295) overflows int

authored by Josh Allen · committed by GitHub · parent ca9473b · Browse files · 2 files+49 added−6 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+49 added−6 removed

  • other:+49 added−6 removed

2 files+49 added−6 removed

modified:inputreader_windows.go+2 added
65 unmodified lines
66// Cancel implements cancelreader.CancelReader.67func (r *conInputReader) Cancel() bool {68 r.setCanceled()6970 // Warning: These cancel methods do not reliably work on console input71 // and should not be counted on.72 return windows.CancelIoEx(r.conin, nil) == nil || windows.CancelIo(r.conin) == nil73}7475// Close implements cancelreader.CancelReader.
54 unmodified lines
modified:key_windows.go+47 added-6 removed
5 unmodified lines
6import (7 "context"8 "fmt"9 "io"10 "time"1112 "github.com/erikgeiser/coninput"13 localereader "github.com/mattn/go-localereader"14 "github.com/muesli/cancelreader"
10 unmodified lines
25func readConInputs(ctx context.Context, msgsch chan<- Msg, con *conInputReader) error {26 var ps coninput.ButtonState // keep track of previous mouse state27 var ws coninput.WindowBufferSizeEventRecord // keep track of the last window size event28 for {28 events, err := coninput.ReadNConsoleInputs(con.conin, 16)29 events, err := peekAndReadConsInput(con)30 if err != nil {30 if con.isCanceled() {31 return cancelreader.ErrCanceled32 }33 return fmt.Errorf("read coninput events: %w", err)31 return err32 }3533 for _, event := range events {34 var msgs []Msg35 switch e := event.Unwrap().(type) {36 case coninput.KeyEventRecord:
53 unmodified lines
90 }91 }92}9394// Peek for new input in a tight loop and then read the input.95// windows.CancelIo* does not work reliably so peek first and only use the data if96// the console input is not cancelled.97func peekAndReadConsInput(con *conInputReader) ([]coninput.InputRecord, error) {98 events, err := peekConsInput(con)99 if err != nil {100 return events, err101 }102 events, err = coninput.ReadNConsoleInputs(con.conin, intToUint32OrDie(len(events)))103 if con.isCanceled() {104 return events, cancelreader.ErrCanceled105 }106 if err != nil {107 return events, fmt.Errorf("read coninput events: %w", err)108 }109 return events, nil110}111112// Convert i to unit32 or panic if it cannot be converted. Check satisifes lint G115.113func intToUint32OrDie(i int) uint32 {114 if i < 0 {115 panic("cannot convert numEvents " + fmt.Sprint(i) + " to uint32")116 }117 return uint32(i)118}119120// Keeps peeking until there is data or the input is cancelled.121func peekConsInput(con *conInputReader) ([]coninput.InputRecord, error) {122 for {123 events, err := coninput.PeekNConsoleInputs(con.conin, 16)124 if con.isCanceled() {125 return events, cancelreader.ErrCanceled126 }127 if err != nil {128 return events, fmt.Errorf("peek coninput events: %w", err)129 }130 if len(events) > 0 {131 return events, nil132 }133 // Sleep for a bit to avoid busy waiting.134 time.Sleep(16 * time.Millisecond)135 }136}137138func mouseEventButton(p, s coninput.ButtonState) (button MouseButton, action MouseAction) {139 btn := p ^ s140 action = MouseActionPress141 if btn&s == 0 {
300 unmodified lines