Skip to content

Commit

Permalink
handles multi-byte characters in autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
jls-esokia committed Feb 23, 2019
1 parent 5ad1f37 commit 136b72f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Expand Up @@ -308,6 +308,12 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu

continue;
} else {
for ($b = 0; $b < 4; ++$b) {
if (false === mb_ord($c)) {
$c .= fread($inputStream, 1);
}
}

$output->write($c);
$ret .= $c;
++$i;
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Expand Up @@ -237,6 +237,43 @@ public function testAskWithAutocompleteWithExactMatch()
$this->assertSame('b', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
}

public function getInputs()
{
return [
['$'], // 1 byte character
['¢'], // 2 bytes character
['€'], // 3 bytes character
['𐍈'], // 4 bytes character
];
}

/**
* @dataProvider getInputs
*/
public function testAskWithAutocompleteWithMultiByteCharacter($character)
{
if (!$this->hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

$inputStream = $this->getInputStream("$character\n");

$possibleChoices = [
'$' => '1 byte character',
'¢' => '2 bytes character',
'€' => '3 bytes character',
'𐍈' => '4 bytes character',
];

$dialog = new QuestionHelper();
$dialog->setHelperSet(new HelperSet([new FormatterHelper()]));

$question = new ChoiceQuestion('Please select a character', $possibleChoices);
$question->setMaxAttempts(1);

$this->assertSame($character, $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
}

public function testAutocompleteWithTrailingBackslash()
{
if (!$this->hasSttyAvailable()) {
Expand Down

0 comments on commit 136b72f

Please sign in to comment.