You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

color_console_windows.go 1.1KB

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