Skip to content

Commit

Permalink
minor symfony#12385 [VarDumper] symfony.com color scheme on the CLI (…
Browse files Browse the repository at this point in the history
…nicolas-grekas)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[VarDumper] symfony.com color scheme on the CLI

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony#12355, symfony#12391
| License       | MIT
| Doc PR        | -

Commits
-------

d5620bf [VarDumper] symfony.com color scheme on the CLI
  • Loading branch information
fabpot committed Nov 3, 2014
2 parents 4b01bc0 + d5620bf commit d0e9e65
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 39 deletions.
70 changes: 51 additions & 19 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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)
{
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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)).' ';
}
}
}
Expand All @@ -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];
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}
29 changes: 12 additions & 17 deletions src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -247,11 +247,7 @@ function a(e, f) {
<style>
pre.sf-dump {
display: block;
background-color: #18171B;
white-space: pre;
line-height: 1.2em;
color: #FF8400;
font: 12px Menlo, Monaco, Consolas, monospace;
padding: 5px;
}
pre.sf-dump span {
Expand All @@ -266,7 +262,6 @@ function a(e, f) {
cursor: help;
}
pre.sf-dump a {
color: #eee8d5;
text-decoration: none;
cursor: pointer;
border: 0;
Expand All @@ -275,7 +270,7 @@ function a(e, f) {
EOHTML;

foreach ($this->styles as $class => $style) {
$line .= "pre.sf-dump .sf-dump-$class {{$style}}";
$line .= 'pre.sf-dump'.('default' !== $class ? ' .sf-dump-'.$class : '').'{'.$style.'}';
}

return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
Expand Down Expand Up @@ -327,10 +322,10 @@ protected function style($style, $value, $attr = array())
return sprintf('<span class=sf-dump-cchr title=\\x%02X>%s</span>', ord($r[0]), "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0])));
}, $v);

if ('solo-ref' === $style) {
return sprintf('<a class=sf-dump-solo-ref>%s</a>', $v);
}
if ('ref' === $style) {
if (empty($attr['count'])) {
return sprintf('<a class=sf-dump-ref>%s</a>', $v);
}
$r = ('#' !== $v[0] ? 1 - ('@' !== $v[0]) : 2).substr($value, 1);

return sprintf('<a class=sf-dump-ref href=#%s-ref%s title="%d occurrences">%s</a>', $this->dumpId, $r, 1 + $attr['count'], $v);
Expand Down Expand Up @@ -400,8 +395,8 @@ function ($m) {
);

if (-1 === $depth) {
parent::dumpLine(0);
AbstractDumper::dumpLine(0);
}
parent::dumpLine($depth);
AbstractDumper::dumpLine($depth);
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testGet()
"<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="4 characters">d&#233;j&#224;</span>"
<span class=sf-dump-key>7</span> => b"<span class=sf-dump-str title="2 binary or non-UTF-8 characters">&#233;<span class=sf-dump-cchr title=\\x00>@</span></span>"
"<span class=sf-dump-key>[]</span>" => []
"<span class=sf-dump-key>res</span>" => <abbr title="`stream` resource" class=sf-dump-note>:stream</abbr> {<a class=sf-dump-solo-ref>@{$res1}</a><samp>
"<span class=sf-dump-key>res</span>" => <abbr title="`stream` resource" class=sf-dump-note>:stream</abbr> {<a class=sf-dump-ref>@{$res1}</a><samp>
<span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"
<span class=sf-dump-meta>stream_type</span>: "<span class=sf-dump-str title="5 characters">STDIO</span>"
<span class=sf-dump-meta>mode</span>: "<span class=sf-dump-str>r</span>"
Expand All @@ -75,12 +75,12 @@ public function testGet()
<span class=sf-dump-meta>eof</span>: <span class=sf-dump-const>false</span>
<span class=sf-dump-meta>options</span>: []
</samp>}
<span class=sf-dump-key>8</span> => <abbr title="`Unknown` resource" class=sf-dump-note>:Unknown</abbr> {<a class=sf-dump-solo-ref>@{$res2}</a>}
<span class=sf-dump-key>8</span> => <abbr title="`Unknown` resource" class=sf-dump-note>:Unknown</abbr> {<a class=sf-dump-ref>@{$res2}</a>}
"<span class=sf-dump-key>obj</span>" => <abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="2 occurrences">#%d</a><samp id={$dumpId}-ref2%d>
+<span class=sf-dump-public title="Public property">foo</span>: "<span class=sf-dump-str title="3 characters">foo</span>"
+"<span class=sf-dump-public title="Runtime added dynamic property">bar</span>": "<span class=sf-dump-str title="3 characters">bar</span>"
</samp>}
"<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {<a class=sf-dump-solo-ref>#%d</a><samp>
"<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {<a class=sf-dump-ref>#%d</a><samp>
<span class=sf-dump-meta>reflection</span>: """
<span class=sf-dump-str title="%d characters">Closure [ &lt;user&gt; {$closureLabel} Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {</span>
<span class=sf-dump-str title="%d characters"> @@ {$var['file']} {$var['line']} - {$var['line']}</span>
Expand Down

0 comments on commit d0e9e65

Please sign in to comment.