terminal_windows.go 721 B

12345678910111213141516171819202122232425262728293031323334
  1. // Based on ssh/terminal:
  2. // Copyright 2011 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. // +build windows,!appengine
  6. package logrus
  7. import (
  8. "io"
  9. "os"
  10. "syscall"
  11. "unsafe"
  12. )
  13. var kernel32 = syscall.NewLazyDLL("kernel32.dll")
  14. var (
  15. procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
  16. )
  17. // IsTerminal returns true if stderr's file descriptor is a terminal.
  18. func IsTerminal(f io.Writer) bool {
  19. switch v := f.(type) {
  20. case *os.File:
  21. var st uint32
  22. r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, uintptr(v.Fd()), uintptr(unsafe.Pointer(&st)), 0)
  23. return r != 0 && e == 0
  24. default:
  25. return false
  26. }
  27. }