Terminal escape sequences: why don’t terminals report what features they support, instead of relying on terminfo

escape-charactersterminalterminfo

I've been looking into escape sequences lately, and I'm surprised of what they can do. You can even move an xterm X11 window with them (try printf '\e[3;0;0t'), wow!

The most common way to know what features a terminal supports seems to be using a database. That's what ncurses does, and it's used by 99.9% of applications that rely on escape sequences.
Ncurses reads the terminfo database for your shell's TERM environment variable in order to decide which features are supported by your console.
You can change the TERM environment variable of your shell, and if you do it most applications could start using less features or misbehaving (try running nano or vim after setting TERM="").

I've seen that some escape codes cause the terminal to report stuff. For instance <ESC>[6n causes the terminal to report the cursor position. (printf '\e[6n')

  • Why don't we use similar report mechanisms to let the console report which features it supports?

Instead of coupling the features with the value of TERM, each console could advertise its own features, making the whole thing more precise and reliable. Why isn't this a thing?


Edit: something that I should have asked before… I'd like to create a new escape sequence, to hack konsole and gnome-terminal in order to support it and to use it in some scripts.
I'd like to be able to query the console in order to know whether the one I'm running supports this feature – what's the suggested way to do that?

Best Answer

It's not as simple as you might suppose. xterm (like the DEC VTxxx terminals starting with VT100) has a number of reports for various features (refer to XTerm Control Sequences). The most generally useful is that which tells what type of terminal it is:

CSI Ps c  Send Device Attributes (Primary DA).

Not all terminals have that type of response (Sun hardware console has/had none).

But there are more features than reports (for instance, how to tell whether a terminal is really interpreting UTF-8: the accepted route for that is via the locale environment variables, so no need has been established for another control sequence/response).

In practice, while there are a few applications that pay attention to reports (such as vim, checking the actual values of function keys, the number of colors using DCS + p Pt ST, and even the cursor appearance using DCS $ q Pt ST), the process is unreliable because some developers find it simpler to return a given report-response than to implement the feature. If you read through the source code for various programs, you'll find interesting quirks where someone has customized a response to make it look like some version of xterm.