Codebase list golang-github-go-kit-kit / d2f2902b-79c4-43cd-8c09-341e33fc6017/v0.2.0 log / term / terminal_windows.go
d2f2902b-79c4-43cd-8c09-341e33fc6017/v0.2.0

Tree @d2f2902b-79c4-43cd-8c09-341e33fc6017/v0.2.0 (Download .tar.gz)

terminal_windows.go @d2f2902b-79c4-43cd-8c09-341e33fc6017/v0.2.0raw · history · blame

// Based on ssh/terminal:
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package term

import (
	"io"
	"syscall"
	"unsafe"
)

var kernel32 = syscall.NewLazyDLL("kernel32.dll")

var (
	procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
)

// IsTerminal returns true if w writes to a terminal.
func IsTerminal(w io.Writer) bool {
	fw, ok := w.(fder)
	if !ok {
		return false
	}
	var st uint32
	r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fw.Fd(), uintptr(unsafe.Pointer(&st)), 0)
	return r != 0 && e == 0
}