Check output device is a terminal: Difference between revisions

Added two (2) examples for Lua
(Added two (2) examples for Lua)
Line 282:
stdout is a terminal
</pre>
 
=={{header|Lua}}==
{{works with|Lua|5.1+}}
Using pure Lua, assuming a *NIX-like runtime environment ...
<lang Lua>local function isTTY ( fd )
fd = tonumber( fd ) or 1
local ok, exit, signal = os.execute( string.format( "test -t %d", fd ) )
return (ok and exit == "exit") and signal == 0 or false
end
 
print( "stdin", isTTY( 0 ) )
print( "stdout", isTTY( 1 ) )
print( "stderr", isTTY( 2 ) )</lang>
 
{{out}}
<pre>
$ lua istty.lua
stdin true
stdout true
stderr true
$ cat /dev/null | lua istty.lua
stdin false
stdout true
stderr true
$ lua istty.lua | tee
stdin true
stdout false
stderr true
$ lua istty.lua 2>&1 | tee
stdin true
stdout false
stderr false
</pre>
 
{{works with|Lua|5.1+}}
{{libheader|posix}}
 
You can accomplish the same results using the luaposix [https://github.com/luaposix/luaposix] library:
<lang lua>local unistd = require( "posix.unistd" )
 
local function isTTY ( fd )
fd = tonumber( fd ) or 1
local ok, err, errno = unistd.isatty( fd )
return ok and true or false
end
 
print( "stdin", isTTY( 0 ) )
print( "stdout", isTTY( 1 ) )
print( "stderr", isTTY( 2 ) )</lang>
 
The output of this version is identical to the output of the first version.
 
=={{header|Nemerle}}==