-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Fix bug with utf-8 bug, change writePrompt to use one function prepareChoices #33911
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
Changes from all commits
0b093ff
ce8526d
fe8ae78
21c5a8b
ee637c8
b5e1596
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,14 +198,8 @@ 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; | ||
} | ||
|
||
$messages = array_merge($m 8000 essages, $this->prepareChoices('info', $question)); | ||
$output->writeln($messages); | ||
|
||
$message = $question->getPrompt(); | ||
|
@@ -214,6 +208,28 @@ protected function writePrompt(OutputInterface $output, Question $question) | |
$output->write($message); | ||
} | ||
|
||
/** | ||
* @param string $tag | ||
* | ||
* @return string[] | ||
*/ | ||
protected function prepareChoices($tag, ChoiceQuestion $question) | ||
{ | ||
$choices = $question->getChoices(); | ||
|
||
$maxWidth = max(array_map([$this, 'strlen'], array_keys($choices))); | ||
|
||
$messages = []; | ||
|
||
foreach ($choices as $key => $value) { | ||
$width = $maxWidth - $this->strlen($key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// We using strlen + str_repeat to fix sprintf whitespace padding problem with UTF-8) | ||
$messages[] = sprintf(' [<%s>%s%s</%s>] %s', $tag, $key, str_repeat(' ', $width), $tag, $value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolas-grekas There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, you want to implement sprintf in pure PHP. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean add a sprintf helper method to handle the left justify. If the string is not utf-8 we use the normal sprintf. If it is, we use the str_repeat strategy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, sure! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @proggga Would you have time to take care of this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, thanks for reviewing this, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
May be you can give instructions, and after may be I can do it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In your proposed changes, you replaces the sprintf left justify feat by strlen + str_repeat everytime. Firstly, this alternative should only be used for multibytes strings. Secondly, it should be done in a new helper method in the abstract Helper.php class (like we did for strlen and sbustr). |
||
} | ||
|
||
return $messages; | ||
} | ||
|
||
/** | ||
* Outputs an error message. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,14 +97,14 @@ protected function writePrompt(OutputInterface $output, Question $question) | |
$output->writeln($text); | ||
|
||
if ($question instanceof ChoiceQuestion) { | ||
$width = max(array_map('strlen', array_keys($question->getChoices()))); | ||
$messages = $this->prepareChoices('comment', $question); | ||
|
||
foreach ($question->getChoices() as $key => $value) { | ||
$output->writeln(sprintf(" [<comment>%-${width}s</comment>] %s", $key, $value)); | ||
} | ||
$output->writeln($messages); | ||
// ChoiceQuestion can have any prompt | ||
$output->write($question->getPrompt() ?: Question::DEFAULT_PROMPT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's another bug fixed by this PR btw. Unrelated to the linked issue. |
||
} else { | ||
$output->write(Question::DEFAULT_PROMPT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here keep the old behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should actually move the prompt from the ChoiceQuestion to the Question class? 🤔 |
||
} | ||
|
||
$output->write(' > '); | ||
} | ||
|
||
/** | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self::strlen
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[static::class, 'strlen']
(but honestly I've no issue with using $this)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently use
self::
everywhere, we don't expect those methods to be overidden.