Skip to content
Myr

terminal_windows.go

This file

Path

src/terminal_windows.go

62 lines · 1.3 KiB · Go

Last changed by

Terminate resize notification when Loop exitsJunegunn Choibefore adoptiondc384af

History of this file

Take it

1//go:build windows23package fzf45import (6	"context"7	"os"8	"syscall"9	"time"1011	"golang.org/x/sys/windows"12)1314const resizePollInterval = 100 * time.Millisecond1516type resizeSignal struct{}1718func (resizeSignal) String() string { return "resize" }19func (resizeSignal) Signal()        {}2021// Windows has no SIGWINCH, so poll the console screen buffer for window22// size changes instead.23func notifyOnResize(ctx context.Context, resizeChan chan<- os.Signal) {24	consoleOut, err := syscall.Open("CONOUT$", syscall.O_RDWR, 0)25	if err != nil {26		return27	}28	var info windows.ConsoleScreenBufferInfo29	if windows.GetConsoleScreenBufferInfo(windows.Handle(consoleOut), &info) != nil {30		syscall.Close(consoleOut)31		return32	}33	last := info.Window34	go func() {35		defer syscall.Close(consoleOut)36		ticker := time.NewTicker(resizePollInterval)37		defer ticker.Stop()38		for {39			select {40			case <-ctx.Done():41				return42			case <-ticker.C:43			}44			if windows.GetConsoleScreenBufferInfo(windows.Handle(consoleOut), &info) != nil {45				continue46			}47			current := info.Window48			if current.Right-current.Left != last.Right-last.Left ||49				current.Bottom-current.Top != last.Bottom-last.Top {50				last = current51				select {52				case resizeChan <- resizeSignal{}:53				default:54				}55			}56		}57	}()58}5960func notifyStop(p *os.Process) {61	// NOOP62}