Skip to content

Commit 8f14e58

Browse files
[VarDumper] symfony.com color scheme on the CLI
1 parent 0e4953c commit 8f14e58

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
lines changed

Dumper/CliDumper.php

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,46 @@ class CliDumper extends AbstractDumper
2828
protected $maxStringWidth = 0;
2929
protected $styles = array(
3030
// See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
31-
'num' => '1;38;5;33',
32-
'const' => '1;38;5;33',
33-
'str' => '1;38;5;37',
31+
'default' => '38;5;208',
32+
'num' => '1;38;5;38',
33+
'const' => '1;38;5;208',
34+
'str' => '1;38;5;113',
3435
'cchr' => '7',
35-
'note' => '38;5;178',
36-
'ref' => '38;5;240',
37-
'solo-ref' => '38;5;240',
38-
'public' => '38;5;28',
39-
'protected' => '38;5;166',
40-
'private' => '38;5;160',
41-
'meta' => '38;5;27',
42-
'key' => '38;5;27',
43-
'index' => '38;5;27',
36+
'note' => '38;5;38',
37+
'ref' => '38;5;247',
38+
'public' => '',
39+
'protected' => '',
40+
'private' => '',
41+
'meta' => '38;5;170',
42+
'key' => '38;5;113',
43+
'index' => '38;5;38',
4444
);
4545

4646
protected static $controlCharsRx = '/[\x00-\x1F\x7F]/';
4747

48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function __construct($output = null)
52+
{
53+
parent::__construct($output);
54+
55+
if (defined('PHP_WINDOWS_VERSION_MAJOR') && false !== @getenv('ANSICON')) {
56+
// Use only the base 16 xterm colors when using ANSICON
57+
$this->setStyles(array(
58+
'default' => '31',
59+
'num' => '1;34',
60+
'const' => '1;31',
61+
'str' => '1;32',
62+
'note' => '34',
63+
'ref' => '1;30',
64+
'meta' => '35',
65+
'key' => '32',
66+
'index' => '34',
67+
));
68+
}
69+
}
70+
4871
/**
4972
* Enables/disables colored output.
5073
*
@@ -70,7 +93,7 @@ public function setMaxStringWidth($maxStringWidth)
7093
/**
7194
* Configures styles.
7295
*
73-
* @param array $styles A map of style namaes to style definitions.
96+
* @param array $styles A map of style names to style definitions.
7497
*/
7598
public function setStyles(array $styles)
7699
{
@@ -205,10 +228,8 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
205228
$prefix = $class ? $this->style('note', 'array:'.$class).' [' : '[';
206229
}
207230

208-
if ($cursor->softRefCount) {
231+
if ($cursor->softRefCount || 0 < $cursor->softRefHandle) {
209232
$prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), array('count' => $cursor->softRefCount));
210-
} elseif (0 < $cursor->softRefHandle) {
211-
$prefix .= $this->style('solo-ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').$cursor->softRefHandle);
212233
} elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) {
213234
$prefix .= $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount));
214235
}
@@ -310,7 +331,7 @@ protected function dumpKey(Cursor $cursor)
310331
}
311332

312333
if ($cursor->hardRefTo) {
313-
$this->line .= ($cursor->hardRefCount ? $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount)) : $this->style('solo-ref', '&')).' ';
334+
$this->line .= $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount)).' ';
314335
}
315336
}
316337
}
@@ -327,7 +348,7 @@ protected function dumpKey(Cursor $cursor)
327348
protected function style($style, $value, $attr = array())
328349
{
329350
if (null === $this->colors) {
330-
$this->colors = $this->supportsColors($this->outputStream);
351+
$this->colors = $this->supportsColors();
331352
}
332353

333354
$style = $this->styles[$style];
@@ -336,7 +357,7 @@ protected function style($style, $value, $attr = array())
336357
return sprintf($cchr, "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0])));
337358
}, $value);
338359

339-
return $this->colors ? sprintf("\033[%sm%s\033[m", $style, $value) : $value;
360+
return $this->colors ? sprintf("\033[%sm%s\033[m\033[%sm", $style, $value, $this->styles['default']) : $value;
340361
}
341362

342363
/**
@@ -385,4 +406,15 @@ protected function supportsColors()
385406

386407
return static::$defaultColors;
387408
}
409+
410+
/**
411+
* {@inheritdoc}
412+
*/
413+
protected function dumpLine($depth)
414+
{
415+
if ($this->colors) {
416+
$this->line = sprintf("\033[%sm%s\033[m", $this->styles['default'], $this->line);
417+
}
418+
parent::dumpLine($depth);
419+
}
388420
}

Dumper/HtmlDumper.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ class HtmlDumper extends CliDumper
3131
protected $headerIsDumped = false;
3232
protected $lastDepth = -1;
3333
protected $styles = array(
34-
'num' => 'font-weight:bold;color:#1299DA',
34+
'default' => 'background-color:#18171B; color:#FF8400; line-height:1.2em; font:12px Menlo, Monaco, Consolas, monospace',
35+
'num' => 'font-weight:bold; color:#1299DA',
3536
'const' => 'font-weight:bold',
36-
'str' => 'font-weight:bold;color:#56DB3A',
37-
'cchr' => 'font-style: italic',
37+
'str' => 'font-weight:bold; color:#56DB3A',
38+
'cchr' => 'font-style:italic',
3839
'note' => 'color:#1299DA',
3940
'ref' => 'color:#A0A0A0',
40-
'solo-ref' => 'color:#A0A0A0',
4141
'public' => 'color:#FFFFFF',
4242
'protected' => 'color:#FFFFFF',
4343
'private' => 'color:#FFFFFF',
@@ -213,8 +213,8 @@ function a(e, f) {
213213
if ('sf-dump' != elt.parentNode.className) {
214214
toggle(a);
215215
}
216-
} else if ("sf-dump-ref" == elt.className) {
217-
a = elt.getAttribute('href').substr(1);
216+
} else if ("sf-dump-ref" == elt.className && (a = elt.getAttribute('href'))) {
217+
a = a.substr(1);
218218
elt.className += ' '+a;
219219
220220
if (/[\[{]$/.test(elt.previousSibling.nodeValue)) {
@@ -247,11 +247,7 @@ function a(e, f) {
247247
<style>
248248
pre.sf-dump {
249249
display: block;
250-
background-color: #18171B;
251250
white-space: pre;
252-
line-height: 1.2em;
253-
color: #FF8400;
254-
font: 12px Menlo, Monaco, Consolas, monospace;
255251
padding: 5px;
256252
}
257253
pre.sf-dump span {
@@ -266,7 +262,6 @@ function a(e, f) {
266262
cursor: help;
267263
}
268264
pre.sf-dump a {
269-
color: #eee8d5;
270265
text-decoration: none;
271266
cursor: pointer;
272267
border: 0;
@@ -275,7 +270,7 @@ function a(e, f) {
275270
EOHTML;
276271

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

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

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

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

402397
if (-1 === $depth) {
403-
parent::dumpLine(0);
398+
AbstractDumper::dumpLine(0);
404399
}
405-
parent::dumpLine($depth);
400+
AbstractDumper::dumpLine($depth);
406401
}
407402
}

Tests/HtmlDumperTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testGet()
6464
"<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="4 characters">d&#233;j&#224;</span>"
6565
<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>"
6666
"<span class=sf-dump-key>[]</span>" => []
67-
"<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>
67+
"<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>
6868
<span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"
6969
<span class=sf-dump-meta>stream_type</span>: "<span class=sf-dump-str title="5 characters">STDIO</span>"
7070
<span class=sf-dump-meta>mode</span>: "<span class=sf-dump-str>r</span>"
@@ -75,12 +75,12 @@ public function testGet()
7575
<span class=sf-dump-meta>eof</span>: <span class=sf-dump-const>false</span>
7676
<span class=sf-dump-meta>options</span>: []
7777
</samp>}
78-
<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>}
78+
<span class=sf-dump-key>8</span> => <abbr title="`Unknown` resource" class=sf-dump-note>:Unknown</abbr> {<a class=sf-dump-ref>@{$res2}</a>}
7979
"<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>
8080
+<span class=sf-dump-public title="Public property">foo</span>: "<span class=sf-dump-str title="3 characters">foo</span>"
8181
+"<span class=sf-dump-public title="Runtime added dynamic property">bar</span>": "<span class=sf-dump-str title="3 characters">bar</span>"
8282
</samp>}
83-
"<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {<a class=sf-dump-solo-ref>#%d</a><samp>
83+
"<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {<a class=sf-dump-ref>#%d</a><samp>
8484
<span class=sf-dump-meta>reflection</span>: """
8585
<span class=sf-dump-str title="%d characters">Closure [ &lt;user&gt; {$closureLabel} Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {</span>
8686
<span class=sf-dump-str title="%d characters"> @@ {$var['file']} {$var['line']} - {$var['line']}</span>

0 commit comments

Comments
 (0)