Make sure the program is in the terminal

I tried to add colors to some lines that should be displayed in the terminal using the ansi escape code. Until now, I did not understand that all ascii supplants the code, just try copying some escape codes. Then they saw the answer, in which it was indicated that the program should check that it is running in the terminal, or continue without polluting lines using escape codes?

The answer explains the use of the * nix isatty() that I discovered is in unistd.h , which, in turn, was not translated to cunistd by the cpp standard, based on my understanding that it was not in the standard c in the first place. I tried again to find https://stackoverflow.com/a/167648/167326/ but failed to understand well. Now I have two questions:

  • In what environment (the correct word?) Can a program using ascii escape codes execute to require an initial check? since I am only for cli.
  • What would be the right ISO cpp solution to solve this problem? using unistd.h ? Will this use be limited to current cpp practices?

Also, is there anything I need to read / understand before understanding the ansi / colors related thing?

+3
source share
1 answer

On a POSIX system (like Linux or OSX), the isatty function isatty indeed the right function to determine if you are outputting a terminal or not.

Use it as

 if (isatty(STDOUT_FILENO)) { // Output using VT100 control codes } else { // Output is not a TTY, could be a pipe or redirected to a file // Use normal output without control codes } 
+4
source

Source: https://habr.com/ru/post/1239631/


All Articles