Make height option work under Windows (#1341)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Separate Unix & Windows code into platform specific files for light renderer
authored by Michael Kelley · committed by Junegunn Choi · parent 7c40a42 · Browse files · 5 files·+244 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+244 added−69 removed
- other:+244 added−69 removed
Provenance
- Commit
7d5985b- Tree
5b67c8a- Author
Michael Kelley <michael.a.kelley@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
7c40a42
5 files changed+244 added−69 removed
added:src/tui/light_unix.go+97 added
1+// +build !windows2+3+package tui4+5+import (6+ "fmt"7+ "os"8+ "syscall"9+10+ "github.com/junegunn/fzf/src/util"11+ "golang.org/x/crypto/ssh/terminal"12+)13+14+func IsLightRendererSupported() bool {15+ return true16+}17+18+func (r *LightRenderer) fd() int {19+ return int(r.ttyin.Fd())20+}21+22+func (r *LightRenderer) initPlatform() error {23+ fd := r.fd()24+ origState, err := terminal.GetState(fd)25+ if err != nil {26+ return err27+ }28+ r.origState = origState29+ terminal.MakeRaw(fd)30+ return nil31+}32+33+func (r *LightRenderer) closePlatform() {34+ // NOOP35+}36+37+func openTtyIn() *os.File {38+ in, err := os.OpenFile(consoleDevice, syscall.O_RDONLY, 0)39+ if err != nil {40+ tty := ttyname()41+ if len(tty) > 0 {42+ if in, err := os.OpenFile(tty, syscall.O_RDONLY, 0); err == nil {43+ return in44+ }45+ }46+ fmt.Fprintln(os.Stderr, "Failed to open "+consoleDevice)47+ os.Exit(2)48+ }49+ return in50+}51+52+func (r *LightRenderer) setupTerminal() {53+ terminal.MakeRaw(r.fd())54+}55+56+func (r *LightRenderer) restoreTerminal() {57+ terminal.Restore(r.fd(), r.origState)58+}59+60+func (r *LightRenderer) updateTerminalSize() {61+ width, height, err := terminal.GetSize(r.fd())62+63+ if err == nil {64+ r.width = width65+ r.height = r.maxHeightFunc(height)66+ } else {67+ r.width = getEnv("COLUMNS", defaultWidth)68+ r.height = r.maxHeightFunc(getEnv("LINES", defaultHeight))69+ }70+}71+72+func (r *LightRenderer) findOffset() (row int, col int) {73+ r.csi("6n")74+ r.flush()75+ bytes := []byte{}76+ for tries := 0; tries < offsetPollTries; tries++ {77+ bytes = r.getBytesInternal(bytes, tries > 0)78+ offsets := offsetRegexp.FindSubmatch(bytes)79+ if len(offsets) > 3 {80+ // Add anything we skipped over to the input buffer81+ r.buffer = append(r.buffer, offsets[1]...)82+ return atoi(string(offsets[2]), 0) - 1, atoi(string(offsets[3]), 0) - 183+ }84+ }85+ return -1, -186+}87+88+func (r *LightRenderer) getch(nonblock bool) (int, bool) {89+ b := make([]byte, 1)90+ fd := r.fd()91+ util.SetNonblock(r.ttyin, nonblock)92+ _, err := util.Read(fd, b)93+ if err != nil {94+ return 0, false95+ }96+ return int(b[0]), true97+}added:src/tui/light_windows.go+132 added
1+//+build windows2+3+package tui4+5+import (6+ "os"7+ "syscall"8+9+ "github.com/junegunn/fzf/src/util"10+ "golang.org/x/sys/windows"11+)12+13+var (14+ consoleFlagsInput = uint32(windows.ENABLE_VIRTUAL_TERMINAL_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_EXTENDED_FLAGS)15+ consoleFlagsOutput = uint32(windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING | windows.ENABLE_PROCESSED_OUTPUT | windows.DISABLE_NEWLINE_AUTO_RETURN)16+)17+18+// IsLightRendererSupported checks to see if the Light renderer is supported19+func IsLightRendererSupported() bool {20+ var oldState uint3221+ // enable vt100 emulation (https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences)22+ if windows.GetConsoleMode(windows.Stderr, &oldState) != nil {23+ return false24+ }25+ // attempt to set mode to determine if we support VT 100 codes. This will work on newer Windows 1026+ // version:27+ canSetVt100 := windows.SetConsoleMode(windows.Stderr, oldState|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) == nil28+ var checkState uint3229+ if windows.GetConsoleMode(windows.Stderr, &checkState) != nil ||30+ (checkState&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) != windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING {31+ return false32+ }33+ windows.SetConsoleMode(windows.Stderr, oldState)34+ return canSetVt10035+}36+37+func (r *LightRenderer) initPlatform() error {38+ //outHandle := windows.Stdout39+ outHandle, _ := syscall.Open("CONOUT$", syscall.O_RDWR, 0)40+ // enable vt100 emulation (https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences)41+ if err := windows.GetConsoleMode(windows.Handle(outHandle), &r.origStateOutput); err != nil {42+ return err43+ }44+ r.outHandle = uintptr(outHandle)45+ inHandle, _ := syscall.Open("CONIN$", syscall.O_RDWR, 0)46+ if err := windows.GetConsoleMode(windows.Handle(inHandle), &r.origStateInput); err != nil {47+ return err48+ }49+ r.inHandle = uintptr(inHandle)50+51+ r.setupTerminal()52+53+ // channel for non-blocking reads. Buffer to make sure54+ // we get the ESC sets:55+ r.ttyinChannel = make(chan byte, 12)56+57+ // the following allows for non-blocking IO.58+ // syscall.SetNonblock() is a NOOP under Windows.59+ go func() {60+ fd := int(r.inHandle)61+ b := make([]byte, 1)62+ for {63+ // HACK: if run from PSReadline, something resets ConsoleMode to remove ENABLE_VIRTUAL_TERMINAL_INPUT.64+ _ = windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)65+66+ _, err := util.Read(fd, b)67+ if err == nil {68+ r.ttyinChannel <- b[0]69+ }70+ }71+ }()72+73+ return nil74+}75+76+func (r *LightRenderer) closePlatform() {77+ windows.SetConsoleMode(windows.Handle(r.outHandle), r.origStateOutput)78+ windows.SetConsoleMode(windows.Handle(r.inHandle), r.origStateInput)79+}80+81+func openTtyIn() *os.File {82+ // not used83+ return nil84+}85+86+func (r *LightRenderer) setupTerminal() error {87+ if err := windows.SetConsoleMode(windows.Handle(r.outHandle), consoleFlagsOutput); err != nil {88+ return err89+ }90+ return windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)91+}92+93+func (r *LightRenderer) restoreTerminal() error {94+ if err := windows.SetConsoleMode(windows.Handle(r.inHandle), r.origStateInput); err != nil {95+ return err96+ }97+ return windows.SetConsoleMode(windows.Handle(r.outHandle), r.origStateOutput)98+}99+100+func (r *LightRenderer) updateTerminalSize() {101+ var bufferInfo windows.ConsoleScreenBufferInfo102+ if err := windows.GetConsoleScreenBufferInfo(windows.Handle(r.outHandle), &bufferInfo); err != nil {103+ r.width = getEnv("COLUMNS", defaultWidth)104+ r.height = r.maxHeightFunc(getEnv("LINES", defaultHeight))105+106+ } else {107+ r.width = int(bufferInfo.Window.Right - bufferInfo.Window.Left)108+ r.height = r.maxHeightFunc(int(bufferInfo.Window.Bottom - bufferInfo.Window.Top))109+ }110+}111+112+func (r *LightRenderer) findOffset() (row int, col int) {113+ var bufferInfo windows.ConsoleScreenBufferInfo114+ if err := windows.GetConsoleScreenBufferInfo(windows.Handle(r.outHandle), &bufferInfo); err != nil {115+ return -1, -1116+ }117+ return int(bufferInfo.CursorPosition.X), int(bufferInfo.CursorPosition.Y)118+}119+120+func (r *LightRenderer) getch(nonblock bool) (int, bool) {121+ if nonblock {122+ select {123+ case bc := <-r.ttyinChannel:124+ return int(bc), true125+ default:126+ return 0, false127+ }128+ } else {129+ bc := <-r.ttyinChannel130+ return int(bc), true131+ }132+}modified:src/tui/light.go+12 added-66 removed
5 unmodified lines6 "os/exec"7 "regexp"8 "strconv"9 "strings"10- "syscall"10 "time"11 "unicode/utf8"12 13 "github.com/junegunn/fzf/src/util"14 unmodified lines28 const consoleDevice string = "/dev/tty"29 30 var offsetRegexp *regexp.Regexp = regexp.MustCompile("(.*)\x1b\\[([0-9]+);([0-9]+)R")31 33-func openTtyIn() *os.File {34- in, err := os.OpenFile(consoleDevice, syscall.O_RDONLY, 0)35- if err != nil {36- tty := ttyname()37- if len(tty) > 0 {38- if in, err := os.OpenFile(tty, syscall.O_RDONLY, 0); err == nil {39- return in40- }41- }42- fmt.Fprintln(os.Stderr, "Failed to open "+consoleDevice)43- os.Exit(2)44- }45- return in46-}47-32 func (r *LightRenderer) stderr(str string) {33 r.stderrInternal(str, true)34 }35 48 unmodified lines84 queued string85 y int86 x int87 maxHeightFunc func(int) int88+89+ // Windows only90+ ttyinChannel chan byte91+ inHandle uintptr92+ outHandle uintptr93+ origStateInput uint3294+ origStateOutput uint3295 }96 97 type LightWindow struct {98 renderer *LightRenderer25 unmodified lines124 maxHeightFunc: maxHeightFunc}125 return &r126 }127 137-func (r *LightRenderer) fd() int {138- return int(r.ttyin.Fd())139-}140-128 func (r *LightRenderer) defaultTheme() *ColorTheme {129 if strings.Contains(os.Getenv("TERM"), "256") {130 return Dark256131 }3 unmodified lines135 }136 return Default16137 }138 152-func (r *LightRenderer) findOffset() (row int, col int) {153- r.csi("6n")154- r.flush()155- bytes := []byte{}156- for tries := 0; tries < offsetPollTries; tries++ {157- bytes = r.getBytesInternal(bytes, tries > 0)158- offsets := offsetRegexp.FindSubmatch(bytes)159- if len(offsets) > 3 {160- // add anything we skipped over to the input buffer161- r.buffer = append(r.buffer, offsets[1]...)162- return atoi(string(offsets[2]), 0) - 1, atoi(string(offsets[3]), 0) - 1163- }164- }165- return -1, -1166-}167-139 func repeat(r rune, times int) string {140 if times > 0 {141 return strings.Repeat(string(r), times)142 }10 unmodified lines153 154 func (r *LightRenderer) Init() {155 r.escDelay = atoi(os.Getenv("ESCDELAY"), defaultEscDelay)156 186- fd := r.fd()187- origState, err := terminal.GetState(fd)188- if err != nil {157+ if err := r.initPlatform(); err != nil {158 errorExit(err.Error())159 }191- r.origState = origState192- terminal.MakeRaw(fd)160 r.updateTerminalSize()161 initTheme(r.theme, r.defaultTheme(), r.forceBlack)162 163 if r.fullscreen {64 unmodified lines228 }229 return atoi(env, defaultValue)230 }231 265-func (r *LightRenderer) updateTerminalSize() {266- width, height, err := terminal.GetSize(r.fd())267- if err == nil {268- r.width = width269- r.height = r.maxHeightFunc(height)270- } else {271- r.width = getEnv("COLUMNS", defaultWidth)272- r.height = r.maxHeightFunc(getEnv("LINES", defaultHeight))273- }274-}275-276-func (r *LightRenderer) getch(nonblock bool) (int, bool) {277- b := make([]byte, 1)278- fd := r.fd()279- util.SetNonblock(r.ttyin, nonblock)280- _, err := util.Read(fd, b)281- if err != nil {282- return 0, false283- }284- return int(b[0]), true285-}286-232 func (r *LightRenderer) getBytes() []byte {233 return r.getBytesInternal(r.buffer, false)234 }235 312 unmodified lines548 r.csi("?1049l")549 }550 551 func (r *LightRenderer) Pause(clear bool) {607- terminal.Restore(r.fd(), r.origState)552+ r.restoreTerminal()553 if clear {554 if r.fullscreen {555 r.rmcup()556 } else {4 unmodified lines561 }562 }563 564 func (r *LightRenderer) Resume(clear bool) {620- terminal.MakeRaw(r.fd())565+ r.setupTerminal()566 if clear {567 if r.fullscreen {568 r.smcup()569 } else {45 unmodified lines615 if r.mouse {616 r.csi("?1000l")617 }618 r.flush()674- terminal.Restore(r.fd(), r.origState)619+ r.closePlatform()620+ r.restoreTerminal()621 }622 623 func (r *LightRenderer) MaxX() int {624 return r.width303 unmodified linesmodified:src/options.go+2 added-3 removed
9 unmodified lines10 "unicode/utf8"11 12 "github.com/junegunn/fzf/src/algo"13 "github.com/junegunn/fzf/src/tui"14- "github.com/junegunn/fzf/src/util"14 15 "github.com/mattn/go-runewidth"16 "github.com/mattn/go-shellwords"17 )1392 unmodified lines1410 return nil1411 }1412 1413 func postProcessOptions(opts *Options) {1415- if util.IsWindows() && opts.Height.size > 0 {1416- errorExit("--height option is currently not supported on Windows")1414+ if !tui.IsLightRendererSupported() && opts.Height.size > 0 {1415+ errorExit("--height option is currently not supported on this platform")1416 }1417 // Default actions for CTRL-N / CTRL-P when --history is set1418 if opts.History != nil {1419 if _, prs := opts.Keymap[tui.CtrlP]; !prs {47 unmodified linesmodified:CHANGELOG.md+1 added
7 unmodified lines8 - `--border` will now print border with rounded corners around the9 finder instead of printing horizontal lines above and below it.10 The previous style is available via `--border=horizontal`11 - Added `--pointer` and `--marker` options12+- `--height` option is now available on Windows binary (@kelleyma49)13 - More keys and actions for `--bind`14 - Bug fixes and improvements15 - Vim plugin: Floating windows support16 - bash: Various improvements in key bindings (CTRL-T, CTRL-R, ALT-C)800 unmodified lines