Check output device is a terminal: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Promote to task, lots of examples, little real controversy, most concerns have been addressed) |
(→{{header|Go}}: A solution without the use of external libraries. I'm 95% sure this isn't any worse than the previous version (both would have corner cases)) |
||
Line 235: | Line 235: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Tells a ''terminal'' apart from a ''pipe'' on Linux and Mac, which is probably exactly what you need. |
|||
{{libheader|Go sub-repositories}} |
|||
<lang go>package main |
<lang go>package main |
||
Line 241: | Line 242: | ||
"os" |
"os" |
||
"fmt" |
"fmt" |
||
"golang.org/x/crypto/ssh/terminal" |
|||
) |
) |
||
func main() { |
func main() { |
||
if |
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 { |
||
fmt.Println("Hello terminal") |
fmt.Println("Hello terminal") |
||
} else { |
} else { |
||
fmt.Println("Who are you? |
fmt.Println("Who are you? You're not a terminal") |
||
} |
} |
||
}</lang> |
}</lang> |
||
Line 256: | Line 256: | ||
Hello terminal |
Hello terminal |
||
> hello | cat |
> hello | cat |
||
Who are you? |
Who are you? You're not a terminal. |
||
</pre> |
</pre> |
||