ansi_windows.go

 1//go:build windows
 2// +build windows
 3
 4package lipgloss
 5
 6import (
 7	"os"
 8
 9	"golang.org/x/sys/windows"
10)
11
12// EnableLegacyWindowsANSI enables support for ANSI color sequences in the
13// Windows default console (cmd.exe and the PowerShell application). Note that
14// this only works with Windows 10 and greater. Also note that Windows Terminal
15// supports colors by default.
16func EnableLegacyWindowsANSI(f *os.File) {
17	var mode uint32
18	handle := windows.Handle(f.Fd())
19	err := windows.GetConsoleMode(handle, &mode)
20	if err != nil {
21		return
22	}
23
24	// See https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
25	if mode&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING {
26		vtpmode := mode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
27		if err := windows.SetConsoleMode(handle, vtpmode); err != nil {
28			return
29		}
30	}
31}