Skip to content
Myr

[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

2 files+39 added−31 removed

modified:src/tui/light_windows.go+36 added-27 removed
16 unmodified lines
1718var (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)2324// IsLightRendererSupported checks to see if the Light renderer is supported25func IsLightRendererSupported() bool {
35 unmodified lines
61 return err62 }63 r.inHandle = uintptr(inHandle)6464 r.setupTerminal()6565 // channel for non-blocking reads. Buffer to make sure66 // we get the ESC sets:67 r.ttyinChannel = make(chan byte, 1024)6870 // 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()7079 _, err := util.Read(fd, b)80 if err == nil {81 r.ttyinChannel <- b[0]82 }83 }84 }()8571 return nil72}7374func (r *LightRenderer) closePlatform() {
9 unmodified lines
84func openTtyOut() (*os.File, error) {85 return os.Stderr, nil86}87103func (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)88func (r *LightRenderer) setupTerminal() {89 windows.SetConsoleMode(windows.Handle(r.outHandle), consoleFlagsOutput)90 windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)9192 // 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}115110func (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)116func (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}125126func (r *LightRenderer) Size() TermSize {127 var w, h int
39 unmodified lines
modified:src/tui/light.go+3 added-4 removed
6 unmodified lines
7 "os"8 "regexp"9 "strconv"10 "strings"11 "sync"12 "time"13 "unicode/utf8"1415 "github.com/junegunn/fzf/src/util"
79 unmodified lines
95}9697// Light renderer98type LightRenderer struct {98 closed *util.AtomicBool99 theme *ColorTheme100 mouse bool101 forceBlack bool102 clearOnExit bool
16 unmodified lines
119 maxHeightFunc func(int) int120 showCursor bool121122 // Windows only123 mutex sync.Mutex124 ttyinChannel chan byte125 inHandle uintptr126 outHandle uintptr127 origStateInput uint32
23 unmodified lines
151 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 lines
774 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}781782func (r *LightRenderer) Top() int {783 return r.yoffset
475 unmodified lines