0

In a terminal that does not support ANSI escape code, outputting a color control code like \033[0m will not only have no effect, but will also disturb the terminal.

I know a json formatting tool called jq, which judges whether the terminal can use ANSI escape code.

I want to know, how to realize this function through C programming language?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
aszswaz
  • 609
  • 3
  • 10
  • 1
    Do you mean ANSI color codes? ASCII does not specify any. – gspr Sep 18 '21 at 12:57
  • 1
    On UNIX and Linux, the environment variable $TERM tells you the terminal type, and from that you can deduce what features are supported, perhaps using termcap or curses. – Steve Summit Sep 18 '21 at 13:02
  • I remembered it wrong, not ASCII color is ANSI escape code standard color code. – aszswaz Sep 18 '21 at 13:25

1 Answers1

5

On Linux and probably POSIX systems, you might use isatty(3) with STDOUT_FILENO. See also stdio(3) and fileno(3).

So your code would be

if (isatty(STDOUT_FILENO)) {
   // standard output is a tty
   ...
}

and you have to bet that in 2021 most terminal emulators support ANSI TTY escapes.

By the way, jq is an open source software tool. You are allowed to download then study its source code.

Notice that the C programming language standard (read n1570 or better) does not know about ttys. Read the TTY demystified web page.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you. Although jq is indeed open source, I think this function is not complicated. It will be faster to directly ask technically strong people on stackoverflow. You are right, there are indeed many terminals that support ANSI. However, some popular tools do not provide ANSI support. For example, Logstash, which is a server log collection tool, collects logs through stdout. Seeing a code like \033[0m on its web is extremely uncomfortable. – aszswaz Sep 18 '21 at 13:44
  • 3
    I strongly disagree. You always will learn a lot more by glancing into existing open source code, and perhaps improving it to suit your needs – Basile Starynkevitch Sep 18 '21 at 13:45
  • Thank you for your suggestion, and I agree with you. Alas, it’s just that one person sometimes needs to choose between time and study. – aszswaz Sep 18 '21 at 14:03
  • 2
    [Relevant jq code](https://github.com/stedolan/jq/blob/a9ce7244b4241c457bace5f7d7871268ed00c40b/src/main.c#L561) – ikegami Sep 18 '21 at 14:09
  • 1
    @aszswaz So you are asking for our time instead of investing your own. Hm, think about that, is this right? – the busybee Sep 18 '21 at 20:40
  • 1
    I don't know where this conclusion is drawn. I did not use any force that they have to answer my question. I ask questions on a voluntary basis. If someone is willing to provide an answer, that is the best. I am very grateful to them for helping me. No one is willing to provide an answer, so I will temporarily put this feature on hold and wait until I have time to check the jq source code. – aszswaz Sep 20 '21 at 04:00