Reputation: 639
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?
Upvotes: 0
Views: 857
Reputation: 1
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.
Upvotes: 6