Jump to content

Check output device is a terminal: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added syntax colouring, marked p2js incompatible)
m (syntax highlighting fixup automation)
Line 14:
=={{header|6502 Assembly}}==
{{works with|Commodore 64}}
<langsyntaxhighlight lang=6502asm>LDA $D011 ;screen control register 1
AND #%00100000 ;bit 5 clear = text mode, bit 5 set = gfx mode
BEQ isTerminal</langsyntaxhighlight>
 
=={{header|Ada}}==
Line 22:
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>.
 
<langsyntaxhighlight lang=ada>with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C_Streams; use Interfaces.C_Streams;
 
Line 32:
Put_Line(Standard_Error, "stdout is a tty.");
end if;
end Test_tty;</langsyntaxhighlight>
 
{{out}}
Line 47:
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>:
 
<langsyntaxhighlight lang=c>#include <unistd.h> // for isatty()
#include <stdio.h> // for fileno()
 
Line 56:
: "stdout is not tty");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 73:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>using System;
 
namespace CheckTerminal {
Line 81:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang=cpp>#if _WIN32
#include <io.h>
#define ISATTY _isatty
Line 105:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|COBOL}}==
Works with GnuCOBOL.
 
<langsyntaxhighlight lang=cobol> *>
*> istty, check id fd 0 is a tty
*> Tectonics: cobc -xj istty.cob
Line 135:
 
goback.
end program istty.</langsyntaxhighlight>
 
DISPLAY for fd 1 is directed to SYSERR to get some output during the various trials.
Line 160:
=={{header|Common Lisp}}==
{{Works with|SBCL}}
<langsyntaxhighlight lang=lisp>(with-open-stream (s *standard-output*)
(format T "stdout is~:[ not~;~] a terminal~%"
(interactive-stream-p s)))</langsyntaxhighlight>
 
{{Out}}
Line 176:
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>. It needs to be compiled to be executed.
 
<langsyntaxhighlight lang=lisp>(ffi:clines "
#include <sys/ioctl.h>
#include <unistd.h>
Line 193:
 
(format T "stdout is~:[ not~;~] a terminal~%" (tty-p))
(quit)</langsyntaxhighlight>
 
Compilation can be done with the following commands :
Line 208:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang=ruby>File.new("testfile").tty? #=> false
File.new("/dev/tty").tty? #=> true
STDOUT.tty? #=> true</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang=D>import std.stdio;
 
extern(C) int isatty(int);
Line 219:
void main() {
writeln("Stdout is tty: ", stdout.fileno.isatty == 1);
}</langsyntaxhighlight>
 
{{out}}
Line 231:
=={{header|Factor}}==
You have to know 1 is the correct file descriptor number:
<langsyntaxhighlight lang=factor>
IN: scratchpad USE: unix.ffi
IN: scratchpad 1 isatty
Line 237:
--- Data stack:
1
</syntaxhighlight>
</lang>
 
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>
Open Cons For Output As #1
' Open Cons abre los flujos de entrada (stdin) o salida (stdout) estándar
Line 253:
Close #1
Sleep
</syntaxhighlight>
</lang>
 
 
Line 259:
Tells a ''terminal'' apart from a ''pipe'' on Linux and Mac, which is probably exactly what you need.
 
<langsyntaxhighlight lang=go>package main
 
import (
Line 272:
fmt.Println("Who are you? You're not a terminal")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 283:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang=haskell>module Main where
 
-- requires the unix package
Line 296:
(if istty
then "stdout is tty"
else "stdout is not tty")</langsyntaxhighlight>
{{Out}}
<pre>$ runhaskell istty.hs
Line 306:
=={{header|J}}==
 
<langsyntaxhighlight lang=J>3=nc<'wd'</langsyntaxhighlight>
 
Explanation:
Line 321:
 
=={{header|Javascript/NodeJS}}==
<langsyntaxhighlight lang=js>node -p -e "Boolean(process.stdout.isTTY)"
true</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang=Julia>
if isa(STDOUT, Base.TTY)
println("This program sees STDOUT as a TTY.")
Line 331:
println("This program does not see STDOUT as a TTY.")
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 340:
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<langsyntaxhighlight lang=scala>// Kotlin Native version 0.5
 
import platform.posix.*
Line 349:
else
println("stdout is not a terminal")
}</langsyntaxhighlight>
 
{{out}}
Line 359:
{{works with|Lua|5.1+}}
Using pure Lua, assuming a *NIX-like runtime environment ...
<langsyntaxhighlight lang=Lua>local function isTTY ( fd )
fd = tonumber( fd ) or 1
local ok, exit, signal = os.execute( string.format( "test -t %d", fd ) )
Line 367:
print( "stdin", isTTY( 0 ) )
print( "stdout", isTTY( 1 ) )
print( "stderr", isTTY( 2 ) )</langsyntaxhighlight>
 
{{out}}
Line 393:
 
You can accomplish the same results using the luaposix [https://github.com/luaposix/luaposix] library:
<langsyntaxhighlight lang=lua>local unistd = require( "posix.unistd" )
 
local function isTTY ( fd )
Line 403:
print( "stdin", isTTY( 0 ) )
print( "stdout", isTTY( 1 ) )
print( "stderr", isTTY( 2 ) )</langsyntaxhighlight>
 
The output of this version is identical to the output of the first version.
Line 409:
=={{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).
<langsyntaxhighlight lang=Nemerle>def isTerm = System.Console.IsOutputRedirected;</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 415:
As we want to redirect stdout, we write the messages on stderr.
 
<langsyntaxhighlight lang=Nim>import terminal
 
stderr.write if stdout.isatty: "stdout is a terminal\n" else: "stdout is not a terminal\n"</langsyntaxhighlight>
 
{{out}}
Line 428:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang=ocaml>let () =
print_endline (
if Unix.isatty Unix.stdout
then "Output goes to tty."
else "Output doesn't go to tty."
)</langsyntaxhighlight>
 
Testing in interpreted mode:
Line 449:
 
=={{header|Ol}}==
<langsyntaxhighlight lang=scheme>
(define (isatty? fd) (syscall 16 fd 19))
(print (if (isatty? stdout)
"stdout is a tty."
"stdout is not a tty."))
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
The -t function on a filehandle tells you whether it's a terminal.
 
<langsyntaxhighlight lang=bash>$ perl -e "warn -t STDOUT ? 'Terminal' : 'Other'"
Terminal
$ perl -e "warn -t STDOUT ? 'Terminal' : 'Other'" > x.tmp
Other
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight 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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 486:
 
=={{header|PHP}}==
<langsyntaxhighlight lang=php>
if(posix_isatty(STDOUT)) {
echo "The output device is a terminal".PHP_EOL;
Line 492:
echo "The output device is NOT a terminal".PHP_EOL;
}
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
Pretty much the same as [[Check input device is a terminal#Python]].
<langsyntaxhighlight lang=python>from sys import stdout
if stdout.isatty():
print 'The output device is a teletype. Or something like a teletype.'
else:
print 'The output device isn\'t like a teletype.'</langsyntaxhighlight>
 
=={{header|Quackery}}==
Line 506:
{{trans|Python}}
 
<langsyntaxhighlight lang=Quackery> [ $ |from sys import stdout
to_stack( 1 if stdout.isatty() else 0)|
python ] is ttyout ( --> b )
Line 513:
[ say "Looks like a teletype." ]
else
[ say "Not a teletype." ]</langsyntaxhighlight>
 
{{out}}
Line 520:
 
=={{header|Racket}}==
<langsyntaxhighlight lang=racket>
(terminal-port? (current-output-port))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 555:
<br>On IBM mainframes, a user can have STDIN defined, but the terminal can be ''disconnected''.
 
<langsyntaxhighlight lang=rexx>/*REXX program determines if the STDIN is a terminal device or other. */
signal on syntax /*if syntax error, then jump ──► SYNTAX*/
say 'output device:' testSTDIN() /*displays terminal ──or── other */
Line 568:
end /* [↑] can't use a RETURN here. */
 
/* ··· handle other REXX syntax errors here ··· */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 591:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang=rust>f = File.open("test.txt")
p f.isatty # => false
p STDOUT.isatty # => true
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>/* Uses C library interface */
 
extern crate libc;
Line 608:
println!("stdout is not tty");
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
{{Works with|Ubuntu|14.04}}
<langsyntaxhighlight lang=scala>import org.fusesource.jansi.internal.CLibrary._
 
object IsATty extends App {
Line 632:
 
println("tty " + apply(true))
}</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang=sml>val stdoutRefersToTerminal : bool = Posix.ProcEnv.isatty Posix.FileSys.stdout</langsyntaxhighlight>
 
=={{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:
<langsyntaxhighlight lang=tcl>set toTTY [dict exists [fconfigure stdout] -mode]
puts [expr {$toTTY ? "Output goes to tty" : "Output doesn't go to tty"}]</langsyntaxhighlight>
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.
{{out|Demonstrating}}
Line 650:
===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.
<langsyntaxhighlight lang=tcl>set toTTY [expr {![catch {fconfigure stdout -mode}]}]</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
<langsyntaxhighlight lang=sh>#!/bin/sh
 
if [ -t 1 ]
Line 660:
else
echo "Output is NOT a terminal" >/dev/tty
fi</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Module Module1
 
Sub Main()
Line 670:
End Sub
 
End Module</langsyntaxhighlight>
 
=={{header|Wren}}==
{{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.
<langsyntaxhighlight lang=ecmascript>/* check_output_device_is_terminal.wren */
 
class C {
Line 681:
}
 
System.print("Output device is a terminal = %(C.isOutputDeviceTerminal)")</langsyntaxhighlight>
<br>
We now embed this Wren script in the following C program, compile and run it.
<langsyntaxhighlight lang=c>#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
Line 765:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 782:
=={{header|zkl}}==
On Unix, check to see if stdout's st_mode is a character device.
<langsyntaxhighlight lang=zkl>const S_IFCHR=0x2000;
fcn S_ISCHR(f){ f.info()[4].bitAnd(S_IFCHR).toBool() }
S_ISCHR(File.stdout).println();</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.