by andrewla · 3y ago · 0 comments
Even things like the use of ESC, and the overloading of ESC for the actual ESC key, for a signal for keycodes, and for the ALT/META key are crazy.
I've thought for a while that I should implement an extremely reduced set of escape sequences which use a non-ESC character (maybe SOH or something) for control sequences and key encoding. A minimal set -- color / cursor position / save-screen / query-size -- whatever it takes to make vim/emacs + tmux/screen work, and call it a day.
Those use TERM/TERMCAP environment variables and some educated guessing reading stdin/stdout/stderr behaviour to figure out what terminal you're using, search for said entry in terminfo/termcap database, and finally invoking native terminal behavior or simulating it with other terminal behavior.
In theory, one could "just implement" an alternative terminal, and put said simplified entries in terminfo/termcap database and things will be all fine.
In practice several of those ANSI escape code behaviors are hard-coded and everyone pretends everything is "at least as capable as xterm" (a oxymoron, given that xterm is one of the most capable and feature-rich terminals around), so you're also gonna have to implement a converter from "xterm ANSI" to your alternative terminal system - sort of how winpty does convert Win32 terminals conventions to ANSI ones.
Then we're back to the starting issue in this converter.
The most common examples I see these days are for red/green/yellow status text in shell scripts. To anyone planning to do that, or even more complex stuff, please consider using tput instead.
tput setaf 1; echo this is red
tput setaf 2; echo this is green
tput setaf 3; echo this is yellow
tput setaf 8; echo this is dim
tput bold; echo this is bold
tput sgr0; echo this is normal
blue=$(tput setaf 4); normal=$(tput sgr0); echo ${blue}Color${normal} text.
(man terminfo for arcane details.)
It's wise to make sure output is going to a terminal before using colors, because they don't play well with logs or text processing commands. You can check with: test -t 1
If you use colors, please consider making them optional, since they don't work well for everyone or in every desktop color scheme. An environment variable makes a sensible switch.
Please don't do that without first checking the background colour. Yellow in particular on a light background is unreadable.
(On an xterm-compatible terminal you can get the current background colour with OSC 11 ; ? BEL. Some popular terminals lie about being xterm-compatible, though.)
¹ Ha, ha. The distro I use, Debian, also omits at least one POSIX standard utility by default, even though all of them used to fit nicely on systems with less disk than your current CPU's cache.
red=$(tput setaf 1 2>/dev/null || true)
echo ${red}text
And PC/3270. Some very powerful systems like Linux on IBM Z are POSIX and your default "interactive login" choice is PC/3270.
And in the long term, isn't this exactly the problem? Do we really want to perpetuate the status-quo of "I am in a somewhat POSIX environment, so the STDOUT/STDERR if interactive must behave exactly like xterm and if not I will just pretend it is xterm anyways and output garbage."?
Old glass terminals are still in use here and there, too.
One problem is termcap/terminfo itself. The definition has expanded greatly over time, and documentation is poor to non-existent. In many cases termcap/terminfo has been implicitly extended to support ncurses specifically. And like you say, everyone just sort of assumes ANSI/xterm -- nobody has `PS1="$(tput setaf 3)\w $(tput setaf 0) $", you just hard-code the codes.
The last time I chased the rabbit down this hole I got mired in TTY-land and was not able to dig myself out. Too many insane layers, and before you know it you're writing code to emulate serial connections in a vain attempt to make things work without burning the whole world down.
Both (n)vim and tmux have internal TTY emulation as well which makes things even crazier; I think vim uses libvterm and tmux has one internally hand-coded. It's a mess.
Consider a really simple case. You are using screen (or tmux, or emacs, or whatever) and you have your terminal divided in half so that you can use a program in one half and tail a log file in the other. The program sends a ^L to clear the screen. Screen (or tmux or whatever) must read the ^L and then _not_ send it on. If it sent it on, the whole screen would be cleared, not just the half with the program in it. Instead it has to send whatever control sequences would erase the text from just the right portion of the screen while leaving everything else alone. Virtually everything sent by the program(s) has to be intercepted and reinterpreted in order for it to work right.
\s is a bit hacky for blanking though, it can produce some undesired results in terms of copy/pasting from the terminal and resizing. But if you've got a "full screen" process like you're describing, you'd be trapping SIGWINCH for resize events anyway.
My shell also creates PTYs on some rare occasions too. I’d prefer not to because it’s messy but sometimes there’s no way around it if you want your command line applications to behave like they would on a TTY whilst still having your utility capture that applications output as if it were writing to a pipe.
This is a limitation of TTYs.
The most annoying part of it all is that creating a PTY differs from one Unix-like OS to another. So it’s definitely not a rabbit hole developers go down happily.
[Edit: I misread the parent comment as saying multiplexers needed to do terminal emulation. Dtach only multiplexes (using pesudo-TTYs, of course) but unlike screen or tmux does not do terminal emulation. Original comment follows:]
That isn't strictly true; the one I use (dtach) is just a multiplexer.
It actually happened the other way around. The escape character was originally only there as an escape character at the start of a control sequence. It was added to the keyboard back in the teletype era specifically so that the operator could control the behavior of the device on the other end. And it was labeled “Alt Mode”! See <https://upload.wikimedia.org/wikipedia/commons/2/23/TTY33ASR...>, though it is a little blurry.
Terminals are in a bad place because there was a whole lot of effort put towards compatibility instead of standardization. The same curse that email suffers under. So the "standards" end up hopelessly complex and also terribly vague because they're trying to support every dumb thing every single company did at the same time. And the worst part is that most all of those companies would cease to exist in short order afterward, so the system is chock full of bug-accurate support for long dead systems that literally nobody uses anymore.
So it's really the reverse of what you state. There has been a lot of effort put into standardization, and compared to the 1960s we are highly uniform in the 21st century and widely adhere to an almost 50-year-old standard. It's not the long-dead systems that spoil this. As evidenced from the headlined article, it is the still active systems adding things like vendor-specific commands for setting titles and clipboards and colours and displaying images and whatnot that have introduced the complexity.
A strictly ECMA-48 only emulator doesn't have even half of the problems mentioned in the headlined article, because the control sequences mentioned are extensions to the standard introduced by various GUI terminal emulator programs.
What else was the ESC key made for if not the control code?
> I've thought for a while that I should implement an extremely reduced set of escape sequences which use a non-ESC character (maybe SOH or something) for control sequences and key encoding. A minimal set -- color / cursor position / save-screen / query-size -- whatever it takes to make vim/emacs + tmux/screen work, and call it a day.
I did that as a 8086 boot sector and it would render tmux with unicode characters stripped. You need surprisingly few sequences.
To me it's insane that one of the most used key command has a 1 second delay in the terminal.
Or that typically Esc + some function/arrow key results in some junk.
Found the vim user! The vast, _vast_ majority of Unix command-line or text-based programs do not use the Esc key because they were not written with a particular terminal or keyboard layout in mind. (Unlike vim.)
But for whatever it's worth, I use vim every day and I notice no 1 second delay when switching modes with the Esc key, so perhaps this is something you can configure in your terminal.
> I notice no 1 second delay when switching modes with the Esc key, so perhaps
> this is something you can configure in your terminal.
This is configurable in Vim. ttimeoutlen, timeoutlen, and noesckeys are pertinent. tmux et al may also need tweaking.> whatever the application wants to use it for
With the application being a terminal emulator, that is emitting an ESC character.
Edit: You asked about the terminal emulator, but its the console application that has to do the wait.
Every time that your terminal emulator does this in response to binary data, wonder why your terminal emulator hasn't taken the very sensible route that was taken by the authors of mosh years ago, to simply not support alternative GL and GR character set switching, treating it as something whose time has passed given that we live in a UTF-8-to-the-terminal world nowadays.
We're still inventing new ways of breaking arguably the most basic user interfaces.
When I reconnect and restart screen back into emacs, ^S pauses output instead of doing an incremental search.
I have to pop out of screen with the right escape sequence to get to the login shell I ran it in, then go "stty -ixon" to disable XON/XOFF flow control. Or always remember to do that before running "screen -RD".
But I never ASKED for flow control, and it has worked PERFECTLY for decades without ever getting into +ixon mode, until just a few months ago, and now it happens every single time.
Did something change recently, is ubuntu auto-installing earth shattering patches that maliciously change the default tty settings behind my back (because XON/XOFF flow control suddenly became cool again?), or did I take a walk on a foggy night and accidentally slip into an alternate universe where it's always been that way for everyone else, and I'm about to be seized by silent men in white coats and taken asylum which turns out to be a rehab clinic for people who can't read minds?
https://en.wikipedia.org/wiki/All_the_Myriad_Ways
I finally gave up and put an "stty -ixon" in my .bashrc, but I get the ominous feeling that's one of those tempting but dangerous kludges you're not supposed to do -- Isn't there a superstition about never touching your tty in an rc file? Or is that never touching your pp in a login file? -- that can come back and bite you in the ass later when you least expect it.
It seems like this is just the latest in a long series of cascading ass bites.
Terminals in non UNIX, that not pretend to be serial printers, like the Amiga DOS, or plain MS-DOS, those are the basic ones.
Rather than reset or stty sane, I use something like one of the following, based on what software is available, e.g., there is no reset on NetBSD
echo w1b63|ired -n /dev/stdout
echo 1b63|xxd -p -r
echo -e '27\n99\n'|nc-data -g
printf \\33cUnsure how fish can fully protect from this, as if you run "cat" in fish, then it isn't between you and the terminal anymore? Or do you mean fish should do a reset before displaying each prompt? If I run the printf commands from the "flash.c" example in fish I end up with a messed up terminal.
printf "\e]4;1;?\a\e[>4;2m"
In xterm+fish will result in a messed up shell which you can't escape from (as ^C to clear the input doesn't work anymore).