Home  >  Article  >  Backend Development  >  Why are ANSI Colors Not Working on My Windows 10 Laptop?

Why are ANSI Colors Not Working on My Windows 10 Laptop?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-27 05:43:02766browse

Why are ANSI Colors Not Working on My Windows 10 Laptop?

ANSI Colors not Working on Windows 10: Resolving the Issue

Console output with color is a recent addition to Windows 10, bringing excitement to developers. However, some users have encountered issues where it suddenly stops working, specifically on certain laptops with Windows 10.0.14393.

Cause and Solution

The culprit behind this problem is the absence of virtual terminal processing in Windows. This feature must be enabled to allow for correct display of ANSI color codes.

To enable virtual terminal processing, follow these steps:

  1. In the registry, navigate to HKLMConsole and delete the entire key.
  2. Create a file named init_windows.go with the following code:

    <code class="go">// +build windows
    
    package main
    
    import (
        "os"
    
        "golang.org/x/sys/windows"
    )
    
    func init() {
        stdout := windows.Handle(os.Stdout.Fd())
        var originalMode uint32
    
        windows.GetConsoleMode(stdout, &amp;originalMode)
        windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
    }</code>
  3. Save the file and build your application.

This code sets virtual terminal processing in Windows without conflicting with other operating systems.

Additional Notes

Restarting the application, deleting user settings, or creating new users will not resolve the issue. Deleting the registry key must be followed by enabling virtual terminal processing using the code provided.

Credits

The solution presented in this answer is adapted from the GitHub issue at https://github.com/sirupsen/logrus/issues/172#issuecomment-353724264.

The above is the detailed content of Why are ANSI Colors Not Working on My Windows 10 Laptop?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn