Skip to content

Instantly share code, notes, and snippets.

@shiena
Created April 29, 2014 06:44
Show Gist options
  • Save shiena/11392308 to your computer and use it in GitHub Desktop.
Save shiena/11392308 to your computer and use it in GitHub Desktop.
detect terminal
package main
import (
"fmt"
"os"
"syscall"
"github.com/monochromegane/terminal"
)
const (
stdin = 0
stdout = 1
)
func isatty(fd uintptr) bool {
dll := syscall.MustLoadDLL("msvcrt.dll")
defer dll.Release()
isatty := dll.MustFindProc("_isatty")
ret, _, _ := isatty.Call(fd)
return int(ret) != 0
}
func main() {
if terminal.IsTerminal(os.Stdin) {
fmt.Printf("[terminal]stdin has not been redirected to file\n")
} else {
fmt.Printf("[terminal]stdin has been redirected to file\n")
}
if terminal.IsTerminal(os.Stdout) {
fmt.Printf("[terminal]stdout has not been redirected to file\n")
} else {
fmt.Printf("[terminal]stdout has been redirected to file\n")
}
if isatty(stdin) {
fmt.Printf("[_isatty ]stdin has not been redirected to file\n")
} else {
fmt.Printf("[_isatty ]stdin has been redirected to file\n")
}
if isatty(stdout) {
fmt.Printf("[_isatty ]stdout has not been redirected to file\n")
} else {
fmt.Printf("[_isatty ]stdout has been redirected to file\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment