Created
May 22, 2013 03:14
-
-
Save araddon/5625004 to your computer and use it in GitHub Desktop.
Determine if a go process is running in a terminal, or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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