diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php
index 1f93c06451630..93e221d36a563 100644
--- a/src/Symfony/Component/Console/Helper/QuestionHelper.php
+++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php
@@ -198,15 +198,9 @@ protected function writePrompt(OutputInterface $output, Question $question)
$message = $question->getQuestion();
if ($question instanceof ChoiceQuestion) {
- $maxWidth = max(array_map([$this, 'strlen'], array_keys($question->getChoices())));
-
- $messages = (array) $question->getQuestion();
- foreach ($question->getChoices() as $key => $value) {
- $width = $maxWidth - $this->strlen($key);
- $messages[] = ' ['.$key.str_repeat(' ', $width).'] '.$value;
- }
-
- $output->writeln($messages);
+ $output->writeln(array_merge([
+ $question->getQuestion(),
+ ], $this->formatChoiceQuestionChoices($question, 'info')));
$message = $question->getPrompt();
}
@@ -214,6 +208,26 @@ protected function writePrompt(OutputInterface $output, Question $question)
$output->write($message);
}
+ /**
+ * @param string $tag
+ *
+ * @return string[]
+ */
+ protected function formatChoiceQuestionChoices(ChoiceQuestion $question, $tag)
+ {
+ $messages = [];
+
+ $maxWidth = max(array_map('self::strlen', array_keys($choices = $question->getChoices())));
+
+ foreach ($choices as $key => $value) {
+ $padding = str_repeat(' ', $maxWidth - self::strlen($key));
+
+ $messages[] = sprintf(" [<$tag>%s$padding$tag>] %s", $key, $value);
+ }
+
+ return $messages;
+ }
+
/**
* Outputs an error message.
*/
diff --git a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
index 5937741a2c69e..7cd050fee133c 100644
--- a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
+++ b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
@@ -96,15 +96,15 @@ protected function writePrompt(OutputInterface $output, Question $question)
$output->writeln($text);
+ $prompt = ' > ';
+
if ($question instanceof ChoiceQuestion) {
- $width = max(array_map('strlen', array_keys($question->getChoices())));
+ $output->writeln($this->formatChoiceQuestionChoices($question, 'comment'));
- foreach ($question->getChoices() as $key => $value) {
- $output->writeln(sprintf(" [%-${width}s] %s", $key, $value));
- }
+ $prompt = $question->getPrompt();
}
- $output->write(' > ');
+ $output->write($prompt);
}
/**
diff --git a/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php
index cbf3b957b3913..b7a26f95087f9 100644
--- a/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php
+++ b/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php
@@ -130,6 +130,49 @@ public function testAskThrowsExceptionOnMissingInput()
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
}
+ public function testChoiceQuestionPadding()
+ {
+ $choiceQuestion = new ChoiceQuestion('qqq', [
+ 'foo' => 'foo',
+ 'żółw' => 'bar',
+ 'łabądź' => 'baz',
+ ]);
+
+ (new SymfonyQuestionHelper())->ask(
+ $this->createStreamableInputInterfaceMock($this->getInputStream("foo\n")),
+ $output = $this->createOutputInterface(),
+ $choiceQuestion
+ );
+
+ $this->assertOutputContains(<<
+EOT
+ , $output);
+ }
+
+ public function testChoiceQuestionCustomPrompt()
+ {
+ $choiceQuestion = new ChoiceQuestion('qqq', ['foo']);
+ $choiceQuestion->setPrompt(' >ccc> ');
+
+ (new SymfonyQuestionHelper())->ask(
+ $this->createStreamableInputInterfaceMock($this->getInputStream("foo\n")),
+ $output = $this->createOutputInterface(),
+ $choiceQuestion
+ );
+
+ $this->assertOutputContains(<<ccc>
+EOT
+ , $output);
+ }
+
protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);