That's a pretty weird title but I'm having trouble articulating this question:
When I run kitty --version
in my terminal it prints its version out to stdout, however the text is styled and colored:
In order to achieve this the process had to output ANSI escape codes to stdout, however I don't see them when I hexdump the output:
$ kitty --version | xxd -g 1 -c 10 -u
00000000: 6B 69 74 74 79 20 30 2E 31 39 kitty 0.19
0000000a: 2E 31 20 63 72 65 61 74 65 64 .1 created
00000014: 20 62 79 20 4B 6F 76 69 64 20 by Kovid
0000001e: 47 6F 79 61 6C 0A Goyal.
I'd expect to see at least a few escape characters and other ANSI sequences here but I don't. This leads me to believe that kitty
is able to "predict" whether its output will appear in a terminal that can process the escape codes.
How is it able to do that? Or is it a feature of the terminal emulator perhaps?
isatty()
actually determines if a file descriptor is a tty, (since, unlike with fifos, there are no special st_mode flags for ttys), is thatisatty()
is actually caling an ioctl likeTCGETS
orTIOCGWINSZ
which is supposed to only works on ttys. If you wonder howkitty
or other program determines if a tty supports color escapes, it that it's doing it in a very unreliable way, by looking for the value of theTERM
envvar into some kind of incomplete and obsolete database (terminfo) or just going on and assuming that any terminal supports color escapes. – Nov 01 '20 at 20:16