8000 [Console] ConsoleOuput::isDecorated is detected from STDOUT and STDERR · symfony/symfony@e8f300c · GitHub
[go: up one dir, main page]

Skip to content

Commit e8f300c

Browse files
committed
[Console] ConsoleOuput::isDecorated is detected from STDOUT and STDERR
1 parent 5f6d235 commit e8f300c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Symfony/Component/Console/Output/ConsoleOutput.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,21 @@ public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = nu
4747
if (!$this->hasStdoutSupport()) {
4848
$outputStream = 'php://output';
4949
}
50+
$autodetection = null === $decorated;
5051

5152
parent::__construct(fopen($outputStream, 'w'), $verbosity, $decorated, $formatter);
5253

54+
// remember autodetected value for stdout and reset
55+
$stdoutIsDecorated = $this->isDecorated();
56+
if ($autodetection) {
57+
$this->getFormatter()->setDecorated(null);
58+
}
59+
5360
$this->stderr = new StreamOutput(fopen('php://stderr', 'w'), $verbosity, $decorated, $this->getFormatter());
61+
62+
if ($this->stderr->isDecorated() !== $stdoutIsDecorated) {
63+
$this->setDecorated(false);
64+
}
5465
}
5566

5667
/**

src/Symfony/Component/Console/Tests/Output/ConsoleOutputTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
use Symfony\Component\Console\Output\ConsoleOutput;
1515
use Symfony\Component\Console\Output\Output;
16+
use Symfony\Component\Console\Formatter\OutputFormatter;
17+
18+
class ConsoleOutputWithMixedDecoratedSupport extends ConsoleOutput
19+
{
20+
protected function hasColorSupport()
21+
{
22+
// emulate posix_isatty(STDOUT) !== posix_isatty(STDERR)
23+
return !parent::hasColorSupport();
24+
}
25+
}
1626

1727
class ConsoleOutputTest extends \PHPUnit_Framework_TestCase
1828
{
@@ -22,4 +32,31 @@ public function testConstructor()
2232
$this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
2333
$this->assertSame($output->getFormatter(), $output->getErrorOutput()->getFormatter(), '__construct() takes a formatter or null as the third argument');
2434
}
35+
36+
public function testIsDecorated()
37+
{
38+
$output = new ConsoleOutput(Output::VERBOSITY_QUIET, true);
39+
40+
$this->assertTrue($output->isDecorated());
41+
$this->assertTrue($output->getErrorOutput()->isDecorated());
42+
43+
$output = new ConsoleOutput(Output::VERBOSITY_QUIET, false);
44+
45+
$this->assertFalse($output->isDecorated());
46+
$this->assertFalse($output->getErrorOutput()->isDecorated());
47+
}
48+
49+
public function testMixedDecoratedSupportDetection()
50+
{
51+
$formatter = new OutputFormatter();
52+
$output = new ConsoleOutputWithMixedDecoratedSupport(Output::VERBOSITY_QUIET, null, $formatter);
53+
54+
$this->assertFalse($formatter->isDecorated());
55+
$this->assertFalse($output->isDecorated());
56+
$this->assertFalse($output->getErrorOutput()->isDecorated());
57+
58+
$output = new ConsoleOutputWithMixedDecoratedSupport(Output::VERBOSITY_QUIET);
59+
$this->assertFalse($output->isDecorated());
60+
$this->assertFalse($output->getErrorOutput()->isDecorated());
61+
}
2562
}

0 commit comments

Comments
 (0)
0