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

Skip to content

[Console][SymfonyQuestionHelper] Handle multibytes question choices keys and custom prompt #35000

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 1 commit into from
Dec 17, 2019
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
32 changes: 23 additions & 9 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,36 @@ 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[] = ' [<info>'.$key.str_repeat(' ', $width).'</info>] '.$value;
}

$output->writeln($messages);
$output->writeln(array_merge([
$question->getQuestion(),
], $this->formatChoiceQuestionChoices($question, 'info')));

$message = $question->getPrompt();
}

$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.
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(" [<comment>%-${width}s</comment>] %s", $key, $value));
}
$prompt = $question->getPrompt();
}

$output->write(' > ');
$output->write($prompt);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
qqq:
[foo ] foo
[żółw ] bar
[łabądź] baz
>
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(<<<EOT
qqq:
[0] foo
>ccc>
EOT
, $output);
}

protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
Expand Down