Skip to content
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

[Console] Correct max width for ChoiceQuestion #15368

Closed
miraage opened this issue Jul 26, 2015 · 2 comments
Closed

[Console] Correct max width for ChoiceQuestion #15368

miraage opened this issue Jul 26, 2015 · 2 comments
Labels

Comments

@miraage
Copy link

miraage commented Jul 26, 2015

https://github.com/symfony/Console/blob/2.7/Helper/QuestionHelper.php#L160

Here is a wrong calculation for cyrillic values.

- $width = max(array_map('strlen', array_keys($question->getChoices())));
+ $width = max(array_map('mb_strlen', array_keys($question->getChoices())));
@miraage miraage changed the title [Console] Correct max width for ChoiceHelper [Console] Correct max width for ChoiceQuestion Jul 26, 2015
@xabbuh xabbuh added the Console label Jul 26, 2015
@RusAlex
Copy link

RusAlex commented Jul 26, 2015

Indeed, I also interested what is the policy in symfony
components. Where unicode allowed multibyte string functions have to
be used?

On Sat, Jul 25, 2015 at 06:12:52PM -0700, Mikhail Osher wrote:

[1]https://github.com/symfony/Console/blob/2.7/Helper/QuestionHelper.ph
p#L160

Here is a wrong calculation for cyrillic values.

  • $width = max(array_map('strlen', array_keys($question->getChoices())));

  • $width = max(array_map('mb_strlen', array_keys($question->getChoices())));


    Reply to this email directly or [2]view it on GitHub.

References

  1. https://github.com/symfony/Console/blob/2.7/Helper/QuestionHelper.php#L160
  2. [Console] Correct max width for ChoiceQuestion #15368

Mr Alexander Pletnev
Development & Support
Viber: +79109209300
www.Benchmark54.com

@marek-pietrzak-tg
Copy link
Contributor

solution is not that straightforward, as sprintf doesn't count properly multibyte strings length. PR is on its way

fabpot added a commit that referenced this issue Jan 14, 2016
This PR was submitted for the 2.8 branch but it was merged into the 2.7 branch instead (closes #17314).

Discussion
----------

Fix max width for multibyte keys in choice question

Fixes wrong key max width for ChoiceQuestion in multibyte strings

Before:
![before](https://cloud.githubusercontent.com/assets/2435655/12203385/977e88c0-b626-11e5-9425-d497f84a9ab3.png)

After:
![after](https://cloud.githubusercontent.com/assets/2435655/12203390/9d9a0b4e-b626-11e5-8d4e-ba9290820778.png)

To replicate you can use this code as an example:
```
namespace AppBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

class QuesionCommand extends Command
{
    protected function configure()
    {
        $this->setName('app:question');
        $this->setDescription('Command for testing PR');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $helper = $this->getHelper('question');

        $question = new ChoiceQuestion('Choose something:',
            [
                'foo' => 'foo',
                'żółw' => 'bar',
                'łabądź' => 'baz',
                'известно' => 'lorem',
                'газета' => 'ipsum',
            ],
            0);

        $colour = $helper->ask($input, $output, $question);
        $output->writeln('you have chosen: ' . $colour);
    }
}
```

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15368
| License       | MIT
| Doc PR        | -

Commits
-------

5d2463b Fix max width for multibyte keys in choice question
@fabpot fabpot closed this as completed Jan 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants