add html syntax highlighting
authorChristian Weiske <cweiske@cweiske.de>
Sat, 19 Dec 2015 13:20:41 +0000 (14:20 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Sat, 19 Dec 2015 13:20:41 +0000 (14:20 +0100)
README.rst
src/phpsqllint/Cli.php
tests/files/create.sql [deleted file]

index ef9e165cb882a93c83bacc4b49c55db22daf30e4..e02ff6bac8b22f91d0efabdc651db85d668f470f 100644 (file)
@@ -44,7 +44,20 @@ Formatting::
     ORDER BY NAME
     LIMIT 10
 
     ORDER BY NAME
     LIMIT 10
 
-You can enable ANSI coloring by passing the ``--colors`` option.
+
+Syntax highlighting
+===================
+ANSI colors are applied automatically when not piping; you can use the
+``--highlight`` option to override the automatism.
+
+``--highlight`` option values:
+
+``none``
+  No highlighting. Use it to disable automatic highlighting
+``ansi``
+  ANSI escape codes for your shell
+``html``
+  HTML tags
 
 
 ====
 
 
 ====
@@ -80,6 +93,15 @@ Now you can use ``./bin/php-sqllint`` without building the phar yourself.
 ========
 Building
 ========
 ========
 Building
 ========
+
+Preparation
+===========
+- Adjust version number in ``src/phpsqllint/Cli.php``
+- Adjust version number in ``build.xml``
+
+
+Create the release
+==================
 You'll need `phing`__, the PHP build tool::
 
     $ phing
 You'll need `phing`__, the PHP build tool::
 
     $ phing
index 2f2473fa7cf7867b2a9a2ffeb447d6c7e1407725..1448a53e649d12fe919968af90689ea91c1eb818 100644 (file)
@@ -29,7 +29,14 @@ class Cli
     protected $renderer;
 
     protected $format = false;
     protected $renderer;
 
     protected $format = false;
-    protected $colors = false;
+
+    /**
+     * What syntax highlighting mode should be used
+     *
+     * none, ansi, html
+     */
+    protected $highlight = 'none';
+
 
     /**
      * Start processing.
 
     /**
      * Start processing.
@@ -122,9 +129,13 @@ class Cli
             return false;
         }
 
             return false;
         }
 
+        $typeMap = array(
+            'none' => 'text',
+            'ansi' => 'cli',
+            'html' => 'html',
+        );
         $options = array(
         $options = array(
-            //FIXME: automatically detect if the shell/tool supports colors
-            'type' => $this->colors ? 'cli' : 'text'
+            'type' => $typeMap[$this->highlight],
         );
         echo \SqlParser\Utils\Formatter::format($sql, $options) . "\n";
     }
         );
         echo \SqlParser\Utils\Formatter::format($sql, $options) . "\n";
     }
@@ -166,19 +177,20 @@ class Cli
             )
         );
         $parser->addOption(
             )
         );
         $parser->addOption(
-            'colors',
-            array(
-                'long_name'   => '--color',
-                'description' => 'Use colors in formatting output',
-                'action'      => 'StoreTrue',
-            )
-        );
-        $parser->addOption(
-            'nocolors',
+            'highlight',
             array(
             array(
-                'long_name'   => '--nocolor',
-                'description' => 'Disable colors in formatting output',
-                'action'      => 'StoreTrue',
+                'short_name'  => '-h',
+                'long_name'   => '--highlight',
+                'description' => 'Highlighting mode (when using --format)',
+                'action'      => 'StoreString',
+                'choices'     => array(
+                    'none',
+                    'ansi',
+                    'html',
+                    'auto',
+                ),
+                'default' => 'auto',
+                'add_list_option' => true,
             )
         );
         $parser->addOption(
             )
         );
         $parser->addOption(
@@ -226,18 +238,20 @@ class Cli
 
             $this->format = $result->options['format'];
 
 
             $this->format = $result->options['format'];
 
-            if ($result->options['colors'] !== null) {
-                $this->colors = $result->options['colors'];
-            } else if ($result->options['nocolors'] !== null) {
-                $this->colors = !$result->options['nocolors'];
-            } else {
-                //default coloring to enabled, except
-                // when piping | to another tool
-                $this->colors = true;
-                if (function_exists('posix_isatty')
-                    && !posix_isatty(STDOUT)
-                ) {
-                    $this->colors = false;
+            $this->highlight = $result->options['highlight'];
+            if ($this->highlight == 'auto') {
+                if (php_sapi_name() == 'cli') {
+                    //default coloring to enabled, except
+                    // when piping | to another tool
+                    $this->highlight = 'ansi';
+                    if (function_exists('posix_isatty')
+                        && !posix_isatty(STDOUT)
+                    ) {
+                        $this->highlight = 'none';
+                    }
+                } else {
+                    //no idea where we are, so do not highlight
+                    $this->highlight = 'none';
                 }
             }
 
                 }
             }
 
diff --git a/tests/files/create.sql b/tests/files/create.sql
deleted file mode 100644 (file)
index f5ff243..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-CREATE TABLE test (
-    uid INT(11) DEFAULT 0 NOT NULL,
-    pid INT(11) DEFAULT 0 NOT NULL
-);