Skip to content

Commit

Permalink
Ensure proper ESC seq handling under Windows preview mode (junegunn#2430
Browse files Browse the repository at this point in the history
)

- Increase go routine buffer size
- Add time wait for nonblock getchr()
- Resolve junegunn#2429
  • Loading branch information
kelleyma49 authored Apr 4, 2021
1 parent 764316a commit c8cd94a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/tui/light_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ package tui
import (
"os"
"syscall"
"time"

"github.com/junegunn/fzf/src/util"
"golang.org/x/sys/windows"
)

const (
timeoutInterval = 10
)

var (
consoleFlagsInput = uint32(windows.ENABLE_VIRTUAL_TERMINAL_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_EXTENDED_FLAGS)
consoleFlagsOutput = uint32(windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING | windows.ENABLE_PROCESSED_OUTPUT | windows.DISABLE_NEWLINE_AUTO_RETURN)
Expand Down Expand Up @@ -60,17 +65,14 @@ func (r *LightRenderer) initPlatform() error {

// channel for non-blocking reads. Buffer to make sure
// we get the ESC sets:
r.ttyinChannel = make(chan byte, 12)
r.ttyinChannel = make(chan byte, 1024)

// the following allows for non-blocking IO.
// syscall.SetNonblock() is a NOOP under Windows.
go func() {
fd := int(r.inHandle)
b := make([]byte, 1)
for {
// HACK: if run from PSReadline, something resets ConsoleMode to remove ENABLE_VIRTUAL_TERMINAL_INPUT.
_ = windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)

_, err := util.Read(fd, b)
if err == nil {
r.ttyinChannel <- b[0]
Expand Down Expand Up @@ -130,7 +132,7 @@ func (r *LightRenderer) getch(nonblock bool) (int, bool) {
select {
case bc := <-r.ttyinChannel:
return int(bc), true
default:
case <-time.After(timeoutInterval * time.Millisecond):
return 0, false
}
} else {
Expand Down

0 comments on commit c8cd94a

Please sign in to comment.