Repository: fatih/color (language: go)

Issue:
Colors not working in Powershell env
Using the Cluster API CLI clusterctl prints the output with color on linux and mac environments, but the same does not work for Windows PowerShell and cmd. The output includes the Raw ANSI codes instead making it unreadable. The current workaround is to set the `NO_COLOR` env variable to turn off color completely, resulting in a readable, but colorless output.

 Is there a way to configure the library to get the correctly colored output on Windows environments? 

Related issue: https://github.com/kubernetes-sigs/cluster-api/issues/7381


Discussion / hints:
Are you sure that it is a problem with this library? I am working on Windows and never encountered any problems. I am using this library in https://github.com/goyek/x and I have encountered no issues. Maybe some accurate repro steps? 😉 
I reproduced this problem when `powershell` is called in `conhost.exe`. I am using Windows Terminal and it works OK. As far as I remember on PowerShell Prompt it works fine as well. https://github.com/mattn/go-colorable works on `conhost.exe` as well. 

So probably the problem is that it is PowerShell inside conhost. Can you confirm? Maybe it is a problem in "PowerShell coloring" design?
It is my understanding that for Windows to properly work with ansi code colors, the application need to signal color awareness thru api:s. If not, color support might be randomly broken between different shells.

fatih/color does not do this setup on Windows, resulting in the same app looking fine in the Powershell Shell or Windows Terminal because they do this setup for every process, but ansi escape sequences are printed while in msys-bash (alongside other apps that properly outputs color by opting in).

The essence of the issue is that in Windows 10 an application need to "opt in" for ansi code support by calling SetConsoleMode() with the ENABLE_VIRTUAL_TERMINAL_PROCESSING bit set.

Here is a better explanation than mine: https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#output-sequences

I write all of this by experience from previously debugging a similar issue in a [rust crate](https://github.com/Peltoche/lsd/issues/657)
Sent a PR which fixes the issue on my end.

With version v1.14.1 of this go package, im still experiencing this issue

![image](https://user-images.githubusercontent.com/34643249/220962683-42c506f1-92a6-4670-8f39-0b54ecfc4023.png)

@GiulianoDecesares this issue is still open, the PR #173 is not merged.

It seems that I accidentally closed the PR, i have reopened it now.
@GiulianoDecesares can you try out @martinlindhe's fix here https://github.com/fatih/color/pull/173 ?
Yes, Is there any practical way to test this or I just have to download the entire repository and manually replace the references in my code? (I don't know, maybe I can `go get` the branch in go or something like that) 🤔 @fatih @martinlindhe 

@GiulianoDecesares I would try running `go get github.com/fatih/color@0b9be245c16cc7838ed7c25db2ec39c6eaa55fbe`
@pellared yep! Already tried, but go takes `0b9be245c16cc7838ed7c25db2ec39c6eaa55fbe` this as a version and throws an `unknown version` error
Then probably the easiest way is to

1. clone the repository
2. make the same fix locally
3. use `replace` in `go.mod` . more: https://go.dev/ref/mod#go-mod-file-replace
Thanks @pellared for the info on the `replace` in `go.mod`. This fix seems to be working fine. Y was able to reproduce it in CMD and then with the `replace` to a local repo with the same changes, it stops happening. @martinlindhe @fatih 

Fix patch:
diff --git a/color_windows.go b/color_windows.go
new file mode 100644
index 0000000..3db9559
--- /dev/null
+++ b/color_windows.go
@@ -0,0 +1,18 @@
+package color
+
+import (
+	"os"
+
+	"golang.org/x/sys/windows"
+)
+
+func init() {
+	// Opt-in for ansi color support for current process.
+	// https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#output-sequences
+	var outMode uint32
+	out := windows.Handle(os.Stdout.Fd())
+	if err := windows.GetConsoleMode(out, &outMode); err == nil {
+		outMode |= windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
+		_ = windows.SetConsoleMode(out, outMode)
+	}
+}
diff --git a/go.mod b/go.mod
index d918d79..4b815b9 100644
--- a/go.mod
+++ b/go.mod
@@ -7,4 +7,4 @@ require (
 	github.com/mattn/go-isatty v0.0.17
 )
 
-require golang.org/x/sys v0.3.0 // indirect
+require golang.org/x/sys v0.5.0
diff --git a/go.sum b/go.sum
index d250931..db58a32 100644
--- a/go.sum
+++ b/go.sum
@@ -4,5 +4,5 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
 github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
 github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
-golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=


Test patch: