mirror of
https://github.com/junegunn/fzf.git
synced 2026-08-12 19:07:00 +08:00
Detect terminal resize on Windows (fix #4790)
Windows has no SIGWINCH and notifyOnResize was an empty TODO, so fzf running in --height mode never noticed console size changes. Poll the console screen buffer dimensions every 100ms and push a signal through the same channel SIGWINCH uses on Unix. Full-screen mode is unaffected as it goes through tcell which emits its own resize events.
This commit is contained in:
+39
-1
@@ -4,10 +4,48 @@ package fzf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const resizePollInterval = 100 * time.Millisecond
|
||||||
|
|
||||||
|
type resizeSignal struct{}
|
||||||
|
|
||||||
|
func (resizeSignal) String() string { return "resize" }
|
||||||
|
func (resizeSignal) Signal() {}
|
||||||
|
|
||||||
|
// Windows has no SIGWINCH, so poll the console screen buffer for window
|
||||||
|
// size changes instead.
|
||||||
func notifyOnResize(resizeChan chan<- os.Signal) {
|
func notifyOnResize(resizeChan chan<- os.Signal) {
|
||||||
// TODO
|
consoleOut, err := syscall.Open("CONOUT$", syscall.O_RDWR, 0)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var info windows.ConsoleScreenBufferInfo
|
||||||
|
if windows.GetConsoleScreenBufferInfo(windows.Handle(consoleOut), &info) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
last := info.Window
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
time.Sleep(resizePollInterval)
|
||||||
|
if windows.GetConsoleScreenBufferInfo(windows.Handle(consoleOut), &info) != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
current := info.Window
|
||||||
|
if current.Right-current.Left != last.Right-last.Left ||
|
||||||
|
current.Bottom-current.Top != last.Bottom-last.Top {
|
||||||
|
last = current
|
||||||
|
select {
|
||||||
|
case resizeChan <- resizeSignal{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func notifyStop(p *os.Process) {
|
func notifyStop(p *os.Process) {
|
||||||
|
|||||||
Reference in New Issue
Block a user