Skip to content

Commit

Permalink
fix: windows: ensure the input buffer is empty
Browse files Browse the repository at this point in the history
Before initializing the input reader, we need to ensure that
the console input buffer is empty, otherwise, we can get dangling
events from previous sessions.
  • Loading branch information
aymanbagabas committed Oct 21, 2024
1 parent 9f6f5cd commit dd2842f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cancelreader_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

xwindows "github.com/charmbracelet/x/windows"
"github.com/muesli/cancelreader"
"golang.org/x/sys/windows"
)
Expand Down Expand Up @@ -47,6 +48,11 @@ func newCancelreader(r io.Reader) (cancelreader.CancelReader, error) {
return fallback(r)
}

// Discard any pending input events.
if err := xwindows.FlushConsoleInputBuffer(conin); err != nil {
return fallback(r)
}

originalMode, err := prepareConsole(conin,
windows.ENABLE_MOUSE_INPUT,
windows.ENABLE_WINDOW_INPUT,
Expand Down

0 comments on commit dd2842f

Please sign in to comment.