Settings

Theme

Build your own Command Line with ANSI escape codes (2016)

lihaoyi.com

41 points by vinhnx 4 days ago · 17 comments

Reader

teddyh 20 hours ago

No mention whatsoever of the $TERM environment variable. sigh

Outputting raw escape codes and hope that they work is not how you do it. This is not how any reasonable library (or bash, or git, etc.) does it. These programs and libraries start with the $TERM environment variable to find out what terminal the user is using, and then use something like termcap(5) or terminfo(5) to look up what capabilities that specific terminal has, and what actual escape codes to output to get that effect. In reality, though, most regular programs delegate all this to a library, like ncurses.

(And also make sure not to output any terminal codes at all if the standard output is not a terminal, as in isatty(3) or tty(1).)

This way, you can check for and use modern cool stuff like sixels, but not use them if some user is using something older like XTerm or the Windows Console.

  • tripflag 19 hours ago

    Aside from the 256-color section, I believe all of the examples given in the article are basic sequences which are supported in ~every terminal. Are you aware of any notable terminals where that is not the case?

    Maybe I've been lucky, but I've written plenty software that blindly shoots CSI's at the console and still haven't hit any snags. Especially convenient when the channel is unidirectional; curl ocv.me :)

    • teddyh 4 hours ago

      Do you only want to support users using “notable” terminals?

    • foresto 9 hours ago

      I have read that people still use 9term. Its terminfo entry doesn't show any sign of ANSI escape code support.

      If I remember correctly, ANSI-like terminal emulators in RGB mode (aka direct mode) only support 16-color and red/green/blue escape codes. Not the 256-color palette used in this article.

      ANSI-compatible terminal emulators widely disagree on whether RGB values should be separated by semicolons or colons.

      I have read articles (possibly on hackaday) this decade by people working on retro hardware projects, expressing frustration with command line programs that spew ANSI garbage to their non-ANSI terminals. At least one author resorted to the screen program, because it can translate some ANSI-isms to the correct codes for the active terminal.

      I had genuine TeleVideo 912 glass terminals hooked up until not terribly long ago.

      In any case, I don't view this as a matter of whether unusual terminals are "notable". We have an abstraction that plays nicely with them (and allows new terminal protocols to be developed). It's a POSIX standard. It's easy to use. I suggest using it.

      (I'll put examples in a separate comment.)

    • skydhash 18 hours ago

      Shelll-mode in emacs uses $TERM=dumb and doesn’t support escapes code by default. Same with compile-mode. Also there’s some tools that persist using escapes code even when piped to another program (like less).

  • shawn_w 19 hours ago

    Easy way is to use `tput` to generate the color codes for your terminal type on the fly.

    See https://stackoverflow.com/questions/54838578/color-codes-for... for examples.

  • matheusmoreira 17 hours ago

    Everything is either xterm or something else that's emulating most if not all of its features. Whatever isn't is probably terrible and broken and not worth supporting unless you're getting paid fat stacks to do it.

  • xyzzy_plugh 20 hours ago

    Indeed, but also termcap is a tire fire. I like the way Ghostty deals with it: https://ghostty.org/docs/help/terminfo

    Even then it still leaves a lot to be desired. I'm not sure I can consider any of this to be truly reasonable.

  • enriquto 20 hours ago

    > No mention whatsoever of the $TERM environment variable. sigh

    I have mixed feelings about this... Sometimes I feel young and reckless and want to just output whatever I need without checking $TERM. In practice, all modern terminal emulators are essentially xterm-256color compatible. If something breaks, the worse you get is some funny characters. Is that such a big deal? Much better than propagate the silly termcap/terminfo complexity.

    By the way, xterm supports sixels since forever.

  • foresto 9 hours ago

    For those who don't know how to properly get the active terminal's color escape codes...

    green=$(tput setaf 2)

    normal=$(tput sgr0)

    echo ${green}Color${normal} text.

    ...or if you plan to run on non-POSIX systems that lack tput...

    green=$(tput setaf 2 2>/dev/null || true)

tl2do 21 hours ago

I've been programming for 20 years and apparently still don't understand what my terminal is doing. Recently I asked Claude Code to generate a small shell script. It came back full of escape codes and I just stared at it like a caveman looking at a smartphone. This article finally explains what's been happening right under my nose.

mjbonanno 4 hours ago

This is cool. I am playing around with Bubble Tea in Go today.

esafak 21 hours ago

If you're going to do this also provide a way to disable it so parsers don't trip up on its escape-coded output.

  • klibertp 21 hours ago

    Also, disable the formatting if stdout is not a terminal. That way, your colors and cursor movements won't be visible when piping to another program, and your tool will be usable in apps that don't understand the CSI and chars that follow. Use a command-line switch with more than two states, e.g., `ls` (and probably other GNU tools) has `--color=always|auto|never` which covers most use cases.

    Also not mentioned in the article: there are a few syntaxes available for specifying things in control sequences, like

        \x1b[38;2;{r};{g};{b}m
    
    for specifying colors. There's a nice list here: https://gist.github.com/ConnerWill/d4b6c776b509add763e17f9f1... You can also cram as many control codes as you want into a control sequence, though it probably isn't useful in a modern context in 99.9% of cases.
  • tom_ 18 hours ago

    Also good for people that have a light coloured terminal background.

bitwize 16 hours ago

Everybody who matters is using Ghostty nowadays! Why bother having the God-damned common decency to call isatty(), let alone use terminfo to look up the actual escape codes used by the user's terminal as reported by $TERM?

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection