From: Christian Weiske Date: Sat, 19 Dec 2015 13:20:41 +0000 (+0100) Subject: add html syntax highlighting X-Git-Tag: v0.1.0~2 X-Git-Url: https://git.cweiske.de/php-sqllint.git/commitdiff_plain/93dacd06794e94769b5de142d569fb5faad3e03d add html syntax highlighting --- diff --git a/README.rst b/README.rst index ef9e165..e02ff6b 100644 --- a/README.rst +++ b/README.rst @@ -44,7 +44,20 @@ Formatting:: 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 ======== + +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 diff --git a/src/phpsqllint/Cli.php b/src/phpsqllint/Cli.php index 2f2473f..1448a53 100644 --- a/src/phpsqllint/Cli.php +++ b/src/phpsqllint/Cli.php @@ -29,7 +29,14 @@ class Cli protected $renderer; protected $format = false; - protected $colors = false; + + /** + * What syntax highlighting mode should be used + * + * none, ansi, html + */ + protected $highlight = 'none'; + /** * Start processing. @@ -122,9 +129,13 @@ class Cli return false; } + $typeMap = array( + 'none' => 'text', + 'ansi' => 'cli', + 'html' => 'html', + ); $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"; } @@ -166,19 +177,20 @@ class Cli ) ); $parser->addOption( - 'colors', - array( - 'long_name' => '--color', - 'description' => 'Use colors in formatting output', - 'action' => 'StoreTrue', - ) - ); - $parser->addOption( - 'nocolors', + 'highlight', 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( @@ -226,18 +238,20 @@ class Cli $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 index f5ff243..0000000 --- a/tests/files/create.sql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE test ( - uid INT(11) DEFAULT 0 NOT NULL, - pid INT(11) DEFAULT 0 NOT NULL -);