Skip to content
Myr

fix(windows): clean up windows input reader

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

Remove unused and use CancelIo API to cancel reads.

authored by Ayman Bagabas · committed by Ayman Bagabas · parent ef4e46c · Browse files · 1 file+2 added−49 removed
This commit

State

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

Diff stats

1 file+2 added−49 removed

  • other:+2 added−49 removed

1 file+2 added−49 removed

modified:inputreader_windows.go+2 added-49 removed
15 unmodified lines
1617type conInputReader struct {18 cancelMixin1920 conin windows.Handle21 cancelEvent windows.Handle20 conin windows.Handle2122 originalMode uint322425 // inputEvent holds the input event that was read in order to avoid26 // unnecessary allocations. This re-use is possible because27 // InputRecord.Unwarp which is called inparseInputMsgFromInputRecord28 // returns an data structure that is independent of the passed InputRecord.29 inputEvent []coninput.InputRecord23}2425var _ cancelreader.CancelReader = &conInputReader{}26
18 unmodified lines
45 if err != nil {46 return nil, fmt.Errorf("failed to prepare console input: %w", err)47 }4856 cancelEvent, err := windows.CreateEvent(nil, 0, 0, nil)57 if err != nil {58 return nil, fmt.Errorf("create stop event: %w", err)59 }6049 return &conInputReader{50 conin: conin,63 cancelEvent: cancelEvent,51 originalMode: originalMode,52 }, nil53}5455// Cancel implements cancelreader.CancelReader.56func (r *conInputReader) Cancel() bool {57 r.setCanceled()5872 err := windows.SetEvent(r.cancelEvent)73 if err != nil {74 return false75 }7677 return true59 return windows.CancelIo(r.conin) == nil60}6162// Close implements cancelreader.CancelReader.63func (r *conInputReader) Close() error {82 err := windows.CloseHandle(r.cancelEvent)83 if err != nil {84 return fmt.Errorf("closing cancel event handle: %w", err)85 }8664 if r.originalMode != 0 {65 err := windows.SetConsoleMode(r.conin, r.originalMode)66 if err != nil {67 return fmt.Errorf("reset console mode: %w", err)
23 unmodified lines
9192 return originalMode, nil93}94118func waitForInput(conin, cancel windows.Handle) error {119 event, err := windows.WaitForMultipleObjects([]windows.Handle{conin, cancel}, false, windows.INFINITE)120 switch {121 case windows.WAIT_OBJECT_0 <= event && event < windows.WAIT_OBJECT_0+2:122 if event == windows.WAIT_OBJECT_0+1 {123 return cancelreader.ErrCanceled124 }125126 if event == windows.WAIT_OBJECT_0 {127 return nil128 }129130 return fmt.Errorf("unexpected wait object is ready: %d", event-windows.WAIT_OBJECT_0)131 case windows.WAIT_ABANDONED <= event && event < windows.WAIT_ABANDONED+2:132 return fmt.Errorf("abandoned")133 case event == uint32(windows.WAIT_TIMEOUT):134 return fmt.Errorf("timeout")135 case event == windows.WAIT_FAILED:136 return fmt.Errorf("failed")137 default:138 return fmt.Errorf("unexpected error: %w", err)139 }140}14195// cancelMixin represents a goroutine-safe cancelation status.96type cancelMixin struct {97 unsafeCanceled bool98 lock sync.Mutex
8 unmodified lines