Skip to content

Commit d8a7d32

Browse files
fancywebproggga
andcommitted
[Console][(SymfonyQuestionHelper] Handle multibytes question choices keys and custom prompt
Co-authored-by: Mikhail Fesenko <m.fesenko@corp.vk.com>
1 parent a073606 commit d8a7d32

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,36 @@ protected function writePrompt(OutputInterface $output, Question $question)
198198
$message = $question->getQuestion();
199199

200200
if ($question instanceof ChoiceQuestion) {
201-
$maxWidth = max(array_map([$this, 'strlen'], array_keys($question->getChoices())));
202-
203-
$messages = (array) $question->getQuestion();
204-
foreach ($question->getChoices() as $key => $value) {
205-
$width = $maxWidth - $this->strlen($key);
206-
$messages[] = ' [<info>'.$key.str_repeat(' ', $width).'</info>] '.$value;
207-
}
208-
209-
$output->writeln($messages);
201+
$output->writeln(array_merge([
202+
$question->getQuestion(),
203+
], $this->formatChoiceQuestionChoices($question, 'info')));
210204

211205
$message = $question->getPrompt();
212206
}
213207

214208
$output->write($message);
215209
}
216210

211+
/**
212+
* @param string $tag
213+
*
214+
* @return string[]
215+
*/
216+
protected function formatChoiceQuestionChoices(ChoiceQuestion $question, $tag)
217+
{
218+
$messages = [];
219+
220+
$maxWidth = max(array_map('self::strlen', array_keys($choices = $question->getChoices())));
221+
222+
foreach ($choices as $key => $value) {
223+
$padding = str_repeat(' ', $maxWidth - self::strlen($key));
224+
225+
$messages[] = sprintf(" [<$tag>%s$padding</$tag>] %s", $key, $value);
226+
}
227+
228+
return $messages;
229+
}
230+
217231
/**
218232
* Outputs an error message.
219233
*/

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ protected function writePrompt(OutputInterface $output, Question $question)
9696

9797
$output->writeln($text);
9898

99+
$prompt = ' > ';
100+
99101
if ($question instanceof ChoiceQuestion) {
100-
$width = max(array_map('strlen', array_keys($question->getChoices())));
102+
$output->writeln($this->formatChoiceQuestionChoices($question, 'comment'));
101103

102-
foreach ($question->getChoices() as $key => $value) {
103-
$output->writeln(sprintf(" [<comment>%-${width}s</comment>] %s", $key, $value));
104-
}
104+
$prompt = $question->getPrompt();
105105
}
106106

107-
$output->write(' > ');
107+
$output->write($prompt);
108108
}
109109

110110
/**

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,49 @@ public function testAskThrowsExceptionOnMissingInput()
130130
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
131131
}
132132

133+
public function testChoiceQuestionPadding()
134+
{
135+
$choiceQuestion = new ChoiceQuestion('qqq', [
136+
'foo' => 'foo',
137+
'żółw' => 'bar',
138+
'łabądź' => 'baz',
139+
]);
140+
141+
(new SymfonyQuestionHelper())->ask(
142+
$this->createStreamableInputInterfaceMock($this->getInputStream("foo\n")),
143+
$output = $this->createOutputInterface(),
144+
$choiceQuestion
145+
);
146+
147+
$this->assertOutputContains(<<<EOT
148+
qqq:
149+
[foo ] foo
150+
[żółw ] bar
151+
[łabądź] baz
152+
>
153+
EOT
154+
, $output);
155+
}
156+
157+
public function testChoiceQuestionCustomPrompt()
158+
{
159+
$choiceQuestion = new ChoiceQuestion('qqq', ['foo']);
160+
$choiceQuestion->setPrompt(' >ccc> ');
161+
162+
(new SymfonyQuestionHelper())->ask(
163+
$this->createStreamableInputInterfaceMock($this->getInputStream("foo\n")),
164+
$output = $this->createOutputInterface(),
165+
$choiceQuestion
166+
);
167+
168+
$this->assertOutputContains(<<<EOT
169+
qqq:
170+
[0] foo
171+
>ccc>
172+
EOT
173+
, $output);
174+
}
175+
133176
protected function getInputStream($input)
134177
{
135178
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy