Skip to content

Instantly share code, notes, and snippets.

@araddon
Created May 22, 2013 03:14
Show Gist options
  • Save araddon/5625004 to your computer and use it in GitHub Desktop.
Save araddon/5625004 to your computer and use it in GitHub Desktop.
Determine if a go process is running in a terminal, or not
package main
import (
"fmt"
"syscall"
"unsafe"
)
type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
const (
_TIOCGWINSZ = 0x5413 // OSX 1074295912
)
func isTerminal() bool {
ws := &winsize{}
retCode, _, _ := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(_TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
return false
}
return true
}
func main() {
fmt.Printf("Isterminal=%v\n", isTerminal())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment