Skip to content
Myr

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

5 files+244 added−69 removed

added:src/tui/light_unix.go+97 added
1// +build !windows23package tui45import (6 "fmt"7 "os"8 "syscall"910 "github.com/junegunn/fzf/src/util"11 "golang.org/x/crypto/ssh/terminal"12)1314func IsLightRendererSupported() bool {15 return true16}1718func (r *LightRenderer) fd() int {19 return int(r.ttyin.Fd())20}2122func (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}3233func (r *LightRenderer) closePlatform() {34 // NOOP35}3637func 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}5152func (r *LightRenderer) setupTerminal() {53 terminal.MakeRaw(r.fd())54}5556func (r *LightRenderer) restoreTerminal() {57 terminal.Restore(r.fd(), r.origState)58}5960func (r *LightRenderer) updateTerminalSize() {61 width, height, err := terminal.GetSize(r.fd())6263 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}7172func (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}8788func (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 windows23package tui45import (6 "os"7 "syscall"89 "github.com/junegunn/fzf/src/util"10 "golang.org/x/sys/windows"11)1213var (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)1718// IsLightRendererSupported checks to see if the Light renderer is supported19func 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}3637func (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)5051 r.setupTerminal()5253 // channel for non-blocking reads. Buffer to make sure54 // we get the ESC sets:55 r.ttyinChannel = make(chan byte, 12)5657 // 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)6566 _, err := util.Read(fd, b)67 if err == nil {68 r.ttyinChannel <- b[0]69 }70 }71 }()7273 return nil74}7576func (r *LightRenderer) closePlatform() {77 windows.SetConsoleMode(windows.Handle(r.outHandle), r.origStateOutput)78 windows.SetConsoleMode(windows.Handle(r.inHandle), r.origStateInput)79}8081func openTtyIn() *os.File {82 // not used83 return nil84}8586func (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}9293func (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}99100func (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))105106 } 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}111112func (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}119120func (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 lines
6 "os/exec"7 "regexp"8 "strconv"9 "strings"10 "syscall"10 "time"11 "unicode/utf8"1213 "github.com/junegunn/fzf/src/util"
14 unmodified lines
28const consoleDevice string = "/dev/tty"2930var offsetRegexp *regexp.Regexp = regexp.MustCompile("(.*)\x1b\\[([0-9]+);([0-9]+)R")3133func 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}4732func (r *LightRenderer) stderr(str string) {33 r.stderrInternal(str, true)34}35
48 unmodified lines
84 queued string85 y int86 x int87 maxHeightFunc func(int) int8889 // Windows only90 ttyinChannel chan byte91 inHandle uintptr92 outHandle uintptr93 origStateInput uint3294 origStateOutput uint3295}9697type LightWindow struct {98 renderer *LightRenderer
25 unmodified lines
124 maxHeightFunc: maxHeightFunc}125 return &r126}127137func (r *LightRenderer) fd() int {138 return int(r.ttyin.Fd())139}140128func (r *LightRenderer) defaultTheme() *ColorTheme {129 if strings.Contains(os.Getenv("TERM"), "256") {130 return Dark256131 }
3 unmodified lines
135 }136 return Default16137}138152func (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}167139func repeat(r rune, times int) string {140 if times > 0 {141 return strings.Repeat(string(r), times)142 }
10 unmodified lines
153154func (r *LightRenderer) Init() {155 r.escDelay = atoi(os.Getenv("ESCDELAY"), defaultEscDelay)156186 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)162163 if r.fullscreen {
64 unmodified lines
228 }229 return atoi(env, defaultValue)230}231265func (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}275276func (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}286232func (r *LightRenderer) getBytes() []byte {233 return r.getBytesInternal(r.buffer, false)234}235
312 unmodified lines
548 r.csi("?1049l")549}550551func (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 lines
561 }562}563564func (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 lines
615 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}622623func (r *LightRenderer) MaxX() int {624 return r.width
303 unmodified lines
modified:src/options.go+2 added-3 removed
9 unmodified lines
10 "unicode/utf8"1112 "github.com/junegunn/fzf/src/algo"13 "github.com/junegunn/fzf/src/tui"14 "github.com/junegunn/fzf/src/util"1415 "github.com/mattn/go-runewidth"16 "github.com/mattn/go-shellwords"17)
1392 unmodified lines
1410 return nil1411}14121413func 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 lines
modified:CHANGELOG.md+1 added
7 unmodified lines
8 - `--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