Enable ANSI for Windows consoles

This commit is contained in:
Christian Muehlhaeuser 2020-01-09 11:31:13 +01:00
parent 225f822a36
commit a358e8ceca
2 changed files with 24 additions and 0 deletions

23
console_windows.go Normal file
View file

@ -0,0 +1,23 @@
// +build windows
package main
import (
"os"
"golang.org/x/sys/windows"
)
// enableAnsiColors enables support for ANSI color sequences in Windows
// default console. Note that this only works with Windows 10.
func enableAnsiColors() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}
func init() {
enableAnsiColors()
}

1
go.mod
View file

@ -8,4 +8,5 @@ require (
github.com/mattn/go-isatty v0.0.4 github.com/mattn/go-isatty v0.0.4
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v0.0.5
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
) )