Home >Backend Development >Golang >Why Aren\'t My ANSI Colors Working on Windows 10?
Ansi Colors Not Functioning on Windows 10: A Fix
In recent developments, Windows 10 has introduced a console that allows for color printing via Ansi escape sequences. However, some users have encountered an issue where these colors fail to display correctly on their laptops.
One such user, using a binary on Windows 10.0.14393, observed that Ansi color printing worked flawlessly in a nodejs app but not in their own application. Despite deleting the registry key and shortcut associated with the console and running the binary as a different user, the issue persisted.
Solution: Enable Virtual Terminal Processing
The key to resolving this issue lies in enabling Virtual Terminal Processing (VTP) on Windows, which is required since a recent update. The following steps can be followed:
Create an Initialization File (init_windows.go):
Add the following code to a file named init_windows.go:
package main import ( "os" "golang.org/x/sys/windows" ) func init() { stdout := windows.Handle(os.Stdout.Fd()) var originalMode uint32 windows.GetConsoleMode(stdout, &originalMode) windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) }
Note: This solution was adapted from this issue thread: [https://github.com/sirupsen/logrus/issues/172#issuecomment-353724264](https://github.com/sirupsen/logrus/issues/172#issuecomment-353724264)
The above is the detailed content of Why Aren\'t My ANSI Colors Working on Windows 10?. For more information, please follow other related articles on the PHP Chinese website!