Skip to content

Commit 136b72f

Browse files
committed
handles multi-byte characters in autocomplete
1 parent 5ad1f37 commit 136b72f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
308308

309309
continue;
310310
} else {
311+
for ($b = 0; $b < 4; ++$b) {
312+
if (false === mb_ord($c)) {
313+
$c .= fread($inputStream, 1);
314+
}
315+
}
316+
311317
$output->write($c);
312318
$ret .= $c;
313319
++$i;

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,43 @@ public function testAskWithAutocompleteWithExactMatch()
237237
$this->assertSame('b', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
238238
}
239239

240+
public function getInputs()
241+
{
242+
return [
243+
['$'], // 1 byte character
244+
['¢'], // 2 bytes character
245+
[''], // 3 bytes character
246+
['𐍈'], // 4 bytes character
247+
];
248+
}
249+
250+
/**
251+
* @dataProvider getInputs
252+
*/
253+
public function testAskWithAutocompleteWithMultiByteCharacter($character)
254+
{
255+
if (!$this->hasSttyAvailable()) {
256+
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
257+
}
258+
259+
$inputStream = $this->getInputStream("$character\n");
260+
261+
$possibleChoices = [
262+
'$' => '1 byte character',
263+
'¢' => '2 bytes character',
264+
'' => '3 bytes character',
265+
'𐍈' => '4 bytes character',
266+
];
267+
268+
$dialog = new QuestionHelper();
269+
$dialog->setHelperSet(new HelperSet([new FormatterHelper()]));
270+
271+
$question = new ChoiceQuestion('Please select a character', $possibleChoices);
272+
$question->setMaxAttempts(1);
273+
274+
$this->assertSame($character, $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
275+
}
276+
240277
public function testAutocompleteWithTrailingBackslash()
241278
{
242279
if (!$this->hasSttyAvailable()) {

0 commit comments

Comments
 (0)