本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

44 lines
1.1KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package log
  5. import (
  6. "os"
  7. "github.com/mattn/go-isatty"
  8. "golang.org/x/sys/windows"
  9. )
  10. func enableVTMode(console windows.Handle) bool {
  11. mode := uint32(0)
  12. err := windows.GetConsoleMode(console, &mode)
  13. if err != nil {
  14. return false
  15. }
  16. // EnableVirtualTerminalProcessing is the console mode to allow ANSI code
  17. // interpretation on the console. See:
  18. // https://docs.microsoft.com/en-us/windows/console/setconsolemode
  19. // It only works on windows 10. Earlier terminals will fail with an err which we will
  20. // handle to say don't color
  21. mode = mode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
  22. err = windows.SetConsoleMode(console, mode)
  23. return err == nil
  24. }
  25. func init() {
  26. if isatty.IsTerminal(os.Stdout.Fd()) {
  27. CanColorStdout = enableVTMode(windows.Stdout)
  28. } else {
  29. CanColorStdout = isatty.IsCygwinTerminal(os.Stderr.Fd())
  30. }
  31. if isatty.IsTerminal(os.Stderr.Fd()) {
  32. CanColorStderr = enableVTMode(windows.Stderr)
  33. } else {
  34. CanColorStderr = isatty.IsCygwinTerminal(os.Stderr.Fd())
  35. }
  36. }
上海开阖软件有限公司 沪ICP备12045867号-1