From a358e8ceca66705b362dbd0e7a2eaf9b1d4df21d Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 9 Jan 2020 11:31:13 +0100 Subject: [PATCH] Enable ANSI for Windows consoles --- console_windows.go | 23 +++++++++++++++++++++++ go.mod | 1 + 2 files changed, 24 insertions(+) create mode 100644 console_windows.go diff --git a/console_windows.go b/console_windows.go new file mode 100644 index 0000000..2516b16 --- /dev/null +++ b/console_windows.go @@ -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() +} diff --git a/go.mod b/go.mod index 8e17d0a..d198a29 100644 --- a/go.mod +++ b/go.mod @@ -8,4 +8,5 @@ require ( github.com/mattn/go-isatty v0.0.4 github.com/spf13/cobra v0.0.5 golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect + golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a )