From 1085c27715661b1eeb151489699f5de5213ab0a2 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 2 Feb 2022 13:38:38 +0100 Subject: [PATCH] Add Windows color support detection --- termenv_windows.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/termenv_windows.go b/termenv_windows.go index 6cd11fc..7f5b1ec 100644 --- a/termenv_windows.go +++ b/termenv_windows.go @@ -4,10 +4,38 @@ package termenv import ( + "os" + "strconv" + "golang.org/x/sys/windows" ) func colorProfile() Profile { + if os.Getenv("ConEmuANSI") == "ON" { + return TrueColor + } + + winVersion, _, buildNumber := windows.RtlGetNtVersionNumbers() + if buildNumber < 10586 || winVersion < 10 { + // No ANSI support before Windows 10 build 10586. + if os.Getenv("ANSICON") != "" { + conVersion := os.Getenv("ANSICON_VER") + cv, err := strconv.ParseInt(conVersion, 10, 64) + if err != nil || cv < 181 { + // No 8 bit color support before v1.81 release. + return ANSI + } + + return ANSI256 + } + + return Ascii + } + if buildNumber < 14931 { + // No true color support before build 14931. + return ANSI256 + } + return TrueColor }