2

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:

enter image description here

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?

Bogdan M.
  • 123

1 Answers1

2

Read man isatty, or https://linux.die.net/man/3/isatty

isatty - test whether a file descriptor refers to a terminal
waltinator
  • 4,865
  • 3
    @bool3max if you wonder how isatty() actually determines if a file descriptor is a tty, (since, unlike with fifos, there are no special st_mode flags for ttys), is that isatty() is actually caling an ioctl like TCGETS or TIOCGWINSZ which is supposed to only works on ttys. If you wonder how kitty 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 the TERM 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
  • @user414777 Thank for you for the additional insight!! – Bogdan M. Nov 04 '20 at 23:26