Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Fix question formatting using SymfonyStyle::ask() #20970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Symfony/Component/Console/Formatter/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public static function escape($text)
{
$text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);

return self::escapeTrailingBackslash($text);
}

/**
* Escapes trailing "\" in given text.
*
* @param string $text Text to escape
*
* @return string Escaped text
*
* @internal
*/
public static function escapeTrailingBackslash($text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the method be marked as internal?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. I guess it can be used by anyone creating it's own helpers in userland.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it should be marked as internal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

{
if ('\\' === substr($text, -1)) {
$len = strlen($text);
$text = rtrim($text, '\\');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
*/
protected function writePrompt(OutputInterface $output, Question $question)
{
$text = OutputFormatter::escape($question->getQuestion());
$text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
$default = $question->getDefault();

switch (true) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Style/SymfonyStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function title($message)
{
$this->autoPrependBlock();
$this->writeln(array(
sprintf('<comment>%s</>', $message),
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
sprintf('<comment>%s</>', str_repeat('=', strlen($message))),
));
$this->newLine();
Expand All @@ -134,7 +134,7 @@ public function section($message)
{
$this->autoPrependBlock();
$this->writeln(array(
sprintf('<comment>%s</>', $message),
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
sprintf('<comment>%s</>', str_repeat('-', strlen($message))),
));
$this->newLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;

//Ensure symfony style helper methods handle trailing backslashes properly when decorating user texts
return function (InputInterface $input, OutputInterface $output) {
$output = new SymfonyStyleWithForcedLineLength($input, $output);

$output->title('Title ending with \\');
$output->section('Section ending with \\');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Title ending with \
===================

Section ending with \
---------------------

Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,22 @@ public function testAskEscapeDefaultValue()
$this->assertOutputContains('Can I have a backslash? [\]', $output);
}

public function testAskEscapeLabel()
public function testAskEscapeAndFormatLabel()
{
$helper = new SymfonyQuestionHelper();
$helper->setInputStream($this->getInputStream('Foo\\Bar'));
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));

$this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
}

public function testLabelTrailingBackslash()
{
$helper = new SymfonyQuestionHelper();
$helper->setInputStream($this->getInputStream('sure'));
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \?'));
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));

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

/**
Expand Down