1

I'm writing a console app using Symfony Console Component. I'm trying to interact with questions and tables. I want to ask a question and then output a table and then ask another and output another table. All of that happens in the same command class. This is my code:

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

    $question        = ( new ConfirmationQuestion( 'This is a confirmation question:' ) );
    $question_result = $question_helper->ask( $this->input, $this->output, $question );

    $table = new Table( $this->output );
    $table
    ->setHeaders( array( 'Name', 'Age' ) )
    ->setRows(
        array(
            array( 'Mike', 21 ),
            array( 'Sara', 22 ),
        )
    );
    $table->setStyle( 'box' );
    $table->render();
}

When I'm trying to implement my code, the question works well but when I output the table it shows in a weird way like this:

This is a confirmation question: bla bla
┌──────┬─────└‚ Name │ Age │
├──────┼─────┤
│ Mike │ 21  │
│ Sara │ 22  │
└──────┴─────┘

If I echo the table before the question it works:

┌──────┬─────┐
│ Name │ Age │
├──────┼─────┤
│ Mike │ 21  │
│ Sara │ 22  │
└──────┴─────┘
This is a confirmation question: bla bla

Any one has any clue how I can solve this?

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
  • I tried this out locally (macOS, PHP 8.0, Symfony 5.2) and it works fine. Can you show the whole command to see if it is anything in there? Also, check your IDE what encoding your file is in (in PHPStorm it is displayed on the status bar on the bottom right). It probably should be UTF-8. – dbrumann Apr 03 '21 at 09:11
  • @dbrumann This is part of a fresh Command class, I didn't add or change anything. The IDE encoding is UTF-8. I figured out that the problem is with "$table->setStyle( 'box' );", if I remove this line everything goes normal. If I add this line and change the argument to 'default', everything goes normal too. So, I'm trying to figure out why this line breaks the style. Maybe it works on macOS, my environment is Win10. – Bisharah Estephan Apr 03 '21 at 09:36
  • I don't have a Windows PC I can try it on. Feel free to create a minimal demo, i.e. run `symfony new ...` & copy the command class in it, stripping out everything in the command that is not needed to reproduce the bug. Then create an issue in github.com/symfony/symfony, so people can check out your example and verify it on their system. – dbrumann Apr 03 '21 at 09:39
  • @dbrumann well noted, thanks! – Bisharah Estephan Apr 03 '21 at 09:46

1 Answers1

0

Call @sapi_windows_cp_set(65001); before (or after) you call the ask method. (@ is just to suppress any error.):

if (\function_exists('sapi_windows_cp_set')) {
    @sapi_windows_cp_set(65001);
}

The problem is that Symfony forces the console to use the codepage 1252 anytime it gets a response of a question:

if (\function_exists('sapi_windows_cp_set')) {
    // Codepage used by cmd.exe on Windows to allow special characters (éàüñ).
    @sapi_windows_cp_set(1252);
}

hence setting the codepage to 1252, (only on windows) and that codepage does not match the current codepage of the OS.

Why 65001?

did sapi_windows_cp_get() to get the current codepage of the OS. And also according to Windows here, the codepage 65001 is meant for UTF-8.

See also question here.

Beware, even though the codepage 65001 is supposed to render UTF-8 characters, it does not work properly if you expect your users to enter special characters (éàüñ, etc.) in their response to the ask method. You will need to use the codepage 1252 for that

The origin of the Symfony code above is this commit. (See also the conversation on that commit)

Prince Dorcis
  • 955
  • 7
  • 7