Skip to content
Myr

light_windows.go

This file

Path

src/tui/light_windows.go

190 lines · 5.8 KiB · Go

Last changed by

Restore bracketed paste mode instead of forcing it offJunegunn Choibefore adoptiondab626b

History of this file

Take it

1//go:build windows23package tui45import (6	"os"7	"syscall"8	"time"910	"github.com/junegunn/fzf/src/util"11	"golang.org/x/sys/windows"12)1314const (15	timeoutInterval = 1016)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 {26	var oldState uint3227	// enable vt100 emulation (https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences)28	if windows.GetConsoleMode(windows.Stderr, &oldState) != nil {29		return false30	}31	// attempt to set mode to determine if we support VT 100 codes. This will work on newer Windows 1032	// version:33	canSetVt100 := windows.SetConsoleMode(windows.Stderr, oldState|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) == nil34	var checkState uint3235	if windows.GetConsoleMode(windows.Stderr, &checkState) != nil ||36		(checkState&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) != windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING {37		return false38	}39	windows.SetConsoleMode(windows.Stderr, oldState)40	return canSetVt10041}4243func (r *LightRenderer) DefaultTheme() *ColorTheme {44	// the getenv check is borrowed from here: https://github.com/gdamore/tcell/commit/0c473b86d82f68226a142e96cc5a34c5a29b3690#diff-b008fcd5e6934bf31bc3d33bf49f47d8R178:45	if !IsLightRendererSupported() || os.Getenv("ConEmuPID") != "" || os.Getenv("TCELL_TRUECOLOR") == "disable" {46		return Default1647	}48	return Dark25649}5051func (r *LightRenderer) initPlatform() error {52	//outHandle := windows.Stdout53	outHandle, _ := syscall.Open("CONOUT$", syscall.O_RDWR, 0)54	// enable vt100 emulation (https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences)55	if err := windows.GetConsoleMode(windows.Handle(outHandle), &r.origStateOutput); err != nil {56		return err57	}58	r.outHandle = uintptr(outHandle)59	inHandle, _ := syscall.Open("CONIN$", syscall.O_RDWR, 0)60	if err := windows.GetConsoleMode(windows.Handle(inHandle), &r.origStateInput); err != nil {61		return err62	}63	r.inHandle = uintptr(inHandle)6465	// channel for non-blocking reads. Buffer to make sure66	// we get the ESC sets:67	r.ttyinChannel = make(chan byte, 1024)6869	r.setupTerminal()7071	return nil72}7374func (r *LightRenderer) closePlatform() {75	windows.SetConsoleMode(windows.Handle(r.outHandle), r.origStateOutput)76	windows.SetConsoleMode(windows.Handle(r.inHandle), r.origStateInput)77}7879func openTtyIn(ttyDefault string) (*os.File, error) {80	// not used81	return nil, nil82}8384func openTtyOut(ttyDefault string) (*os.File, error) {85	return os.Stderr, nil86}8788func (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}115116func (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 int128	var bufferInfo windows.ConsoleScreenBufferInfo129	if err := windows.GetConsoleScreenBufferInfo(windows.Handle(r.outHandle), &bufferInfo); err != nil {130		w = getEnv("COLUMNS", defaultWidth)131		h = r.maxHeightFunc(getEnv("LINES", defaultHeight))132133	} else {134		w = int(bufferInfo.Window.Right - bufferInfo.Window.Left)135		h = r.maxHeightFunc(int(bufferInfo.Window.Bottom - bufferInfo.Window.Top))136	}137	return TermSize{h, w, 0, 0}138}139140func (r *LightRenderer) updateTerminalSize() {141	size := r.Size()142	r.width = size.Columns143	r.height = size.Lines144}145146func (r *LightRenderer) findOffset() (row int, col int) {147	var bufferInfo windows.ConsoleScreenBufferInfo148	if err := windows.GetConsoleScreenBufferInfo(windows.Handle(r.outHandle), &bufferInfo); err != nil {149		return -1, -1150	}151	return int(bufferInfo.CursorPosition.Y), int(bufferInfo.CursorPosition.X)152}153154// The console API answers for the cursor, and there is no reply to parse for155// bracketed paste, so fzf keeps disabling the mode on exit here.156func (r *LightRenderer) queryStartup() (row int, col int, pasteWasSet *bool) {157	row, col = r.findOffset()158	return159}160161func (r *LightRenderer) getch(cancellable bool, nonblock bool) (int, getCharResult) {162	if !nonblock && !cancellable {163		bc := <-r.ttyinChannel164		return int(bc), getCharSuccess165	}166167	var timeout <-chan time.Time168	if nonblock {169		timeout = time.After(timeoutInterval * time.Millisecond)170	}171172	var cancel chan struct{}173	if cancellable {174		cancel = make(chan struct{})175		r.setCancel(func() {176			close(cancel)177		})178		defer r.setCancel(nil)179	}180181	select {182	case bc := <-r.ttyinChannel:183		return int(bc), getCharSuccess184	case <-cancel:185		return 0, getCharCancelled186	case <-timeout:187		// NOTE: not really an error188		return 0, getCharError189	}190}