code.gitea.io/gitea@v1.19.3/modules/log/console_windows.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package log
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/mattn/go-isatty"
    10  	"golang.org/x/sys/windows"
    11  )
    12  
    13  func enableVTMode(console windows.Handle) bool {
    14  	mode := uint32(0)
    15  	err := windows.GetConsoleMode(console, &mode)
    16  	if err != nil {
    17  		return false
    18  	}
    19  
    20  	// EnableVirtualTerminalProcessing is the console mode to allow ANSI code
    21  	// interpretation on the console. See:
    22  	// https://docs.microsoft.com/en-us/windows/console/setconsolemode
    23  	// It only works on windows 10. Earlier terminals will fail with an err which we will
    24  	// handle to say don't color
    25  	mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
    26  	err = windows.SetConsoleMode(console, mode)
    27  	return err == nil
    28  }
    29  
    30  func init() {
    31  	if isatty.IsTerminal(os.Stdout.Fd()) {
    32  		CanColorStdout = enableVTMode(windows.Stdout)
    33  	} else {
    34  		CanColorStdout = isatty.IsCygwinTerminal(os.Stderr.Fd())
    35  	}
    36  
    37  	if isatty.IsTerminal(os.Stderr.Fd()) {
    38  		CanColorStderr = enableVTMode(windows.Stderr)
    39  	} else {
    40  		CanColorStderr = isatty.IsCygwinTerminal(os.Stderr.Fd())
    41  	}
    42  }