Skip to content

Commit 15bede5

Browse files
committed
[Console] refactored style management
The current code was broken when a style was defined inline: <bg=black>Foo</bg=black> When creatin a new style formatter, it's better to let the formatter apply the style to the text.
1 parent d03961e commit 15bede5

File tree

5 files changed

+35
-140
lines changed

5 files changed

+35
-140
lines changed

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

+5-63
Original file line numberDiff line numberDiff line change
@@ -125,48 +125,20 @@ public function getStyle($name)
125125
*/
126126
public function format($message)
127127
{
128-
$message = preg_replace_callback(
129-
$this->getBeginStyleRegex(), array($this, 'replaceBeginStyle'), $message
130-
);
131-
132-
return preg_replace_callback(
133-
$this->getEndStyleRegex(), array($this, 'replaceEndStyle'), $message
134-
);
128+
return preg_replace_callback('#<([a-z][a-z0-9_=;-]+)>(.*?)</\\1?>#i', array($this, 'replaceStyle'), $message);
135129
}
136130

137131
/**
138-
* Gets regex for a style start.
139-
*
140-
* @return string
141-
*/
142-
protected function getBeginStyleRegex()
143-
{
144-
return '#<([a-z][a-z0-9\-_=;]+)>#i';
145-
}
146-
147-
/**
148-
* Gets regex for a style end.
149-
*
150-
* @return string
151-
*/
152-
protected function getEndStyleRegex()
153-
{
154-
return '#</([a-z][a-z0-9\-_]*)?>#i';
155-
}
156-
157-
/**
158-
* Replaces the starting style of the output.
132+
* Replaces style of the output.
159133
*
160134
* @param array $match
161135
*
162136
* @return string The replaced style
163-
*
164-
* @throws \InvalidArgumentException When style is unknown
165137
*/
166-
private function replaceBeginStyle($match)
138+
private function replaceStyle($match)
167139
{
168140
if (!$this->isDecorated()) {
169-
return '';
141+
return $match[2];
170142
}
171143

172144
if (isset($this->styles[strtolower($match[1])])) {
@@ -179,37 +151,7 @@ private function replaceBeginStyle($match)
179151
}
180152
}
181153

182-
return $style->getBeginStyle();
183-
}
184-
185-
/**
186-
* Replaces the end style.
187-
*
188-
* @param string $match The text to match
189-
*
190-
* @return string The end style
191-
*/
192-
private function replaceEndStyle($match)
193-
{
194-
if (!$this->isDecorated()) {
195-
return '';
196-
}
197-
198-
if (!isset($match[1])) {
199-
$match[1] = '';
200-
}
201-
202-
if ('' == $match[1]) {
203-
$style = new OutputFormatterStyle();
204-
} else {
205-
if (!isset($this->styles[strtolower($match[1])])) {
206-
return $match[0];
207-
}
208-
209-
$style = $this->styles[strtolower($match[1])];
210-
}
211-
212-
return $style->getEndStyle();
154+
return $style->apply($match[2]);
213155
}
214156

215157
/**

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

+6-14
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,13 @@ public function setOptions(array $options)
184184
}
185185

186186
/**
187-
* Returns begin style code.
187+
* Applies the style to a given text.
188188
*
189-
* @return string
189+
* @param string $text The text to style
190+
*
191+
* @return string
190192
*/
191-
public function getBeginStyle()
193+
public function apply($text)
192194
{
193195
$codes = array();
194196

@@ -202,16 +204,6 @@ public function getBeginStyle()
202204
$codes = array_merge($codes, $this->options);
203205
}
204206

205-
return "\033[" . implode(';', $codes) . 'm';
206-
}
207-
208-
/**
209-
* Returns end style code.
210-
*
211-
* @return string
212-
*/
213-
public function getEndStyle()
214-
{
215-
return "\033[0m";
207+
return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
216208
}
217209
}

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,11 @@ function unsetOption($option);
6262
function setOptions(array $options);
6363

6464
/**
65-
* Returns begin style code.
65+
* Applies the style to a given text.
6666
*
67-
* @return string
68-
*/
69-
function getBeginStyle();
70-
71-
/**
72-
* Returns end style code.
67+
* @param string $text The text to style
7368
*
74-
* @return string
69+
* @return string
7570
*/
76-
function getEndStyle();
71+
function apply($text);
7772
}

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

+12-32
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,26 @@ class OutputFormatterStyleTest extends \PHPUnit_Framework_TestCase
1818
public function testConstructor()
1919
{
2020
$style = new OutputFormatterStyle('green', 'black', array('bold', 'underscore'));
21-
22-
$this->assertEquals("\033[32;40;1;4m", $style->getBeginStyle());
23-
$this->assertEquals("\033[0m", $style->getEndStyle());
21+
$this->assertEquals("\033[32;40;1;4mfoo\033[0m", $style->apply('foo'));
2422

2523
$style = new OutputFormatterStyle('red', null, array('blink'));
26-
27-
$this->assertEquals("\033[31;5m", $style->getBeginStyle());
28-
$this->assertEquals("\033[0m", $style->getEndStyle());
24+
$this->assertEquals("\033[31;5mfoo\033[0m", $style->apply('foo'));
2925

3026
$style = new OutputFormatterStyle(null, 'white');
31-
32-
$this->assertEquals("\033[47m", $style->getBeginStyle());
33-
$this->assertEquals("\033[0m", $style->getEndStyle());
27+
$this->assertEquals("\033[47mfoo\033[0m", $style->apply('foo'));
3428
}
3529

3630
public function testForeground()
3731
{
3832
$style = new OutputFormatterStyle();
3933

4034
$style->setForeground('black');
41-
42-
$this->assertEquals("\033[30m", $style->getBeginStyle());
43-
$this->assertEquals("\033[0m", $style->getEndStyle());
35+
$this->assertEquals("\033[30mfoo\033[0m", $style->apply('foo'));
4436

4537
$style->setForeground('blue');
46-
47-
$this->assertEquals("\033[34m", $style->getBeginStyle());
48-
$this->assertEquals("\033[0m", $style->getEndStyle());
38+
$this->assertEquals("\033[34mfoo\033[0m", $style->apply('foo'));
4939

5040
$this->setExpectedException('InvalidArgumentException');
51-
5241
$style->setForeground('undefined-color');
5342
}
5443

@@ -57,16 +46,12 @@ public function testBackground()
5746
$style = new OutputFormatterStyle();
5847

5948
$style->setBackground('black');
60-
61-
$this->assertEquals("\033[40m", $style->getBeginStyle());
62-
$this->assertEquals("\033[0m", $style->getEndStyle());
49+
$this->assertEquals("\033[40mfoo\033[0m", $style->apply('foo'));
6350

6451
$style->setBackground('yellow');
65-
66-
$this->assertEquals("\033[43m", $style->getBeginStyle());
52+
$this->assertEquals("\033[43mfoo\033[0m", $style->apply('foo'));
6753

6854
$this->setExpectedException('InvalidArgumentException');
69-
7055
$style->setBackground('undefined-color');
7156
}
7257

@@ -75,23 +60,18 @@ public function testOptions()
7560
$style = new OutputFormatterStyle();
7661

7762
$style->setOptions(array('reverse', 'conceal'));
78-
79-
$this->assertEquals("\033[7;8m", $style->getBeginStyle());
63+
$this->assertEquals("\033[7;8mfoo\033[0m", $style->apply('foo'));
8064

8165
$style->setOption('bold');
82-
83-
$this->assertEquals("\033[7;8;1m", $style->getBeginStyle());
66+
$this->assertEquals("\033[7;8;1mfoo\033[0m", $style->apply('foo'));
8467

8568
$style->unsetOption('reverse');
86-
87-
$this->assertEquals("\033[8;1m", $style->getBeginStyle());
69+
$this->assertEquals("\033[8;1mfoo\033[0m", $style->apply('foo'));
8870

8971
$style->setOption('bold');
90-
91-
$this->assertEquals("\033[8;1m", $style->getBeginStyle());
72+
$this->assertEquals("\033[8;1mfoo\033[0m", $style->apply('foo'));
9273

9374
$style->setOptions(array('bold'));
94-
95-
$this->assertEquals("\033[1m", $style->getBeginStyle());
75+
$this->assertEquals("\033[1mfoo\033[0m", $style->apply('foo'));
9676
}
9777
}

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

+8-22
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,18 @@ public function testNewStyle()
4343
{
4444
$formatter = new OutputFormatter(true);
4545

46-
$style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')
47-
->getMock();
46+
$style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')->getMock();
4847
$formatter->setStyle('test', $style);
4948

5049
$this->assertEquals($style, $formatter->getStyle('test'));
5150
$this->assertNotEquals($style, $formatter->getStyle('info'));
5251

5352
$style
5453
->expects($this->once())
55-
->method('getBeginStyle')
56-
->will($this->returnValue('[STYLE_BEG]'));
57-
58-
$style
59-
->expects($this->once())
60-
->method('getEndStyle')
61-
->will($this->returnValue('[STYLE_END]'));
54+
->method('apply')
55+
->will($this->returnValue('[STYLE_BEG]some custom msg[STYLE_END]'));
6256

63-
$this->assertEquals(
64-
"[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<test>some custom msg</test>')
65-
);
57+
$this->assertEquals("[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<test>some custom msg</test>'));
6658
}
6759

6860
public function testRedefineStyle()
@@ -75,13 +67,8 @@ public function testRedefineStyle()
7567

7668
$style
7769
->expects($this->once())
78-
->method('getBeginStyle')
79-
->will($this->returnValue('[STYLE_BEG]'));
80-
81-
$style
82-
->expects($this->once())
83-
->method('getEndStyle')
84-
->will($this->returnValue('[STYLE_END]'));
70+
->method('apply')
71+
->will($this->returnValue('[STYLE_BEG]some custom msg[STYLE_END]'));
8572

8673
$this->assertEquals(
8774
"[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<info>some custom msg</info>')
@@ -92,9 +79,8 @@ public function testInlineStyle()
9279
{
9380
$formatter = new OutputFormatter(true);
9481

95-
$this->assertEquals(
96-
"\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</>')
97-
);
82+
$this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</>'));
83+
$this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
9884
}
9985

10086
public function testNotDecoratedFormatter()

0 commit comments

Comments
 (0)