From dd2842f3aface1b184e5ef0e06caee5476c3ae34 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 21 Oct 2024 12:32:34 -0400 Subject: [PATCH] fix: windows: ensure the input buffer is empty 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. --- cancelreader_windows.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cancelreader_windows.go b/cancelreader_windows.go index 4aa5a759e0..e55ca79d31 100644 --- a/cancelreader_windows.go +++ b/cancelreader_windows.go @@ -10,6 +10,7 @@ import ( "sync" "time" + xwindows "github.com/charmbracelet/x/windows" "github.com/muesli/cancelreader" "golang.org/x/sys/windows" ) @@ -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,