Live data from Hacker News

Everything you ever wanted to know about terminals

xn--rpa.cc

121–130 of 191 posts

Re: Everything you ever wanted to know about terminals

#121
post #5

> but i swear to god developers have so completely forgotten how terminals work that i might be one of a handful of people left on earth who actually has the knowledge to, so they all just layer their bullshit on top of ncurses (which should never have survived the '90s) instead and it's maddening. Actually, yes, understanding the tty in detail seems to become a dark art. However it's the best way to do complex thing…

It looks like you have done a lot of work with sixels based on your github too. Have you ever written anything about that?

Not just sixels but she also has the most superior tool for rendering images as unicode blocks. https://github.com/csdvrx/derasterize

Re: Everything you ever wanted to know about terminals

#122
post #111

One thing that the author doesn't seem to know about is /dev/tty. In the article the escape codes are just sent to stdout. Though an awful lot of applications (including the greatest ones) do this, IMHO this is wrong. The terminal control codes are used to control a terminal, and they are often not meant to be part of the output stream, for example when the output is piped or redirected to a text file. When what you…

A very nervous very young me gave a bad talk on terminals at a conference about 10 years ago. Reviews of my talk were pretty rough, but a number of them mentioned learning that they should have been sending their control codes to standard error, so it wasn't a total waste...

Re: Everything you ever wanted to know about terminals

#123

The author seems not to be aware of the recent TUI rennaisance[1]. There are libraries like termbox and blessings (python) that are a middle-ground between full ncurses and adding your own ansi codes. There are a lot of modern TUI frameworks like tui-go or tui-rs that bring common GUI conventions back to the TUI (heck there are TUI programming libraries that are designed to be similar to react) - these too tend to be…

> The author seems not to be aware of the recent TUI renaissance

The title should be changed to (2019) because this post came out at about the same time as the teletypewriter aristocracy kicked off the renaissance.

Re: Everything you ever wanted to know about terminals

#124

I can tell you why people still use ncurses: if you’re actually interested in accounting for all the nuanced eldritch madness that is terminal emulation implementation and history it’s a monumental effort, at least imo. Sure, if all you care about is coloring text on recent terminals, I do think just using ansi codes is fine. However, I completely disagree with the assertion that hardcoding ansi codes is somehow “mor…

But, you see, ncurses works, so it can't be allowed to stand. We have to disdain it, and pretend that it's bad for some undefined reason, so we can make half-functional software that ignores any solutions older than My August Personage. It's the Wheel Of Progress or something.

/s

Re: Everything you ever wanted to know about terminals

#125

I could take this a lot more seriously if capital letters were properly used.

Isn't worrying about someone else's grammatical use of capitalization when the piece is understood anyway a little too pedantic?

I know this is not always intended but I read all lowercase as being passive aggressive in tone.

Much like all capitals is seen as shouting.

It’s a way of saying, you are not worth talking to properly.

Re: Everything you ever wanted to know about terminals

#126

I was working on terminal code recently and had a hell of a time finding anything approaching an authoritative spec or reference docs. The best I found was this: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html I'd recommend that over the wikipedia link in the article, since wikipedia seems to be missing some things.

Here's the reference I use for VT100 ANSI control sequences: https://github.com/jart/cosmopolitan/blob/c6bbca55e9f977e386... I created this reference because there wasn't one available before that could be easily copy/pasted into GNU C or Python string literals.

Re: Everything you ever wanted to know about terminals

#127
post #124

I can tell you why people still use ncurses: if you’re actually interested in accounting for all the nuanced eldritch madness that is terminal emulation implementation and history it’s a monumental effort, at least imo. Sure, if all you care about is coloring text on recent terminals, I do think just using ansi codes is fine. However, I completely disagree with the assertion that hardcoding ansi codes is somehow “mor…

But, you see, ncurses works , so it can't be allowed to stand. We have to disdain it, and pretend that it's bad for some undefined reason, so we can make half-functional software that ignores any solutions older than My August Personage. It's the Wheel Of Progress or something. /s

ncurses delenda est (ncurses must be destroyed)

Re: Everything you ever wanted to know about terminals

#128
post #119
post #111

One thing that the author doesn't seem to know about is /dev/tty. In the article the escape codes are just sent to stdout. Though an awful lot of applications (including the greatest ones) do this, IMHO this is wrong. The terminal control codes are used to control a terminal, and they are often not meant to be part of the output stream, for example when the output is piped or redirected to a text file. When what you…

This is my first time hearing of this functionality. How does it work with buffered stdout? For example, if you wanted to colour a single word?

> This is my first time hearing of this functionality.

I was also surprised when I discovered this. IMO this should be used everywhere but I've rarely seen it.

> How does it work with buffered stdout? For example, if you wanted to colour a single word?

This is not affected by buffering. What matters is what the output device/file/stream gets eventually.

How coloring a single word works depends on how you use it.

You can do

    write_and_flush_to_dev_tty(BEGIN_RED_TEXT);
    write_and_flush_to_dev_tty("word");
    write_and_flush_to_dev_tty(END_RED_TEXT);
The write_and_flush_... pseudo functions are written in this way just to make it clear that I'm describing the behavior when the output gets the bytes immediately. I don't mean that you should use /dev/tty like this.

Redirection won't have any effect on these lines of code. You always get a red "word" on the terminal. The redirection target doesn't get anything.

    write_and_flush_to_dev_tty(BEGIN_RED_TEXT);
    write_and_flush_to_stdout("word");
    write_and_flush_to_dev_tty(END_RED_TEXT);
When directly used on a terminal, you get a red "word" on your terminal, but when you do redirection, the target only gets plaintext "word".

    write_and_flush_to_dev_tty(BEGIN_RED_TEXT);
    write_and_flush_to_stdout("word");
    write_and_flush_to_stdout(END_RED_TEXT);
When redirected, your terminal stays red after the red "word" is printed, and your redirection target gets an extra escape code.

When stdout is a tty, /dev/tty is the same as stdout, so a write that goes to one goes to the other. In this case, even if you don't flush after write immediately it's likely not a problem. Still it's something that the developer should pay attention to.

-------------

Edit:

Please ignore all the pseudocode examples. They don't convey what I wanted to express.

Just use /dev/tty in the same way as how you would use stdin/stdout/stderr that is connected to a terminal. Only difference is that it is a rw device that can be used for input and output at the same time.

Re: Everything you ever wanted to know about terminals

#129

You can't have "everything you wanted to know about terminals" with absolutely no mention of terminfo and (previously) termcap. You don't necessarily need ncurses to have a portable smart terminal experience, but you do need terminfo. ncurses magic relies on the lore stored in terminfo, which contains all of the obscure escape sequences and other information about the disparate world of terminals. They're maintained…

As the article states: > being compatible only with ANSI-capable terminals is a feature, not a bug Driving old hardware terminals (as opposed to terminal emulators) is a fun stunt, not something of actual modern value. Every modern terminal emulator supports ANSI escape sequences. Some features are supported by a subset of terminals, but you can either 1) probe for those features by asking the terminal, which some te…

Plus a lot of interesting things aren't mentioned in terminfo - bracketed paste, cursor shaping, synchronized output, ...

And even truecolor was added to it about 10 years after terminals started gaining support.

And many terminals just claim to be "xterm-256color".

>2) try them and have graceful degradation if they're not supported

Note: There are many cases where degradation isn't graceful. Many terminals on windows currently spew garbage on your screen if you send bracketed paste (alacritty, for instance).

>1) probe for those features by asking the terminal, which some terminals support

This requires waiting for a reply, which often isn't useful. E.g. if you want synchronized output, you want it from the very first paint (because that's when the terminal is most likely to still be resized, e.g. by a tiling window manager). So you would have to delay your startup until you've either gotten a reply or "enough" time has passed that you believe it's not supported.

Frankly, this is all a big mess and terminfo isn't very helpful, but we don't have a good alternative either.

Re: Everything you ever wanted to know about terminals

#130
post #128
post #119

Earlier quoted context omitted.

This is my first time hearing of this functionality. How does it work with buffered stdout? For example, if you wanted to colour a single word?

> This is my first time hearing of this functionality. I was also surprised when I discovered this. IMO this should be used everywhere but I've rarely seen it. > How does it work with buffered stdout? For example, if you wanted to colour a single word? This is not affected by buffering. What matters is what the output device/file/stream gets eventually. How coloring a single word works depends on how you use it. You…

So, the answer to "How does it work with buffered stdout?" is, "It doesn't, you have to keep flushing."
Post reply on HN