Created
April 29, 2014 06:44
-
-
Save shiena/11392308 to your computer and use it in GitHub Desktop.
detect terminal
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" | |
"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