Debian Codegraph @

src/tui/light_windows.go

raw
1 //go:build windows
2
3 package tui
4
5 import (
6 "os"
7 "syscall"
8 "time"
9
10 "github.com/junegunn/fzf/src/util"
11 "golang.org/x/sys/windows"
12 )
13
14 const (
15 timeoutInterval = 10
16 )
17
18 var (
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 )
23
24 // IsLightRendererSupported checks to see if the Light renderer is supported
25 func IsLightRendererSupported() bool {
26 var oldState uint32
27 // 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 false
30 }
31 // attempt to set mode to determine if we support VT 100 codes. This will work on newer Windows 10
32 // version:
33 canSetVt100 := windows.SetConsoleMode(windows.Stderr, oldState|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) == nil
34 var checkState uint32
35 if windows.GetConsoleMode(windows.Stderr, &checkState) != nil ||
36 (checkState&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) != windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING {
37 return false
38 }
39 windows.SetConsoleMode(windows.Stderr, oldState)
40 return canSetVt100
41 }
42
43 func (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 Default16
47 }
48 return Dark256
49 }
50
51 func (r *LightRenderer) initPlatform() error {
52 //outHandle := windows.Stdout
53 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 err
57 }
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 err
62 }
63 r.inHandle = uintptr(inHandle)
64
65 // channel for non-blocking reads. Buffer to make sure
66 // we get the ESC sets:
67 r.ttyinChannel = make(chan byte, 1024)
68
69 r.setupTerminal()
70
71 return nil
72 }
73
74 func (r *LightRenderer) closePlatform() {
75 windows.SetConsoleMode(windows.Handle(r.outHandle), r.origStateOutput)
76 windows.SetConsoleMode(windows.Handle(r.inHandle), r.origStateInput)
77 }
78
79 func openTtyIn(ttyDefault string) (*os.File, error) {
80 // not used
81 return nil, nil
82 }
83
84 func openTtyOut(ttyDefault string) (*os.File, error) {
85 return os.Stderr, nil
86 }
87
88 func (r *LightRenderer) setupTerminal() {
89 windows.SetConsoleMode(windows.Handle(r.outHandle), consoleFlagsOutput)
90 windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)
91
92 // The following allows for non-blocking IO.
93 // syscall.SetNonblock() is a NOOP under Windows.
94 current := counter
95 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 renderer
102 // has been closed or paused.
103 if current != counter {
104 r.mutex.Unlock()
105 break
106 }
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 }
115
116 func (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 }
125
126 func (r *LightRenderer) Size() TermSize {
127 var w, h int
128 var bufferInfo windows.ConsoleScreenBufferInfo
129 if err := windows.GetConsoleScreenBufferInfo(windows.Handle(r.outHandle), &bufferInfo); err != nil {
130 w = getEnv("COLUMNS", defaultWidth)
131 h = r.maxHeightFunc(getEnv("LINES", defaultHeight))
132
133 } 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 }
139
140 func (r *LightRenderer) updateTerminalSize() {
141 size := r.Size()
142 r.width = size.Columns
143 r.height = size.Lines
144 }
145
146 func (r *LightRenderer) findOffset() (row int, col int) {
147 var bufferInfo windows.ConsoleScreenBufferInfo
148 if err := windows.GetConsoleScreenBufferInfo(windows.Handle(r.outHandle), &bufferInfo); err != nil {
149 return -1, -1
150 }
151 return int(bufferInfo.CursorPosition.Y), int(bufferInfo.CursorPosition.X)
152 }
153
154 // The console API answers for the cursor, and there is no reply to parse for
155 // bracketed paste, so fzf keeps disabling the mode on exit here.
156 func (r *LightRenderer) queryStartup() (row int, col int, pasteWasSet *bool) {
157 row, col = r.findOffset()
158 return
159 }
160
161 func (r *LightRenderer) getch(cancellable bool, nonblock bool) (int, getCharResult) {
162 if !nonblock && !cancellable {
163 bc := <-r.ttyinChannel
164 return int(bc), getCharSuccess
165 }
166
167 var timeout <-chan time.Time
168 if nonblock {
169 timeout = time.After(timeoutInterval * time.Millisecond)
170 }
171
172 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 }
180
181 select {
182 case bc := <-r.ttyinChannel:
183 return int(bc), getCharSuccess
184 case <-cancel:
185 return 0, getCharCancelled
186 case <-timeout:
187 // NOTE: not really an error
188 return 0, getCharError
189 }
190 }