php-sqllint got an SQL formatting output mode now. Together with SqlParser's output coloring mode (which generates ANSI escape codes) I get nice colorful SQL statements on my terminal.
But this is not what I want when piping the output to another tool that does not understand escape sequences.
Using a --color option is too inconvenient for day-to-day use, and using --nocolor for piping is also not convenient. It should just work.
PHP's POSIX extension offers a posix_isatty() function that can be used to check if the output devices is an interactive TTY or not:
$usecolors = true; if (function_exists('posix_isatty') && !posix_isatty(STDOUT) ) { $usecolors = false; }
This code automatically enables coloring except when the output is not a teletype (non-interactive). Coloring can then be done with e.g. Console_Color2.
Other implementations
Symfony's Console's StreamOutput class uses posix_isatty for unix systems, and uses environment variables on Windows to detect if the terminal has been made ANSI-capable:
protected function hasColorSupport() { if (DIRECTORY_SEPARATOR === '\\') { return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM'); } return function_exists('posix_isatty') && @posix_isatty($this->stream); }