merged branch jfsimon/issue-5196 (PR #5197)

Commits
-------

c2207da [Console] Removed unused phpunit annotation.
c0c61da [Console] Added current style appliance for all styled text.
696a653 [Console] Removed text transformation for empty styles.
c7e324a [Console] Added test for non style tag formatting.

Discussion
----------

[Console] Non style tags dont disturb formatting anymore

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes

Fixes issue #5196.

---------------------------------------------------------------------------

by travisbot at 2012-08-06T19:35:20Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/2050455) (merged c0c61da0 into 842b599c).

---------------------------------------------------------------------------

by travisbot at 2012-08-07T07:47:31Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/2055153) (merged c2207da6 into 842b599c).
This commit is contained in:
Fabien Potencier 2012-08-07 10:07:43 +02:00
commit 13454efe30
3 changed files with 19 additions and 10 deletions

View File

@ -171,7 +171,7 @@ class OutputFormatter implements OutputFormatterInterface
{ {
// we got "\<" escaped char // we got "\<" escaped char
if ('\\' === $match[1]) { if ('\\' === $match[1]) {
return $match[0]; return $this->applyCurrentStyle($match[0]);
} }
if ('' === $match[3]) { if ('' === $match[3]) {
@ -179,11 +179,11 @@ class OutputFormatter implements OutputFormatterInterface
// we got "</>" tag // we got "</>" tag
$this->styleStack->pop(); $this->styleStack->pop();
return $this->applyStyle($this->styleStack->getCurrent(), $match[4]); return $this->applyCurrentStyle($match[4]);
} }
// we got "<>" tag // we got "<>" tag
return '<>'.$match[4]; return '<>'.$this->applyCurrentStyle($match[4]);
} }
if (isset($this->styles[strtolower($match[3])])) { if (isset($this->styles[strtolower($match[3])])) {
@ -192,7 +192,7 @@ class OutputFormatter implements OutputFormatterInterface
$style = $this->createStyleFromString($match[3]); $style = $this->createStyleFromString($match[3]);
if (false === $style) { if (false === $style) {
return $match[0]; return $this->applyCurrentStyle($match[0]);
} }
} }
@ -202,7 +202,7 @@ class OutputFormatter implements OutputFormatterInterface
$this->styleStack->push($style); $this->styleStack->push($style);
} }
return $this->applyStyle($this->styleStack->getCurrent(), $match[4]); return $this->applyCurrentStyle($match[4]);
} }
/** /**
@ -235,15 +235,14 @@ class OutputFormatter implements OutputFormatterInterface
} }
/** /**
* Applies style to text if must be applied. * Applies current style from stack to text, if must be applied.
* *
* @param OutputFormatterStyleInterface $style Style to apply * @param string $text Input text
* @param string $text Input text
* *
* @return string string Styled text * @return string string Styled text
*/ */
private function applyStyle(OutputFormatterStyleInterface $style, $text) private function applyCurrentStyle($text)
{ {
return $this->isDecorated() && strlen($text) > 0 ? $style->apply($text) : $text; return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
} }
} }

View File

@ -213,6 +213,10 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
$codes = array_merge($codes, $this->options); $codes = array_merge($codes, $this->options);
} }
if (0 === count($codes)) {
return $text;
}
return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text); return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
} }
} }

View File

@ -115,6 +115,12 @@ class FormatterStyleTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>')); $this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
} }
public function testNonStyleTag()
{
$formatter = new OutputFormatter(true);
$this->assertEquals("\033[32msome \033[0m\033[32m<tag> styled\033[0m", $formatter->format('<info>some <tag> styled</info>'));
}
public function testNotDecoratedFormatter() public function testNotDecoratedFormatter()
{ {
$formatter = new OutputFormatter(false); $formatter = new OutputFormatter(false);