8000 [Console] Fixed unsetting of setted attributes on OutputFormatterStyle · symfony/symfony@ce0c4b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce0c4b4

Browse files
Danilo SilvaDanilo Silva
Danilo Silva
authored and
Danilo Silva
committed
[Console] Fixed unsetting of setted attributes on OutputFormatterStyle
Unset only previous setted styles in place of all styles, before this fix, OutputFormatterStyle reset all attributes with '\e[0m' sequence. Now, if a foreground is setted, reset only the foreground so you can use innested attributes Conflicts: src/Symfony/Component/Console/Tests/Formatter/OutputFormatterStyleTest.php src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php src/Symfony/Component/Console/Tests/Output/OutputTest.php
1 parent 71dc07c commit ce0c4b4

File tree

5 files changed

+83
-77
lines changed

5 files changed

+83
-77
lines changed

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

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,31 @@
2121
class OutputFormatterStyle implements OutputFormatterStyleInterface
2222
{
2323
private static $availableForegroundColors = array(
24-
'black' => 30,
25-
'red' => 31,
26-
'green' => 32,
27-
'yellow' => 33,
28-
'blue' => 34,
29-
'magenta' => 35,
30-
'cyan' => 36,
31-
'white' => 37
24+
'black' => array('set' => 30, 'unset' => 39),
25+
'red' => array('set' => 31, 'unset' => 39),
26+
'green' => array('set' => 32, 'unset' => 39),
27+
'yellow' => array('set' => 33, 'unset' => 39),
28+
'blue' => array('set' => 34, 'unset' => 39),
29+
'magenta' => array('set' => 35, 'unset' => 39),
30+
'cyan' => array('set' => 36, 'unset' => 39),
31+
'white' => array('set' => 37, 'unset' => 39)
3232
);
3333
private static $availableBackgroundColors = array(
34-
'black' => 40,
35-
'red' => 41,
36-
'green' => 42,
37-
'yellow' => 43,
38-
'blue' => 44,
39-
'magenta' => 45,
40-
'cyan' => 46,
41-
'white' => 47
34+
'black' => array('set' => 40, 'unset' => 49),
35+
'red' => array('set' => 41, 'unset' => 49),
36+
'green' => array('set' => 42, 'unset' => 49),
37+
'yellow' => array('set' => 43, 'unset' => 49),
38+
'blue' => array('set' => 44, 'unset' => 49),
39+
'magenta' => array('set' => 45, 'unset' => 49),
40+
'cyan' => array('set' => 46, 'unset' => 49),
41+
'white' => array('set' => 47, 'unset' => 49)
4242
);
4343
private static $availableOptions = array(
44-
'bold' => 1,
45-
'underscore' => 4,
46-
'blink' => 5,
47-
'reverse' => 7,
48-
'conceal' => 8
44+
'bold' => array('set' => 1, 'unset' => 21),
45+
'underscore' => array('set' => 4, 'unset' => 24),
46+
'blink' => array('set' => 5, 'unset' => 25),
47+
'reverse' => array('set' => 7, 'unset' => 27),
48+
'conceal' => array('set' => 8, 'unset' => 28)
4949
);
5050

5151
private $foreground;
@@ -201,22 +201,28 @@ public function setOptions(array $options)
201201
*/
202202
public function apply($text)
203203
{
204-
$codes = array();
204+
$setCodes = array();
205+
$unsetCode = array();
205206

206207
if (null !== $this->foreground) {
207-
$codes[] = $this->foreground;
208+
$setCodes[] = $this->foreground['set'];
209+
$unsetCodes[] = $this->foreground['unset'];
208210
}
209211
if (null !== $this->background) {
210-
$codes[] = $this->background;
212+
$setCodes[] = $this->background['set'];
213+
$unsetCodes[] = $this->background['unset'];
211214
}
212215
if (count($this->options)) {
213-
$codes = array_merge($codes, $this->options);
216+
foreach ($this->options as $option) {
217+
$setCodes[] = $option['set'];
218+
$unsetCodes[] = $option['unset'];
219+
}
214220
}
215221

216-
if (0 === count($codes)) {
222+
if (0 === count($setCodes)) {
217223
return $text;
218224
}
219225

220-
return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
226+
return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
221227
}
222228
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11

22

3-
 
4-
 [Exception] 
5-
 Third exception comment 
6-
 
3+
 
4+
 [Exception] 
5+
 Third exception comment 
6+
 
77

88

99

1010

11-
 
12-
 [Exception] 
13-
 Second exception comment 
14-
 
11+
 
12+
 [Exception] 
13+
 Second exception comment 
14+
 
1515

1616

1717

1818

19-
 
20-
 [Exception] 
21-
 First exception <p>this is html</p> 
22-
 
19+
 
20+
 [Exception] 
21+
 First exception <p>this is html</p> 
22+
 
2323

2424

25-
foo3:bar
25+
foo3:bar
2626

2727

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ class OutputFormatterStyleTest extends \PHPUnit_Framework_TestCase
1818
public function testConstructor()
1919
{
2020
$style = new OutputFormatterStyle('green', 'black', array('bold', 'underscore'));
21-
$this->assertEquals("\033[32;40;1;4mfoo\033[0m", $style->apply('foo'));
21+
$this->assertEquals("\033[32;40;1;4mfoo\033[39;49;21;24m", $style->apply('foo'));
2222

2323
$style = new OutputFormatterStyle('red', null, array('blink'));
24-
$this->assertEquals("\033[31;5mfoo\033[0m", $style->apply('foo'));
24+
$this->assertEquals("\033[31;5mfoo\033[39;25m", $style->apply('foo'));
2525

2626
$style = new OutputFormatterStyle(null, 'white');
27-
$this->assertEquals("\033[47mfoo\033[0m", $style->apply('foo'));
27+
$this->assertEquals("\033[47mfoo\033[49m", $style->apply('foo'));
2828
}
2929

3030
public function testForeground()
3131
{
3232
$style = new OutputFormatterStyle();
3333

3434
$style->setForeground('black');
35-
$this->assertEquals("\033[30mfoo\033[0m", $style->apply('foo'));
35+
$this->assertEquals("\033[30mfoo\033[39m", $style->apply('foo'));
3636

3737
$style->setForeground('blue');
38-
$this->assertEquals("\033[34mfoo\033[0m", $style->apply('foo'));
38+
$this->assertEquals("\033[34mfoo\033[39m", $style->apply('foo'));
3939

4040
$this->setExpectedException('InvalidArgumentException');
4141
$style->setForeground('undefined-color');
@@ -46,10 +46,10 @@ public function testBackground()
4646
$style = new OutputFormatterStyle();
4747

4848
$style->setBackground('black');
49-
$this->assertEquals("\033[40mfoo\033[0m", $style->apply('foo'));
49+
$this->assertEquals("\033[40mfoo\033[49m", $style->apply('foo'));
5050

5151
$style->setBackground('yellow');
52-
$this->assertEquals("\033[43mfoo\033[0m", $style->apply('foo'));
52+
$this->assertEquals("\033[43mfoo\033[49m", $style->apply('foo'));
5353

5454
$this->setExpectedException('InvalidArgumentException');
5555
$style->setBackground('undefined-color');
@@ -60,19 +60,19 @@ public function testOptions()
6060
$style = new OutputFormatterStyle();
6161

6262
$style->setOptions(array('reverse', 'conceal'));
63-
$this->assertEquals("\033[7;8mfoo\033[0m", $style->apply('foo'));
63+
$this->assertEquals("\033[7;8mfoo\033[27;28m", $style->apply('foo'));
6464

6565
$style->setOption('bold');
66-
$this->assertEquals("\033[7;8;1mfoo\033[0m", $style->apply('foo'));
66+
$this->assertEquals("\033[7;8;1mfoo\033[27;28;21m", $style->apply('foo'));
6767

6868
$style->unsetOption('reverse');
69-
$this->assertEquals("\033[8;1mfoo\033[0m", $style->apply('foo'));
69+
$this->assertEquals("\033[8;1mfoo\033[28;21m", $style->apply('foo'));
7070

7171
$style->setOption('bold');
72-
$this->assertEquals("\033[8;1mfoo\033[0m", $style->apply('foo'));
72+
$this->assertEquals("\033[8;1mfoo\033[28;21m", $style->apply('foo'));
7373

7474
$style->setOptions(array('bold'));
75-
$this->assertEquals("\033[1mfoo\033[0m", $style->apply('foo'));
75+
$this->assertEquals("\033[1mfoo\033[21m", $style->apply('foo'));
7676

7777
try {
7878
$style->setOption('foo');

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testLGCharEscaping()
3131
$this->assertEquals("\\<info>some info\\</info>", OutputFormatter::escape('<info>some info</info>'));
3232

3333
$this->assertEquals(
34-
"\033[33mSymfony\\Component\\Console does work very well!\033[0m",
34+
"\033[33mSymfony\\Component\\Console does work very well!\033[39m",
3535
$formatter->format('<comment>Symfony\Component\Console does work very well!</comment>')
3636
);
3737
}
@@ -46,19 +46,19 @@ public function testBundledStyles()
4646
$this->assertTrue($formatter->hasStyle('question'));
4747

4848
$this->assertEquals(
49-
"\033[37;41msome error\033[0m",
49+
"\033[37;41msome error\033[39;49m",
5050
$formatter->format('<error>some error</error>')
5151
);
5252
$this->assertEquals(
53-
"\033[32msome info\033[0m",
53+
"\033[32msome info\033[39m",
5454
$formatter->format('<info>some info</info>')
5555
);
5656
$this->assertEquals(
57-
"\033[33msome comment\033[0m",
57+
"\033[33msome comment\033[39m",
5858
$formatter->format('<comment>some comment</comment>')
5959
);
6060
$this->assertEquals(
61-
"\033[30;46msome question\033[0m",
61+
"\033[30;46msome question\033[39;49m",
6262
$formatter->format('<question>some question</question>')
6363
);
6464
}
@@ -68,7 +68,7 @@ public function testNestedStyles()
6868
$formatter = new OutputFormatter(true);
6969

7070
$this->assertEquals(
71-
"\033[37;41msome \033[0m\033[32msome info\033[0m\033[37;41m error\033[0m",
71+
"\033[37;41msome \033[39;49m\033[32msome info\033[39m\033[37;41m error\033[39;49m",
7272
$formatter->format('<error>some <info>some info</info> error</error>')
7373
);
7474
}
@@ -78,7 +78,7 @@ public function testAdjacentStyles()
7878
$formatter = new OutputFormatter(true);
7979

8080
$this->assertEquals(
81-
"\033[37;41msome error\033[0m\033[32msome info\033[0m",
81+
"\033[37;41msome error\033[39;49m\033[32msome info\033[39m",
8282
$formatter->format('<error>some error</error><info>some info</info>')
8383
);
8484
}
@@ -88,7 +88,7 @@ public function testStyleMatchingNotGreedy()
8888
$formatter = new OutputFormatter(true);
8989

9090
$this->assertEquals(
91-
"(\033[32m>=2.0,<2.3\033[0m)",
91+
"(\033[32m>=2.0,<2.3\033[39m)",
9292
$formatter->format('(<info>>=2.0,<2.3</info>)')
9393
);
9494
}
@@ -98,7 +98,7 @@ public function testStyleEscaping()
9898
$formatter = new OutputFormatter(true);
9999

100100
$this->assertEquals(
101-
"(\033[32mz>=2.0,<a2.3\033[0m)",
101+
"(\033[32mz>=2.0,<a2.3\033[39m)",
102102
$formatter->format('(<info>'.$formatter->escape('z>=2.0,<a2.3').'</info>)')
103103
);
104104
}
@@ -108,7 +108,7 @@ public function testDeepNestedStyles()
108108
$formatter = new OutputFormatter(true);
109109

110110
$this->assertEquals(
111-
"\033[37;41merror\033[0m\033[32minfo\033[0m\033[33mcomment\033[0m\033[37;41merror\033[0m",
111+
"\033[37;41merror\033[39;49m\033[32minfo\033[39m\033[33mcomment\033[39m\033[37;41merror\033[39;49m",
112112
$formatter->format('<error>error<info>info<comment>comment</info>error</error>')
113113
);
114114
}
@@ -126,7 +126,7 @@ public function testNewStyle()
126126
$style = new OutputFormatterStyle('blue', 'white');
127127
$formatter->setStyle('b', $style);
128128

129-
$this->assertEquals("\033[34;47msome \033[0m\033[34;47mcustom\033[0m\033[34;47m msg\033[0m", $formatter->format('<test>some <b>custom</b> msg</test>'));
129+
$this->assertEquals("\033[34;47msome \033[39;49m\033[34;47mcustom\033[39;49m\033[34;47m msg\033[39;49m", $formatter->format('<test>some <b>custom</b> msg</test>'));
130130
}
131131

132132
public function testRedefineStyle()
@@ -136,29 +136,29 @@ public function testRedefineStyle()
136136
$style = new OutputFormatterStyle('blue', 'white');
137137
$formatter->setStyle('info', $style);
138138

139-
$this->assertEquals("\033[34;47msome custom msg\033[0m", $formatter->format('<info>some custom msg</info>'));
139+
$this->assertEquals("\033[34;47msome custom msg\033[39;49m", $formatter->format('<info>some custom msg</info>'));
140140
}
141141

142142
public function testInlineStyle()
143143
{
144144
$formatter = new OutputFormatter(true);
145145

146-
$this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</>'));
147-
$this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
146+
$this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</>'));
147+
$this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
148148
}
149149

150150
public function testNonStyleTag()
151151
{
152152
$formatter = new OutputFormatter(true);
153153

154-
$this->assertEquals("\033[32msome \033[0m\033[32m<tag>\033[0m\033[32m styled \033[0m\033[32m<p>\033[0m\033[32msingle-char tag\033[0m\033[32m</p>\033[0m", $formatter->format('<info>some <tag> styled <p>single-char tag</p></info>'));
154+
$this->assertEquals("\033[32msome \033[39m\033[32m<tag>\033[39m\033[32m styled \033[39m\033[32m<p>\033[39m\033[32msingle-char tag\033[39m\033[32m</p>\033[39m", $formatter->format('<info>some <tag> styled <p>single-char tag</p></info>'));
155155
}
156156

157157
public function testFormatLongString()
158158
{
159159
$formatter = new OutputFormatter(true);
160160
$long = str_repeat("\\", 14000);
161-
$this->assertEquals("\033[37;41msome error\033[0m".$long, $formatter->format('<error>some error</error>'.$long));
161+
$this->assertEquals("\033[37;41msome error\033[39;49m".$long, $formatter->format('<error>some error</error>'.$long));
162162
}
163163

164164
public function testNotDecoratedFormatter()
@@ -186,16 +186,16 @@ public function testNotDecoratedFormatter()
186186
$formatter->setDecorated(true);
187187

188188
$this->assertEquals(
189-
"\033[37;41msome error\033[0m", $formatter->format('<error>some error</error>')
189+
"\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
190190
);
191191
$this->assertEquals(
192-
"\033[32msome info\033[0m", $formatter->format('<info>some info</info>')
192+
"\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
193193
);
194194
$this->assertEquals(
195-
"\033[33msome comment\033[0m", $formatter->format('<comment>some comment</comment>')
195+
"\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
196196
);
197197
$this->assertEquals(
198-
"\033[30;46msome question\033[0m", $formatter->format('<question>some question</question>')
198+
"\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
199199
);
200200
}
201201

@@ -205,7 +205,7 @@ public function testContentWithLineBreaks()
205205

206206
$this->assertEquals(<<<EOF
207207
\033[32m
208-
some text\033[0m
208+
some text\033[39m
209209
EOF
210210
, $formatter->format(<<<EOF
211211
<info>
@@ -215,7 +215,7 @@ public function testContentWithLineBreaks()
215215

216216
$this->assertEquals(<<<EOF
217217
\033[32msome text
218-
\033[0m
218+
\033[39m
219219
EOF
220220
, $formatter->format(<<<EOF
221221
<info>some text
@@ -226,7 +226,7 @@ public function testContentWithLineBreaks()
226226
$this->assertEquals(<<<EOF
227227
\033[32m
228228
some text
229-
\033[0m
229+
\033[39m
230230
EOF
231231
, $formatter->format(<<<EOF
232232
<info>
@@ -239,7 +239,7 @@ public function testContentWithLineBreaks()
239239
\033[32m
240240
some text
241241
more text
242-
\033[0m
242+
\033[39m
243243
EOF
244244
, $formatter->format(<<<EOF
245245
<info>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function testWriteDecoratedMessage()
113113
$output->getFormatter()->setStyle('FOO', $fooStyle);
114114
$output->setDecorated(true);
115115
$output->writeln('<foo>foo</foo>');
116-
$this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output');
116+
$this->assertEquals("\033[33;41;5mfoo\033[39;49;25m\n", $output->output, '->writeln() decorates the output');
117117
}
118118

119119
/**

0 commit comments

Comments
 (0)
0