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

Appearance settings

Symfony Console Exception reporting doesn't parse style tags. #22552

Copy link
Copy link
Closed
@rquadling

Description

@rquadling
Issue body actions
Q A
Bug report? yes
Feature request? yes
Symfony version 2.8.19 though probably earlier

When an exception is generated by Symfony Console, the output is in a nice big red box.

throw new RuntimeException(
    sprintf(
        'Unable to locate the src directory within <fg=yellow;bg=red>%s</fg=yellow;bg=red>.'.PHP_EOL.
        'Try running with <fg=yellow;bg=red>--ignore-checkers</fg=yellow;bg=red>.',
        $input->getArgument('phpmaker-installation')
    )
);

image

But, unfortunately, if you put in style tags, they are rendered as literals.

If you override \Symfony\Component\Console\Output\OutputInterface::write() and unescape the tags, this will provide the output colouring, but not the correct lengths.

class MyOutput extends \Symfony\Component\Console\Output\Output
{
    /**
     * @inheritdoc
     *
     * Also removes the escaping of <fg> and <bg> tags.
     */
    public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
    {
        parent::write(
            array_map(
                function ($message) {
                    return preg_replace('`\\\(?=</?[fb]g=)`', '', $message);
                },
                (array) $messages
            ),
            $newline,
            $options
        );
    }
}

image

The width of the block is determined by \Symfony\Component\Console\Application::stringWidth().

If this method was to remove the style tags before determining the length, then the length would/should be correct.

I'm aware that not all uses of '<xxxx>' will be style tags. Some may be literal and therefore escaping of the '<' (which is performed by \Symfony\Component\Console\Application::renderException() when it calls \Symfony\Component\Console\Formatter\OutputFormatter::escape()).

One idea on how to solve this would be to have an internal OutputFormatter that essentially removes known style tags. This formatter could wrap the string being passed to \Symfony\Component\Console\Application::stringWidth(), thus providing the correct string length.

The example code above doesn't take into account the other formats of the style tags (named styles).

Also, the \Symfony\Component\Console\Formatter\OutputFormatter::escape() method would need to ignore recognised style tags.

Happy to work on this, but any pointers, things I've missed, etc would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.