12

I'm writing a console app using Symfony 2 Console Component, and I want to run a console clear command before (or as) the first line of my app.

I have done some research and am struggling to find if this is possible.

1 Answer 1

27

I have been looking for similar functionality. There is nothing built-in to the symfony console that can clear the screen as far as I can tell, but you can still achieve the clear behavior by using the escape code for resetting the terminal like so:

$output->write(sprintf("\033\143")); 

See the post "Clear the Ubuntu bash screen for real" and "What commands can I use to reset and clear my terminal?" for a more extensive explanation of the codes, and this symfony console pull request, where I got the code from, that attempted to emulate the clear functionality.

Sign up to request clarification or add additional context in comments.

2 Comments

Even though PHP has a shell_exec('<any terminal command>') it did not work for me for clearing the terminal with a clear. But sending \033\143 to the output worked, which I think is the escape code for CTRL + C on a mac.
Is sprintf actually required at this point?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.