How to detect what terminal people use

474 views Asked by At

Currently I am working on a feature to detect the kind of (pseudo)terminal people use.

My first idea is ask the /etc/alternatives/x-terminal-emulator:

term=/etc/alternatives/x-terminal-emulator
test -e $term && file $term | ...

But I don't know if it can work well on OSX... Maybe there is a better method to detect it?

Will someone be willing to offer me more ideas about detecting the terminal people use?

2

There are 2 answers

0
statox On

You can use the $TERM variable which should be present on both *nix OSes and OSX. This variable is (normally) initialized by the system to let you know which terminal you're using.

As a bonus you can use the toe command to find the list of terminal types recognized by the system.

0
Thomas Dickey On

It would be nice to ask the terminal itself, but there is no reliable, portable method for doing so. The OP's question is in the context of Debian or derivatives, since it refers to x-terminal-emulator, and asks about OSX.

The current state of the art requires the user to set the TERM value so that terminal applications (such as ncurses) will know which terminal description to use. Many (perhaps "most") applications use termcap or terminfo (or termcap emulated by terminfo), but some are either hardcoded or use their own special data (such as GNU ls). So setting TERM to a usable value sometimes depends on the common denominator of data maintained independently for different reasons.

As to why it requires the user to do this:

  • some applications simply set TERM to xterm, to save the developers of the corresponding terminals the trouble of providing accurate terminal descriptions.
  • The xterm FAQ What versions are available? lists several of the most well-known versions along with some details of their differences from xterm.
  • OSX provides a few more (Terminal.app and iTerm2), which also set TERM to xterm, but are different. In ncurses, the recommended value for TERM in these applications is nsterm (or nsterm-256color).
  • When the terminal provides a way to set TERM (xterm does this in a command-line option), then users can get better results.
  • Some terminal programs set other environment variables which may help scripts to distinguish which terminal type is used. xterm for instance sets XTERM_VERSION. Others may set COLOR_TERM, but there is no standard for how that variable is used. Terminal.app sets TERM_PROGRAM, as does iTerm2. (None of those are by default passed via ssh).
  • The terminals which (like xterm) emulate VT100s provide as a minor detail a control sequence which asks the terminal what it is. That is less than useful, since most of these either set the response to the code used by VT100 or by some particular version of xterm (without, of course, ensuring that the terminal acts like xterm).
  • you can test this using vttest (menu 6.4 "Primary attributes")
  • Terminal.app and iTerm2 both claim they are a VT100.
  • current xterm says it is a member of the VT400 family, and lists the features which it implements (again, some programs copy a given response without actually supporting those features).
  • vttest also provides a menu entry for the secondary response (supported by VT220s and up), and passes back its version number in the response. Some terminals as noted pretend to be a particular version of xterm. VTE used to do that; it now passes back its version number. But there are others for which this information is still useless.
  • ranging further afield, there are other programs than vttest which try to identify the terminal. The original qterm program (there are several others with the same name but different purpose) did this by matching patterns against the terminal's response. The tack program shows the terminal's response as well, using data from ncurses' database.
  • finally, some terminals do not have a way to ask them what they are. Sun's (now Oracle's) console emulators lack this feature altogether. They are not VT100-compatible. It is mentioned in this obscure manner in the wscons manual page:

Note: The VT100 adheres the ANSI X3.64 standard. However, because the VT100 features nonstandard extensions to ANSI X3.64, it is incompatible with Sun terminal emulators.

Amusingly enough, the reason for that last comment is Sun's own nonstandard extension to ANSI X3.64:

ESC[s Reset terminal emulator (SUNRESET)

Takes no parameters. Resets all modes to default, restores current font from PROM. Screen and cursor position are unchanged.

It used to be frequently mentioned, because someone at Sun Microsystems (see beginning of this answer) had reduced their development effort by setting TERM to vt100.