From 71b3e450a10cbfaad585bebf372810143ea485ee Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 25 Oct 2020 03:32:59 +0100 Subject: [PATCH] Improve support for Windows platforms This enables ANSI support on older Windows versions and ensures correct rendering when using the classic command prompt. --- console_windows.go | 23 +++++++++++++++++++++++ 1 file changed, 23 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() +}