[windows] Prevent fzf from consuming user input while paused
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This partly fixes #4260. fzf still can consume the first key stroke.
authored by Junegunn Choi · committed by Junegunn Choi · parent 21ce700 · Browse files · 2 files·+39 added−31 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+39 added−31 removed
- other:+39 added−31 removed
Provenance
- Commit
8916cbc- Tree
af47bec- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
21ce700
2 files changed+39 added−31 removed
modified:src/tui/light_windows.go+36 added-27 removed
16 unmodified lines17 18 var (19 consoleFlagsInput = uint32(windows.ENABLE_VIRTUAL_TERMINAL_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_EXTENDED_FLAGS)20 consoleFlagsOutput = uint32(windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING | windows.ENABLE_PROCESSED_OUTPUT | windows.DISABLE_NEWLINE_AUTO_RETURN)21+ counter = uint64(0)22 )23 24 // IsLightRendererSupported checks to see if the Light renderer is supported25 func IsLightRendererSupported() bool {35 unmodified lines61 return err62 }63 r.inHandle = uintptr(inHandle)64 64- r.setupTerminal()65-65 // channel for non-blocking reads. Buffer to make sure66 // we get the ESC sets:67 r.ttyinChannel = make(chan byte, 1024)68 70- // the following allows for non-blocking IO.71- // syscall.SetNonblock() is a NOOP under Windows.72- go func() {73- fd := int(r.inHandle)74- b := make([]byte, 1)75- for !r.closed.Get() {76- // HACK: if run from PSReadline, something resets ConsoleMode to remove ENABLE_VIRTUAL_TERMINAL_INPUT.77- _ = windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)69+ r.setupTerminal()70 79- _, err := util.Read(fd, b)80- if err == nil {81- r.ttyinChannel <- b[0]82- }83- }84- }()85-71 return nil72 }73 74 func (r *LightRenderer) closePlatform() {9 unmodified lines84 func openTtyOut() (*os.File, error) {85 return os.Stderr, nil86 }87 103-func (r *LightRenderer) setupTerminal() error {104- if err := windows.SetConsoleMode(windows.Handle(r.outHandle), consoleFlagsOutput); err != nil {105- return err106- }107- return windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)88+func (r *LightRenderer) setupTerminal() {89+ windows.SetConsoleMode(windows.Handle(r.outHandle), consoleFlagsOutput)90+ windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)91+92+ // The following allows for non-blocking IO.93+ // syscall.SetNonblock() is a NOOP under Windows.94+ current := counter95+ go func() {96+ fd := int(r.inHandle)97+ b := make([]byte, 1)98+ for {99+ if _, err := util.Read(fd, b); err == nil {100+ r.mutex.Lock()101+ // This condition prevents the goroutine from running after the renderer102+ // has been closed or paused.103+ if current != counter {104+ r.mutex.Unlock()105+ break106+ }107+ r.ttyinChannel <- b[0]108+ // HACK: if run from PSReadline, something resets ConsoleMode to remove ENABLE_VIRTUAL_TERMINAL_INPUT.109+ windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)110+ r.mutex.Unlock()111+ }112+ }113+ }()114 }115 110-func (r *LightRenderer) restoreTerminal() error {111- if err := windows.SetConsoleMode(windows.Handle(r.inHandle), r.origStateInput); err != nil {112- return err113- }114- return windows.SetConsoleMode(windows.Handle(r.outHandle), r.origStateOutput)116+func (r *LightRenderer) restoreTerminal() {117+ r.mutex.Lock()118+ counter++119+ // We're setting ENABLE_VIRTUAL_TERMINAL_INPUT to allow escape sequences to be read during 'execute'.120+ // e.g. fzf --bind 'enter:execute:less {}'121+ windows.SetConsoleMode(windows.Handle(r.inHandle), r.origStateInput|windows.ENABLE_VIRTUAL_TERMINAL_INPUT)122+ windows.SetConsoleMode(windows.Handle(r.outHandle), r.origStateOutput)123+ r.mutex.Unlock()124 }125 126 func (r *LightRenderer) Size() TermSize {127 var w, h int39 unmodified linesmodified:src/tui/light.go+3 added-4 removed
6 unmodified lines7 "os"8 "regexp"9 "strconv"10 "strings"11+ "sync"12 "time"13 "unicode/utf8"14 15 "github.com/junegunn/fzf/src/util"79 unmodified lines95 }96 97 // Light renderer98 type LightRenderer struct {98- closed *util.AtomicBool99 theme *ColorTheme100 mouse bool101 forceBlack bool102 clearOnExit bool16 unmodified lines119 maxHeightFunc func(int) int120 showCursor bool121 122 // Windows only123+ mutex sync.Mutex124 ttyinChannel chan byte125 inHandle uintptr126 outHandle uintptr127 origStateInput uint3223 unmodified lines151 if err != nil {152 out = os.Stderr153 }154 r := LightRenderer{154- closed: util.NewAtomicBool(false),155 theme: theme,156 forceBlack: forceBlack,157 mouse: mouse,158 clearOnExit: clearOnExit,615 unmodified lines774 r.csi("?25h")775 }776 r.disableMouse()777 r.flush()778- r.closePlatform()778 r.restoreTerminal()780- r.closed.Set(true)779+ r.closePlatform()780 }781 782 func (r *LightRenderer) Top() int {783 return r.yoffset475 unmodified lines