From d5620bf0a4967e2fd7a1067898944937aaf767fc Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 3 Nov 2014 10:57:41 +0100 Subject: [PATCH] [VarDumper] symfony.com color scheme on the CLI --- .../Component/VarDumper/Dumper/CliDumper.php | 70 ++++++++++++++----- .../Component/VarDumper/Dumper/HtmlDumper.php | 29 ++++---- .../VarDumper/Tests/HtmlDumperTest.php | 6 +- 3 files changed, 66 insertions(+), 39 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php index 523ac8030ea55..51896dc9190b8 100644 --- a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php @@ -28,23 +28,46 @@ class CliDumper extends AbstractDumper protected $maxStringWidth = 0; protected $styles = array( // See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics - 'num' => '1;38;5;33', - 'const' => '1;38;5;33', - 'str' => '1;38;5;37', + 'default' => '38;5;208', + 'num' => '1;38;5;38', + 'const' => '1;38;5;208', + 'str' => '1;38;5;113', 'cchr' => '7', - 'note' => '38;5;178', - 'ref' => '38;5;240', - 'solo-ref' => '38;5;240', - 'public' => '38;5;28', - 'protected' => '38;5;166', - 'private' => '38;5;160', - 'meta' => '38;5;27', - 'key' => '38;5;27', - 'index' => '38;5;27', + 'note' => '38;5;38', + 'ref' => '38;5;247', + 'public' => '', + 'protected' => '', + 'private' => '', + 'meta' => '38;5;170', + 'key' => '38;5;113', + 'index' => '38;5;38', ); protected static $controlCharsRx = '/[\x00-\x1F\x7F]/'; + /** + * {@inheritdoc} + */ + public function __construct($output = null) + { + parent::__construct($output); + + if (defined('PHP_WINDOWS_VERSION_MAJOR') && false !== @getenv('ANSICON')) { + // Use only the base 16 xterm colors when using ANSICON + $this->setStyles(array( + 'default' => '31', + 'num' => '1;34', + 'const' => '1;31', + 'str' => '1;32', + 'note' => '34', + 'ref' => '1;30', + 'meta' => '35', + 'key' => '32', + 'index' => '34', + )); + } + } + /** * Enables/disables colored output. * @@ -70,7 +93,7 @@ public function setMaxStringWidth($maxStringWidth) /** * Configures styles. * - * @param array $styles A map of style namaes to style definitions. + * @param array $styles A map of style names to style definitions. */ public function setStyles(array $styles) { @@ -205,10 +228,8 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild) $prefix = $class ? $this->style('note', 'array:'.$class).' [' : '['; } - if ($cursor->softRefCount) { + if ($cursor->softRefCount || 0 < $cursor->softRefHandle) { $prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), array('count' => $cursor->softRefCount)); - } elseif (0 < $cursor->softRefHandle) { - $prefix .= $this->style('solo-ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').$cursor->softRefHandle); } elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) { $prefix .= $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount)); } @@ -310,7 +331,7 @@ protected function dumpKey(Cursor $cursor) } if ($cursor->hardRefTo) { - $this->line .= ($cursor->hardRefCount ? $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount)) : $this->style('solo-ref', '&')).' '; + $this->line .= $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount)).' '; } } } @@ -327,7 +348,7 @@ protected function dumpKey(Cursor $cursor) protected function style($style, $value, $attr = array()) { if (null === $this->colors) { - $this->colors = $this->supportsColors($this->outputStream); + $this->colors = $this->supportsColors(); } $style = $this->styles[$style]; @@ -336,7 +357,7 @@ protected function style($style, $value, $attr = array()) return sprintf($cchr, "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0]))); }, $value); - return $this->colors ? sprintf("\033[%sm%s\033[m", $style, $value) : $value; + return $this->colors ? sprintf("\033[%sm%s\033[m\033[%sm", $style, $value, $this->styles['default']) : $value; } /** @@ -385,4 +406,15 @@ protected function supportsColors() return static::$defaultColors; } + + /** + * {@inheritdoc} + */ + protected function dumpLine($depth) + { + if ($this->colors) { + $this->line = sprintf("\033[%sm%s\033[m", $this->styles['default'], $this->line); + } + parent::dumpLine($depth); + } } diff --git a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php index 8c8fedb6312ab..af8c57c75e9e0 100644 --- a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php @@ -31,13 +31,13 @@ class HtmlDumper extends CliDumper protected $headerIsDumped = false; protected $lastDepth = -1; protected $styles = array( - 'num' => 'font-weight:bold;color:#1299DA', + 'default' => 'background-color:#18171B; color:#FF8400; line-height:1.2em; font:12px Menlo, Monaco, Consolas, monospace', + 'num' => 'font-weight:bold; color:#1299DA', 'const' => 'font-weight:bold', - 'str' => 'font-weight:bold;color:#56DB3A', - 'cchr' => 'font-style: italic', + 'str' => 'font-weight:bold; color:#56DB3A', + 'cchr' => 'font-style:italic', 'note' => 'color:#1299DA', 'ref' => 'color:#A0A0A0', - 'solo-ref' => 'color:#A0A0A0', 'public' => 'color:#FFFFFF', 'protected' => 'color:#FFFFFF', 'private' => 'color:#FFFFFF', @@ -213,8 +213,8 @@ function a(e, f) { if ('sf-dump' != elt.parentNode.className) { toggle(a); } - } else if ("sf-dump-ref" == elt.className) { - a = elt.getAttribute('href').substr(1); + } else if ("sf-dump-ref" == elt.className && (a = elt.getAttribute('href'))) { + a = a.substr(1); elt.className += ' '+a; if (/[\[{]$/.test(elt.previousSibling.nodeValue)) { @@ -247,11 +247,7 @@ function a(e, f) { '.$this->dumpHeader; @@ -327,10 +322,10 @@ protected function style($style, $value, $attr = array()) return sprintf('%s', ord($r[0]), "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0]))); }, $v); - if ('solo-ref' === $style) { - return sprintf('%s', $v); - } if ('ref' === $style) { + if (empty($attr['count'])) { + return sprintf('%s', $v); + } $r = ('#' !== $v[0] ? 1 - ('@' !== $v[0]) : 2).substr($value, 1); return sprintf('%s', $this->dumpId, $r, 1 + $attr['count'], $v); @@ -400,8 +395,8 @@ function ($m) { ); if (-1 === $depth) { - parent::dumpLine(0); + AbstractDumper::dumpLine(0); } - parent::dumpLine($depth); + AbstractDumper::dumpLine($depth); } } diff --git a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php index 2916ba4506ea4..b3ec2adbe1257 100644 --- a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php @@ -64,7 +64,7 @@ public function testGet() "str" => "déjà" 7 => b"é@" "[]" => [] - "res" => :stream {@{$res1} + "res" => :stream {@{$res1} wrapper_type: "plainfile" stream_type: "STDIO" mode: "r" @@ -75,12 +75,12 @@ public function testGet() eof: false options: [] } - 8 => :Unknown {@{$res2}} + 8 => :Unknown {@{$res2}} "obj" => DumbFoo {#%d +foo: "foo" +"bar": "bar" } - "closure" => Closure {#%d + "closure" => Closure {#%d reflection: """ Closure [ <user> {$closureLabel} Symfony\Component\VarDumper\Tests\Fixture\{closure} ] { @@ {$var['file']} {$var['line']} - {$var['line']}