Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5d2463b

Browse files
marek-pietrzak-tgfabpot
authored andcommitted
Fix max width for multibyte keys in choice question
1 parent3c6d1a9 commit5d2463b

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,12 @@ protected function writePrompt(OutputInterface $output, Question $question)
162162
$message =$question->getQuestion();
163163

164164
if ($questioninstanceof ChoiceQuestion) {
165-
$width =max(array_map('strlen',array_keys($question->getChoices())));
165+
$maxWidth =max(array_map(array($this,'strlen'),array_keys($question->getChoices())));
166166

167167
$messages = (array)$question->getQuestion();
168168
foreach ($question->getChoices()as$key =>$value) {
169-
$messages[] =sprintf(" [<info>%-${width}s</info>] %s",$key,$value);
169+
$width =$maxWidth -$this->strlen($key);
170+
$messages[] =' [<info>'.$key.str_repeat('',$width).'</info>]'.$value;
170171
}
171172

172173
$output->writeln($messages);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\Console\Tests\Helper;
1313

14+
useSymfony\Component\Console\Formatter\OutputFormatter;
1415
useSymfony\Component\Console\Helper\QuestionHelper;
1516
useSymfony\Component\Console\Helper\HelperSet;
1617
useSymfony\Component\Console\Helper\FormatterHelper;
@@ -350,6 +351,34 @@ public function testNoInteraction()
350351
$this->assertEquals('not yet',$dialog->ask($this->createInputInterfaceMock(false),$this->createOutputInterface(),$question));
351352
}
352353

354+
publicfunctiontestChoiceOutputFormattingQuestionForUtf8Keys()
355+
{
356+
$question ='Lorem ipsum?';
357+
$possibleChoices =array(
358+
'foo' =>'foo',
359+
'żółw' =>'bar',
360+
'łabądź' =>'baz',
361+
);
362+
$outputShown =array(
363+
$question,
364+
' [<info>foo </info>] foo',
365+
' [<info>żółw </info>] bar',
366+
' [<info>łabądź</info>] baz',
367+
);
368+
$output =$this->getMock('\Symfony\Component\Console\Output\OutputInterface');
369+
$output->method('getFormatter')->willReturn(newOutputFormatter());
370+
371+
$dialog =newQuestionHelper();
372+
$dialog->setInputStream($this->getInputStream("\n"));
373+
$helperSet =newHelperSet(array(newFormatterHelper()));
374+
$dialog->setHelperSet($helperSet);
375+
376+
$output->expects($this->once())->method('writeln')->with($this->equalTo($outputShown));
377+
378+
$question =newChoiceQuestion($question,$possibleChoices,'foo');
379+
$dialog->ask($this->createInputInterfaceMock(),$output,$question);
380+
}
381+
353382
protectedfunctiongetInputStream($input)
354383
{
355384
$stream =fopen('php://memory','r+',false);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp