Difference between version 15 and 16 - Previous - Next
A '''terminal''' is a display unit that is part of the user interface to a
computer system.  The word carries the connotation of being text-only.

** See Also **

   [ANSI]:   Various ANSI standards govern terminal behaviour.

   [xterm]:   A terminal emulator.

   [curses]:   A [C] library for controlling terminals.

   [handling of ANSI terminals using Expect]:   

   [stty]:   The [Units%|%*nix] command to configure and manage a terminal.

   [terminal control sequence]:   

** Reference **

   [http://vt100.net/%|%VT100.net]:   Dedicated to the range of video terminals produced by Digital Equipment Corporation (DEC) from 1970 to 1995.  The most famous of these is the VT100.

   [http://www.linusakesson.net/programming/tty/index.php%|%The TTY demystified], Linus Åkesson, 2008-07-25:   

** Description **

Terminal emulators for [Linux] have support for ANSI escape codes built in.

** Checking for a Terminal **

[AMG]: To check if stdin is connected to a terminal, run:

if {![catch {exec /usr/bin/tty -s}]} {
    # stdin is a terminal

Checking for stdout/stderr is harder because [exec] does not allow stdout/stderr to be redirected to stdin because stdout/stderr are not considered readable.  Instead, let the shell do the redirection.  Furthermore, instruct [[exec]] to not intercept stdout/stderr.

if {![catch {exec /bin/sh -c {/usr/bin/tty -s < /dev/stdout} >@ stdout}]} {
    # stdout is a terminal
if {![catch {exec /bin/sh -c {/usr/bin/tty -s < /dev/stderr} 2>@ stderr}]} {
    # stderr is a terminal

Without the `-s` switch, the tty program not only checks if its stdin is a terminal, but it also prints the device name to its stdout, e.g. `/dev/pts/0`.
How to test this on Windows?
[MHo] 2021-09-17: How to test this on Windows? I found the following way if [twapi] is available:
package require twapi
proc isStdinRedirected {} {
catch {get_tcl_channel_handle stdin write}
puts [isStdinRedirected]
c:\Users\matthiasu\usr\pgm\tcl\usr\Tst\isatty>echo test|tclkitsh test8.tcl
c:\Users\matthiasu\usr\pgm\tcl\usr\Tst\isatty>tclkitsh test8.tcl<nul
c:\Users\matthiasu\usr\pgm\tcl\usr\Tst\isatty>tclkitsh test8.tcl
<<categories>> Text Screen