diff options
author | Elan Ruusamäe <glen@delfi.ee> | 2014-06-06 20:36:01 +0300 |
---|---|---|
committer | Elan Ruusamäe <glen@delfi.ee> | 2014-06-06 20:37:25 +0300 |
commit | 069e49d5da441ad32a49f3546f69bc412c87481d (patch) | |
tree | 71e89e5bd66a5f740fc6cda5c7f816479e753f73 /bin | |
parent | ebeb18ffd93454572a829bb43c5ca8c3318c3d63 (diff) | |
download | sql-formatter-069e49d5da441ad32a49f3546f69bc412c87481d.zip sql-formatter-069e49d5da441ad32a49f3546f69bc412c87481d.tar.gz sql-formatter-069e49d5da441ad32a49f3546f69bc412c87481d.tar.bz2 |
autodetect colors
so we can redirect output to file and it will not be colored while
outputing to terminal it would be colored
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/sql-formatter | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/bin/sql-formatter b/bin/sql-formatter index 4466306..2aa4465 100755 --- a/bin/sql-formatter +++ b/bin/sql-formatter @@ -15,4 +15,25 @@ else { require_once(__DIR__.'/../lib/SqlFormatter.php'); -echo SqlFormatter::format($sql); +/** + * Returns true if the stream supports colorization. + * + * Colorization is disabled if not supported by the stream: + * + * - Windows without Ansicon and ConEmu + * - non tty consoles + * + * @return bool true if the stream supports colorization, false otherwise + * @link https://github.com/symfony/symfony/blob/v2.4.6/src/Symfony/Component/Console/Output/StreamOutput.php#L97 + */ +function hasColorSupport() { + if (DIRECTORY_SEPARATOR == '\\') { + return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI'); + } + + return function_exists('posix_isatty') && @posix_isatty(STDOUT); +} + +$highlight = hasColorSupport(); + +echo SqlFormatter::format($sql, $highlight); |