summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xbin/sql-formatter30
1 files changed, 26 insertions, 4 deletions
diff --git a/bin/sql-formatter b/bin/sql-formatter
index 4466306..c96c79b 100755
--- a/bin/sql-formatter
+++ b/bin/sql-formatter
@@ -1,9 +1,10 @@
#!/usr/bin/env php
<?php
if("cli" !== php_sapi_name()) {
- echo "<p>Run this php script from the command line to see CLI syntax highlighting and formatting. It support Unix pipes or command line argument style.</p>";
- echo "<pre><code>php examples/cli.php \"SELECT * FROM MyTable WHERE (id>5 AND \\`name\\` LIKE \\&quot;testing\\&quot;);\"</code></pre>";
- echo "<pre><code>echo \"SELECT * FROM MyTable WHERE (id>5 AND \\`name\\` LIKE \\&quot;testing\\&quot;);\" | php examples/cli.php</code></pre>";
+ echo "<p>Run this PHP script from the command line to see CLI syntax highlighting and formatting. It supports Unix pipes or command line argument style.</p>";
+ echo "<pre><code>php bin/sql-formatter \"SELECT * FROM MyTable WHERE (id>5 AND \\`name\\` LIKE \\&quot;testing\\&quot;);\"</code></pre>";
+ echo "<pre><code>echo \"SELECT * FROM MyTable WHERE (id>5 AND \\`name\\` LIKE \\&quot;testing\\&quot;);\" | php bin/sql-formatter</code></pre>";
+ exit;
}
if(isset($argv[1])) {
@@ -15,4 +16,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);