Check output device is a terminal: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 1: | Line 1: | ||
⚫ | |||
⚫ | |||
⚫ | |||
{{task}} |
{{task}} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
;Task: |
;Task: |
||
Line 14: | Line 15: | ||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
{{works with|Commodore 64}} |
{{works with|Commodore 64}} |
||
<syntaxhighlight lang=6502asm>LDA $D011 ;screen control register 1 |
<syntaxhighlight lang="6502asm">LDA $D011 ;screen control register 1 |
||
AND #%00100000 ;bit 5 clear = text mode, bit 5 set = gfx mode |
AND #%00100000 ;bit 5 clear = text mode, bit 5 set = gfx mode |
||
BEQ isTerminal</syntaxhighlight> |
BEQ isTerminal</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|GNAT}} |
{{works with|GNAT}} |
||
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>. |
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>. |
||
<syntaxhighlight lang=ada>with Ada.Text_IO; use Ada.Text_IO; |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Interfaces.C_Streams; use Interfaces.C_Streams; |
with Interfaces.C_Streams; use Interfaces.C_Streams; |
||
Line 42: | Line 42: | ||
stdout is not a tty. |
stdout is not a tty. |
||
</pre> |
</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Use <code>isatty()</code> on file descriptor to determine if it's a TTY. To get the file descriptor from a <code>FILE*</code> pointer, use <code>fileno</code>: |
Use <code>isatty()</code> on file descriptor to determine if it's a TTY. To get the file descriptor from a <code>FILE*</code> pointer, use <code>fileno</code>: |
||
<syntaxhighlight lang=c>#include <unistd.h> // for isatty() |
<syntaxhighlight lang="c">#include <unistd.h> // for isatty() |
||
#include <stdio.h> // for fileno() |
#include <stdio.h> // for fileno() |
||
Line 71: | Line 70: | ||
stdout is not tty |
stdout is not tty |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<syntaxhighlight lang=csharp>using System; |
<syntaxhighlight lang="csharp">using System; |
||
namespace CheckTerminal { |
namespace CheckTerminal { |
||
Line 82: | Line 80: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=cpp>#if _WIN32 |
<syntaxhighlight lang="cpp">#if _WIN32 |
||
#include <io.h> |
#include <io.h> |
||
#define ISATTY _isatty |
#define ISATTY _isatty |
||
Line 106: | Line 103: | ||
return 0; |
return 0; |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Works with GnuCOBOL. |
Works with GnuCOBOL. |
||
<syntaxhighlight lang=cobol> *> |
<syntaxhighlight lang="cobol"> *> |
||
*> istty, check id fd 0 is a tty |
*> istty, check id fd 0 is a tty |
||
*> Tectonics: cobc -xj istty.cob |
*> Tectonics: cobc -xj istty.cob |
||
Line 157: | Line 153: | ||
fd 0 tty: +0000000000 |
fd 0 tty: +0000000000 |
||
fd 2 tty: +0000000000</pre> |
fd 2 tty: +0000000000</pre> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
{{Works with|SBCL}} |
{{Works with|SBCL}} |
||
<syntaxhighlight lang=lisp>(with-open-stream (s *standard-output*) |
<syntaxhighlight lang="lisp">(with-open-stream (s *standard-output*) |
||
(format T "stdout is~:[ not~;~] a terminal~%" |
(format T "stdout is~:[ not~;~] a terminal~%" |
||
(interactive-stream-p s)))</syntaxhighlight> |
(interactive-stream-p s)))</syntaxhighlight> |
||
Line 176: | Line 171: | ||
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>. It needs to be compiled to be executed. |
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>. It needs to be compiled to be executed. |
||
<syntaxhighlight lang=lisp>(ffi:clines " |
<syntaxhighlight lang="lisp">(ffi:clines " |
||
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
||
#include <unistd.h> |
#include <unistd.h> |
||
Line 206: | Line 201: | ||
$ ./is-tty | cat - |
$ ./is-tty | cat - |
||
stdout is not a terminal</pre> |
stdout is not a terminal</pre> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
<syntaxhighlight lang=ruby>File.new("testfile").tty? #=> false |
<syntaxhighlight lang="ruby">File.new("testfile").tty? #=> false |
||
File.new("/dev/tty").tty? #=> true |
File.new("/dev/tty").tty? #=> true |
||
STDOUT.tty? #=> true</syntaxhighlight> |
STDOUT.tty? #=> true</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="d">import std.stdio; |
||
extern(C) int isatty(int); |
extern(C) int isatty(int); |
||
Line 228: | Line 221: | ||
Stdout is tty: false |
Stdout is tty: false |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
You have to know 1 is the correct file descriptor number: |
You have to know 1 is the correct file descriptor number: |
||
<syntaxhighlight lang=factor> |
<syntaxhighlight lang="factor"> |
||
IN: scratchpad USE: unix.ffi |
IN: scratchpad USE: unix.ffi |
||
IN: scratchpad 1 isatty |
IN: scratchpad 1 isatty |
||
Line 238: | Line 230: | ||
1 |
1 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight lang=freebasic> |
<syntaxhighlight lang="freebasic"> |
||
Open Cons For Output As #1 |
Open Cons For Output As #1 |
||
' Open Cons abre los flujos de entrada (stdin) o salida (stdout) estándar |
' Open Cons abre los flujos de entrada (stdin) o salida (stdout) estándar |
||
Line 254: | Line 244: | ||
Sleep |
Sleep |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Tells a ''terminal'' apart from a ''pipe'' on Linux and Mac, which is probably exactly what you need. |
Tells a ''terminal'' apart from a ''pipe'' on Linux and Mac, which is probably exactly what you need. |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 280: | Line 268: | ||
Who are you? You're not a terminal. |
Who are you? You're not a terminal. |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<syntaxhighlight lang=haskell>module Main where |
<syntaxhighlight lang="haskell">module Main where |
||
-- requires the unix package |
-- requires the unix package |
||
Line 303: | Line 290: | ||
stdout is not tty |
stdout is not tty |
||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="j">3=nc<'wd'</syntaxhighlight> |
||
Explanation: |
Explanation: |
||
Line 319: | Line 305: | ||
But, correctness requires us to keep in mind that these will only be heuristics, and will sometimes be incorrect (hopefully not often enough to matter a lot...). |
But, correctness requires us to keep in mind that these will only be heuristics, and will sometimes be incorrect (hopefully not often enough to matter a lot...). |
||
=={{header|Javascript/NodeJS}}== |
=={{header|Javascript/NodeJS}}== |
||
<syntaxhighlight lang=js>node -p -e "Boolean(process.stdout.isTTY)" |
<syntaxhighlight lang="js">node -p -e "Boolean(process.stdout.isTTY)" |
||
true</syntaxhighlight> |
true</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="julia"> |
||
if isa(STDOUT, Base.TTY) |
if isa(STDOUT, Base.TTY) |
||
println("This program sees STDOUT as a TTY.") |
println("This program sees STDOUT as a TTY.") |
||
Line 337: | Line 321: | ||
This program sees STDOUT as a TTY. |
This program sees STDOUT as a TTY. |
||
</pre> |
</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{Works with|Ubuntu|14.04}} |
{{Works with|Ubuntu|14.04}} |
||
<syntaxhighlight lang=scala>// Kotlin Native version 0.5 |
<syntaxhighlight lang="scala">// Kotlin Native version 0.5 |
||
import platform.posix.* |
import platform.posix.* |
||
Line 355: | Line 338: | ||
stdout is a terminal |
stdout is a terminal |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{works with|Lua|5.1+}} |
{{works with|Lua|5.1+}} |
||
Using pure Lua, assuming a *NIX-like runtime environment ... |
Using pure Lua, assuming a *NIX-like runtime environment ... |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="lua">local function isTTY ( fd ) |
||
fd = tonumber( fd ) or 1 |
fd = tonumber( fd ) or 1 |
||
local ok, exit, signal = os.execute( string.format( "test -t %d", fd ) ) |
local ok, exit, signal = os.execute( string.format( "test -t %d", fd ) ) |
||
Line 393: | Line 375: | ||
You can accomplish the same results using the luaposix [https://github.com/luaposix/luaposix] library: |
You can accomplish the same results using the luaposix [https://github.com/luaposix/luaposix] library: |
||
<syntaxhighlight lang=lua>local unistd = require( "posix.unistd" ) |
<syntaxhighlight lang="lua">local unistd = require( "posix.unistd" ) |
||
local function isTTY ( fd ) |
local function isTTY ( fd ) |
||
Line 406: | Line 388: | ||
The output of this version is identical to the output of the first version. |
The output of this version is identical to the output of the first version. |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
There is no explicit way (ie <tt>isatty()</tt>)to do this; however, if we ''assume'' that standard out ''is'' a terminal, we can check if the output stream has been redirected (presumably to something other than a terminal). |
There is no explicit way (ie <tt>isatty()</tt>)to do this; however, if we ''assume'' that standard out ''is'' a terminal, we can check if the output stream has been redirected (presumably to something other than a terminal). |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nemerle">def isTerm = System.Console.IsOutputRedirected;</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Using function "isatty" of standard module "terminal" which accepts a File as argument. |
Using function "isatty" of standard module "terminal" which accepts a File as argument. |
||
As we want to redirect stdout, we write the messages on stderr. |
As we want to redirect stdout, we write the messages on stderr. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import terminal |
||
stderr.write if stdout.isatty: "stdout is a terminal\n" else: "stdout is not a terminal\n"</syntaxhighlight> |
stderr.write if stdout.isatty: "stdout is a terminal\n" else: "stdout is not a terminal\n"</syntaxhighlight> |
||
Line 425: | Line 405: | ||
<pre>Command: ./check_output_dev >somefile |
<pre>Command: ./check_output_dev >somefile |
||
Result: stdout is not a terminal</pre> |
Result: stdout is not a terminal</pre> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang=ocaml>let () = |
<syntaxhighlight lang="ocaml">let () = |
||
print_endline ( |
print_endline ( |
||
if Unix.isatty Unix.stdout |
if Unix.isatty Unix.stdout |
||
Line 447: | Line 426: | ||
Output doesn't go to tty. |
Output doesn't go to tty. |
||
</pre> |
</pre> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
<syntaxhighlight lang=scheme> |
<syntaxhighlight lang="scheme"> |
||
(define (isatty? fd) (syscall 16 fd 19)) |
(define (isatty? fd) (syscall 16 fd 19)) |
||
(print (if (isatty? stdout) |
(print (if (isatty? stdout) |
||
Line 455: | Line 433: | ||
"stdout is not a tty.")) |
"stdout is not a tty.")) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
The -t function on a filehandle tells you whether it's a terminal. |
The -t function on a filehandle tells you whether it's a terminal. |
||
<syntaxhighlight lang=bash>$ perl -e "warn -t STDOUT ? 'Terminal' : 'Other'" |
<syntaxhighlight lang="bash">$ perl -e "warn -t STDOUT ? 'Terminal' : 'Other'" |
||
Terminal |
Terminal |
||
$ perl -e "warn -t STDOUT ? 'Terminal' : 'Other'" > x.tmp |
$ perl -e "warn -t STDOUT ? 'Terminal' : 'Other'" > x.tmp |
||
Other |
Other |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (no input or output redirection in a browser!)</span> |
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (no input or output redirection in a browser!)</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"stdin:%t, stdout:%t, stderr:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"stdin:%t, stdout:%t, stderr:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)})</span> |
||
Line 484: | Line 460: | ||
stdin:false, stdout:true, stderr:true |
stdin:false, stdout:true, stderr:true |
||
</pre> |
</pre> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<syntaxhighlight lang=php> |
<syntaxhighlight lang="php"> |
||
if(posix_isatty(STDOUT)) { |
if(posix_isatty(STDOUT)) { |
||
echo "The output device is a terminal".PHP_EOL; |
echo "The output device is a terminal".PHP_EOL; |
||
Line 493: | Line 468: | ||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Pretty much the same as [[Check input device is a terminal#Python]]. |
Pretty much the same as [[Check input device is a terminal#Python]]. |
||
<syntaxhighlight lang=python>from sys import stdout |
<syntaxhighlight lang="python">from sys import stdout |
||
if stdout.isatty(): |
if stdout.isatty(): |
||
print 'The output device is a teletype. Or something like a teletype.' |
print 'The output device is a teletype. Or something like a teletype.' |
||
else: |
else: |
||
print 'The output device isn\'t like a teletype.'</syntaxhighlight> |
print 'The output device isn\'t like a teletype.'</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="quackery"> [ $ |from sys import stdout |
||
to_stack( 1 if stdout.isatty() else 0)| |
to_stack( 1 if stdout.isatty() else 0)| |
||
python ] is ttyout ( --> b ) |
python ] is ttyout ( --> b ) |
||
Line 518: | Line 491: | ||
<pre>Looks like a teletype.</pre> |
<pre>Looks like a teletype.</pre> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket> |
<syntaxhighlight lang="racket"> |
||
(terminal-port? (current-output-port)) |
(terminal-port? (current-output-port)) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
Line 532: | Line 503: | ||
$ raku -e 'note $*OUT.t' >/dev/null |
$ raku -e 'note $*OUT.t' >/dev/null |
||
False</pre> |
False</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
{{works with|PC/REXX under DOS or in a DOS window under MS Windows}} |
{{works with|PC/REXX under DOS or in a DOS window under MS Windows}} |
||
Line 555: | Line 525: | ||
<br>On IBM mainframes, a user can have STDIN defined, but the terminal can be ''disconnected''. |
<br>On IBM mainframes, a user can have STDIN defined, but the terminal can be ''disconnected''. |
||
<syntaxhighlight lang=rexx>/*REXX program determines if the STDIN is a terminal device or other. */ |
<syntaxhighlight lang="rexx">/*REXX program determines if the STDIN is a terminal device or other. */ |
||
signal on syntax /*if syntax error, then jump ──► SYNTAX*/ |
signal on syntax /*if syntax error, then jump ──► SYNTAX*/ |
||
say 'output device:' testSTDIN() /*displays terminal ──or── other */ |
say 'output device:' testSTDIN() /*displays terminal ──or── other */ |
||
Line 589: | Line 559: | ||
output device: 6 |
output device: 6 |
||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang=rust>f = File.open("test.txt") |
<syntaxhighlight lang="rust">f = File.open("test.txt") |
||
p f.isatty # => false |
p f.isatty # => false |
||
p STDOUT.isatty # => true |
p STDOUT.isatty # => true |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<syntaxhighlight lang=rust>/* Uses C library interface */ |
<syntaxhighlight lang="rust">/* Uses C library interface */ |
||
extern crate libc; |
extern crate libc; |
||
Line 609: | Line 577: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{Works with|Ubuntu|14.04}} |
{{Works with|Ubuntu|14.04}} |
||
<syntaxhighlight lang=scala>import org.fusesource.jansi.internal.CLibrary._ |
<syntaxhighlight lang="scala">import org.fusesource.jansi.internal.CLibrary._ |
||
object IsATty extends App { |
object IsATty extends App { |
||
Line 633: | Line 600: | ||
println("tty " + apply(true)) |
println("tty " + apply(true)) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
<syntaxhighlight lang=sml>val stdoutRefersToTerminal : bool = Posix.ProcEnv.isatty Posix.FileSys.stdout</syntaxhighlight> |
<syntaxhighlight lang="sml">val stdoutRefersToTerminal : bool = Posix.ProcEnv.isatty Posix.FileSys.stdout</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
To detect whether output is going to a terminal in Tcl, you check whether the <code>stdout</code> channel looks like a serial line (as those are indistinguishable from terminals). The simplest way of doing that is to see whether you can read the <tt>-mode</tt> or <code>-xchar</code> channel options, which are only present on serial channels: |
To detect whether output is going to a terminal in Tcl, you check whether the <code>stdout</code> channel looks like a serial line (as those are indistinguishable from terminals). The simplest way of doing that is to see whether you can read the <tt>-mode</tt> or <code>-xchar</code> channel options, which are only present on serial channels: |
||
<syntaxhighlight lang=tcl>set toTTY [dict exists [fconfigure stdout] -mode] |
<syntaxhighlight lang="tcl">set toTTY [dict exists [fconfigure stdout] -mode] |
||
puts [expr {$toTTY ? "Output goes to tty" : "Output doesn't go to tty"}]</syntaxhighlight> |
puts [expr {$toTTY ? "Output goes to tty" : "Output doesn't go to tty"}]</syntaxhighlight> |
||
At the system call level, when Tcl is setting up the channels that correspond to the underlying <tt>stdout</tt> (and <tt>stdin</tt> and <tt>stderr</tt>) file descriptors, it checks whether the channels are network sockets (with <code>getsockname()</code>) or serial lines (with <code>isatty()</code>). This allows Tcl scripts to find out information about their calling environment (e.g., when they are run from <tt>inetd</tt>) with minimal code. |
At the system call level, when Tcl is setting up the channels that correspond to the underlying <tt>stdout</tt> (and <tt>stdin</tt> and <tt>stderr</tt>) file descriptors, it checks whether the channels are network sockets (with <code>getsockname()</code>) or serial lines (with <code>isatty()</code>). This allows Tcl scripts to find out information about their calling environment (e.g., when they are run from <tt>inetd</tt>) with minimal code. |
||
Line 650: | Line 615: | ||
===Channel type discovery with older Tcl versions=== |
===Channel type discovery with older Tcl versions=== |
||
Before Tcl 8.4, this discovery process is impossible; <code>stdout</code> always looks like it is going to a file. With 8.4, you can discover the channel type but you need slightly different (and less efficient, due to the thrown error in the non-tty case) code to do it. |
Before Tcl 8.4, this discovery process is impossible; <code>stdout</code> always looks like it is going to a file. With 8.4, you can discover the channel type but you need slightly different (and less efficient, due to the thrown error in the non-tty case) code to do it. |
||
<syntaxhighlight lang=tcl>set toTTY [expr {![catch {fconfigure stdout -mode}]}]</syntaxhighlight> |
<syntaxhighlight lang="tcl">set toTTY [expr {![catch {fconfigure stdout -mode}]}]</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
<syntaxhighlight lang=sh>#!/bin/sh |
<syntaxhighlight lang="sh">#!/bin/sh |
||
if [ -t 1 ] |
if [ -t 1 ] |
||
Line 661: | Line 625: | ||
echo "Output is NOT a terminal" >/dev/tty |
echo "Output is NOT a terminal" >/dev/tty |
||
fi</syntaxhighlight> |
fi</syntaxhighlight> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
<syntaxhighlight lang=vbnet>Module Module1 |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Sub Main() |
Sub Main() |
||
Line 671: | Line 634: | ||
End Module</syntaxhighlight> |
End Module</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|C}} |
{{trans|C}} |
||
As there is currently no way to obtain this information via Wren CLI, we instead embed a Wren script in a C application and ask the host program to get it for us. |
As there is currently no way to obtain this information via Wren CLI, we instead embed a Wren script in a C application and ask the host program to get it for us. |
||
<syntaxhighlight lang=ecmascript>/* check_output_device_is_terminal.wren */ |
<syntaxhighlight lang="ecmascript">/* check_output_device_is_terminal.wren */ |
||
class C { |
class C { |
||
Line 684: | Line 646: | ||
<br> |
<br> |
||
We now embed this Wren script in the following C program, compile and run it. |
We now embed this Wren script in the following C program, compile and run it. |
||
<syntaxhighlight lang=c>#include <stdlib.h> |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <unistd.h> |
#include <unistd.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 779: | Line 741: | ||
Output device is a terminal = false |
Output device is a terminal = false |
||
</pre> |
</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
On Unix, check to see if stdout's st_mode is a character device. |
On Unix, check to see if stdout's st_mode is a character device. |
||
<syntaxhighlight lang=zkl>const S_IFCHR=0x2000; |
<syntaxhighlight lang="zkl">const S_IFCHR=0x2000; |
||
fcn S_ISCHR(f){ f.info()[4].bitAnd(S_IFCHR).toBool() } |
fcn S_ISCHR(f){ f.info()[4].bitAnd(S_IFCHR).toBool() } |
||
S_ISCHR(File.stdout).println();</syntaxhighlight> |
S_ISCHR(File.stdout).println();</syntaxhighlight> |
||
Line 795: | Line 756: | ||
False |
False |
||
</pre> |
</pre> |
||
⚫ |