Skip to content

Commit 0f65a76

Browse files
committed
[Console][VarDumper] Ignore href for PhpStorm terminal emulator
1 parent 32a53bf commit 0f65a76

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
5454
private $background;
5555
private $href;
5656
private $options = array();
57+
private $handlesHrefGracefully;
5758

5859
/**
5960
* Initializes output formatter style.
@@ -185,6 +186,10 @@ public function apply($text)
185186
$setCodes = array();
186187
$unsetCodes = array();
187188

189+
if (null === $this->handlesHrefGracefully) {
190+
$this->handlesHrefGracefully = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR');
191+
}
192+
188193
if (null !== $this->foreground) {
189194
$setCodes[] = $this->foreground['set'];
190195
$unsetCodes[] = $this->foreground['unset'];
@@ -199,7 +204,7 @@ public function apply($text)
199204
$unsetCodes[] = $option['unset'];
200205
}
201206

202-
if (null !== $this->href) {
207+
if (null !== $this->href && $this->handlesHrefGracefully) {
203208
$text = "\033]8;;$this->href\033\\$text\033]8;;\033\\";
204209
}
205210

src/Symfony/Component/Console/Tests/Formatter/OutputFormatterStyleTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,19 @@ public function testOptions()
9797
$this->assertContains('Invalid option specified: "foo"', $e->getMessage(), '->unsetOption() throws an \InvalidArgumentException when the option does not exist in the available options');
9898
}
9999
}
100+
101+
public function testHref()
102+
{
103+
$prevTerminalEmulator = getenv('TERMINAL_EMULATOR');
104+
putenv('TERMINAL_EMULATOR');
105+
106+
$style = new OutputFormatterStyle();
107+
108+
try {
109+
$style->setHref('idea://open/?file=/path/SomeFile.php&line=12');
110+
$this->assertSame("\e]8;;idea://open/?file=/path/SomeFile.php&line=12\e\\some URL\e]8;;\e\\", $style->apply('some URL'));
111+
} finally {
112+
putenv('TERMINAL_EMULATOR'.($prevTerminalEmulator ? "=$prevTerminalEmulator" : ''));
113+
}
114+
}
100115
}

src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,17 @@ public function testFormatterHasStyles()
241241
/**
242242
* @dataProvider provideDecoratedAndNonDecoratedOutput
243243
*/
244-
public function testNotDecoratedFormatter(string $input, string $expectedNonDecoratedOutput, string $expectedDecoratedOutput)
244+
public function testNotDecoratedFormatter(string $input, string $expectedNonDecoratedOutput, string $expectedDecoratedOutput, string $terminalEmulator = 'foo')
245245
{
246-
$this->assertEquals($expectedDecoratedOutput, (new OutputFormatter(true))->format($input));
247-
$this->assertEquals($expectedNonDecoratedOutput, (new OutputFormatter(false))->format($input));
246+
$prevTerminalEmulator = getenv('TERMINAL_EMULATOR');
247+
putenv('TERMINAL_EMULATOR='.$terminalEmulator);
248+
249+
try {
250+
$this->assertEquals($expectedDecoratedOutput, (new OutputFormatter(true))->format($input));
251+
$this->assertEquals($expectedNonDecoratedOutput, (new OutputFormatter(false))->format($input));
252+
} finally {
253+
putenv('TERMINAL_EMULATOR'.($prevTerminalEmulator ? "=$prevTerminalEmulator" : ''));
254+
}
248255
}
249256

250257
public function provideDecoratedAndNonDecoratedOutput()
@@ -256,6 +263,7 @@ public function provideDecoratedAndNonDecoratedOutput()
256263
array('<question>some question</question>', 'some question', "\033[30;46msome question\033[39;49m"),
257264
array('<fg=red>some text with inline style</>', 'some text with inline style', "\033[31msome text with inline style\033[39m"),
258265
array('<href=idea://open/?file=/path/SomeFile.php&line=12>some URL</>', 'some URL', "\033]8;;idea://open/?file=/path/SomeFile.php&line=12\033\\some URL\033]8;;\033\\"),
266+
array('<href=idea://open/?file=/path/SomeFile.php&line=12>some URL</>', 'some URL', 'some URL', 'JetBrains-JediTerm'),
259267
);
260268
}
261269

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class CliDumper extends AbstractDumper
5959
'fileLinkFormat' => null,
6060
);
6161

62+
private $handlesHrefGracefully;
63+
6264
/**
6365
* {@inheritdoc}
6466
*/
@@ -431,6 +433,10 @@ protected function style($style, $value, $attr = array())
431433
$this->colors = $this->supportsColors();
432434
}
433435

436+
if (null === $this->handlesHrefGracefully) {
437+
$this->handlesHrefGracefully = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR');
438+
}
439+
434440
if (isset($attr['ellipsis'], $attr['ellipsis-type'])) {
435441
$prefix = substr($value, 0, -$attr['ellipsis']);
436442
if ('cli' === \PHP_SAPI && 'path' === $attr['ellipsis-type'] && isset($_SERVER[$pwd = '\\' === \DIRECTORY_SEPARATOR ? 'CD' : 'PWD']) && 0 === strpos($prefix, $_SERVER[$pwd])) {
@@ -477,7 +483,7 @@ protected function style($style, $value, $attr = array())
477483
}
478484

479485
href:
480-
if ($this->colors) {
486+
if ($this->colors && $this->handlesHrefGracefully) {
481487
if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], isset($attr['line']) ? $attr['line'] : 0)) {
482488
$attr['href'] = $href;
483489
}

src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@
2121
class CliDescriptorTest extends TestCase
2222
{
2323
private static $timezone;
24+
private static $prevTerminalEmulator;
2425

2526
public static function setUpBeforeClass()
2627
{
2728
self::$timezone = date_default_timezone_get();
2829
date_default_timezone_set('UTC');
30+
31+
self::$prevTerminalEmulator = getenv('TERMINAL_EMULATOR');
32+
putenv('TERMINAL_EMULATOR');
2933
}
3034

3135
public static function tearDownAfterClass()
3236
{
3337
date_default_timezone_set(self::$timezone);
38+
putenv('TERMINAL_EMULATOR'.(self::$prevTerminalEmulator ? '='.self::$prevTerminalEmulator : ''));
3439
}
3540

3641
/**

0 commit comments

Comments
 (0)