symfonyvt100symfony-process

Symfony Process Eats Terminal Control Codes


So I'm running a Symfony Process for some commands, and I noticed that when I write those to Symfony's OutputInterface it is not showing colors or progress bars. I think that the commands (npm, artisan, ls, etc) are using terminal control codes, and one of those classes is eating the non-standard ASCII characters.

EDIT: I've done some digging and I belive Symfony uses its StreamOutput class by default. It appears to be able to output in color, and I've tried telling it to OUTPUT_RAW. No beans there. Perhaps the problem is somewhere else...

Is there a built-in way to tell these classes not to do that? How can I get my pretty output back?


Solution

  • Colors availability depends on the program you are calling. You may try to set the tty/pty:

    protected function execute(InputInterface $input, OutputInterface $output) 
    {
        $process = new Process('ls -l --color="always"');
        $process->setTty(true); // or $process->setPty(true);
        $process->run();
        $output->write($process->getOutput());
    }
    

    See related issue.

    I don't think that a command output removes the escape codes. Next example works well (for me):

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->write(shell_exec('ls -l --color="always"')); // ok, output is colored
    }
    

    Hope this helps.