github.com/safing/portbase@v0.19.5/utils/osdetail/colors_windows.go (about) 1 package osdetail 2 3 import ( 4 "sync" 5 6 "golang.org/x/sys/windows" 7 ) 8 9 var ( 10 colorSupport bool 11 12 colorSupportChecked bool 13 checkingColorSupport sync.Mutex 14 ) 15 16 // EnableColorSupport tries to enable color support for cmd on windows and returns whether it is enabled. 17 func EnableColorSupport() bool { 18 checkingColorSupport.Lock() 19 defer checkingColorSupport.Unlock() 20 21 if !colorSupportChecked { 22 colorSupport = enableColorSupport() 23 colorSupportChecked = true 24 } 25 return colorSupport 26 } 27 28 func enableColorSupport() bool { 29 if IsAtLeastWindowsNTVersionWithDefault("10", false) { 30 31 // check if windows.Stdout is file 32 if windows.GetFileInformationByHandle(windows.Stdout, &windows.ByHandleFileInformation{}) == nil { 33 return false 34 } 35 36 var mode uint32 37 err := windows.GetConsoleMode(windows.Stdout, &mode) 38 if err == nil { 39 if mode&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 { 40 mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING 41 err = windows.SetConsoleMode(windows.Stdout, mode) 42 if err != nil { 43 return false 44 } 45 } 46 return true 47 } 48 } 49 50 return false 51 }