10000 bug #20970 [Console] Fix question formatting using SymfonyStyle::ask(… · symfony/symfony@0bb7eaf · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bb7eaf

Browse files
committed
bug #20970 [Console] Fix question formatting using SymfonyStyle::ask() (chalasr, ogizanagi)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix question formatting using SymfonyStyle::ask() | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20964 | License | MIT | Doc PR | n/a Given ```php $io = new SymfonyStyle($input, $output); $io->ask('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Bar'); ``` Before output ![before](http://image.prntscr.com/image/af3806f866654deda2dec79b50d1ffa2.png) After output ![after](http://image.prntscr.com/image/59c031d9e02949cebeae7a4734c7043f.png) Commits ------- e189183 [Console] SymfonyStyle: Escape trailing backslashes in user texts 9d46712 [Console] Fix question formatting using SymfonyStyle::ask()
2 parents e13e3f0 + e189183 commit 0bb7eaf

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ public static function escape($text)
3333
{
3434
$text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
3535

36+
return self::escapeTrailingBackslash($text);
37+
}
38+
39+
/**
40+
* Escapes trailing "\" in given text.
41+
*
42+
* @param string $text Text to escape
43+
*
44+
* @return string Escaped text
45+
*
46+
* @internal
47+
*/
48+
public static function escapeTrailingBackslash($text)
49+
{
3650
if ('\\' === substr($text, -1)) {
3751
$len = strlen($text);
3852
$text = rtrim($text, '\\');

src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
5353
*/
5454
protected function writePrompt(OutputInterface $output, Question $question)
5555
{
56-
$text = OutputFormatter::escape($question->getQuestion());
56+
$text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
5757
$default = $question->getDefault();
5858

5959
switch (true) {

src/Symfony/Component/Console/Style/SymfonyStyle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function title($message)
121121
{
122122
$this->autoPrependBlock();
123123
$this->writeln(array(
124-
sprintf('<comment>%s</>', $message),
124+
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
125125
sprintf('<comment>%s</>', str_repeat('=', strlen($message))),
126126
));
127127
$this->newLine();
@@ -134,7 +134,7 @@ public function section($message)
134134
{
135135
$this->autoPrependBlock();
136136
$this->writeln(array(
137-
sprintf('<comment>%s</>', $message),
137+
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
138138
sprintf('<comment>%s</>', str_repeat('-', strlen($message))),
139139
));
140140
$this->newLine();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Input\InputInterface;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
6+
7+
//Ensure symfony style helper methods handle trailing backslashes properly when decorating user texts
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output = new SymfonyStyleWithForcedLineLength($input, $output);
10+
11+
$output->title('Title ending with \\');
12+
$output->section('Section ending with \\');
13+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
Title ending with \
3+
===================
4+
5+
Section ending with \
6+
---------------------
7+

src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,22 @@ public function testAskEscapeDefaultValue()
9292
$this->assertOutputContains('Can I have a backslash? [\]', $output);
9393
}
9494

95-
public function testAskEscapeLabel()
95+
public function testAskEscapeAndFormatLabel()
96+
{
97+
$helper = new SymfonyQuestionHelper();
98+
$helper->setInputStream($this->getInputStream('Foo\\Bar'));
99+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));
100+
101+
$this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
102+
}
103+
104+
public function testLabelTrailingBackslash()
96105
{
97106
$helper = new SymfonyQuestionHelper();
98107
$helper->setInputStream($this->getInputStream('sure'));
99-
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \?'));
108+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
100109

101-
$this->assertOutputContains('Do you want a \?', $output);
110+
$this->assertOutputContains('Question with a trailing \\', $output);
102111
}
103112

104113
/**

0 commit comments

Comments
 (0)
0