Ansi colours on Windows 10, sort of not working

后端 未结 1 439

Console with colours is fairly new and exciting for Windows :-)

I wrote some programs that printed using the ansi colour escapes and all was good ...

1条回答
  •  抹茶落季
    2021-01-13 10:37

    You have to enable virtual terminal processing on Windows since that update.

    I usually add a file init_windows.go which sets this in windows but is also compatible with other OS:

    // +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, &originalMode)
        windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
    }
    

    as copied from: https://github.com/sirupsen/logrus/issues/172#issuecomment-353724264

    0 讨论(0)
提交回复
热议问题