Skip to content

Commit 13454ef

Browse files
committed
merged branch jfsimon/issue-5196 (PR symfony#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 symfony#5196. --------------------------------------------------------------------------- by travisbot at 2012-08-06T19:35:20Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/2050455) (merged c0c61da into 842b599). --------------------------------------------------------------------------- by travisbot at 2012-08-07T07:47:31Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/2055153) (merged c2207da into 842b599).
2 parents ca3aa9c + c2207da commit 13454ef

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,19 @@ private function replaceStyle($match)
171171
{
172172
// we got "\<" escaped char
173173
if ('\\' === $match[1]) {
174-
return $match[0];
174+
return $this->applyCurrentStyle($match[0]);
175175
}
176176

177177
if ('' === $match[3]) {
178178
if ('/' === $match[2]) {
179179
// we got "</>" tag
180180
$this->styleStack->pop();
181181

182-
return $this->applyStyle($this->styleStack->getCurrent(), $match[4]);
182+
return $this->applyCurrentStyle($match[4]);
183183
}
184184

185185
// we got "<>" tag
186-
return '<>'.$match[4];
186+
return '<>'.$this->applyCurrentStyle($match[4]);
187187
}
188188

189189
if (isset($this->styles[strtolower($match[3])])) {
@@ -192,7 +192,7 @@ private function replaceStyle($match)
192192
$style = $this->createStyleFromString($match[3]);
193193

194194
if (false === $style) {
195-
return $match[0];
195+
return $this->applyCurrentStyle($match[0]);
196196
}
197197
}
198198

@@ -202,7 +202,7 @@ private function replaceStyle($match)
202202
$this->styleStack->push($style);
203203
}
204204

205-
return $this->applyStyle($this->styleStack->getCurrent(), $match[4]);
205+
return $this->applyCurrentStyle($match[4]);
206206
}
207207

208208
/**
@@ -235,15 +235,14 @@ private function createStyleFromString($string)
235235
}
236236

237237
/**
238-
* Applies style to text if must be applied.
238+
* Applies current style from stack to text, if must be applied.
239239
*
240-
* @param OutputFormatterStyleInterface $style Style to apply
241-
* @param string $text Input text
240+
* @param string $text Input text
242241
*
243242
* @return string string Styled text
244243
*/
245-
private function applyStyle(OutputFormatterStyleInterface $style, $text)
244+
private function applyCurrentStyle($text)
246245
{
247-
return $this->isDecorated() && strlen($text) > 0 ? $style->apply($text) : $text;
246+
return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
248247
}
249248
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ public function apply($text)
213213
$codes = array_merge($codes, $this->options);
214214
}
215215

216+
if (0 === count($codes)) {
217+
return $text;
218+
}
219+
216220
return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
217221
}
218222
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public function testInlineStyle()
115115
$this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
116116
}
117117

118+
public function testNonStyleTag()
119+
{
120+
$formatter = new OutputFormatter(true);
121+
$this->assertEquals("\033[32msome \033[0m\033[32m<tag> styled\033[0m", $formatter->format('<info>some <tag> styled</info>'));
122+
}
123+
118124
public function testNotDecoratedFormatter()
119125
{
120126
$formatter = new OutputFormatter(false);

0 commit comments

Comments
 (0)