How to action a command from a PHP script in the current CLI context?

170 Views Asked by At

Suppose I have a PHP script that prints time.

echo time() . "\r\n";

Executing this script several times in CLI will look like this:

curiosity:bin gajus$ php -r 'echo time() . "\r\n";'
1434453316
curiosity:bin gajus$ php -r 'echo time() . "\r\n";'
1434453318
curiosity:bin gajus$ php -r 'echo time() . "\r\n";'
1434453319
curiosity:bin gajus$

I want my PHP script to print printf '\e\]50;ClearScrollback\a' sequence that would clear iTerm2 scrollback. I have tried variations of php -r 'echo "\e\]50;ClearScrollback\a" .time() . "\r\n";' but that just spits the string to the CLI output:

curiosity:bin gajus$ php -r 'echo "\e\]50;ClearScrollback\a" .time() . "\r\n";'
]50;ClearScrollback\a1434453543
curiosity:bin gajus$

Is there a way to trigger CLI ClearScrollback from within PHP script?

1

There are 1 best solutions below

6
On BEST ANSWER

The escape code ^[]50;ClearScrollback^G itself is described in Proprietary Escape Codes iTerm2 documentation.

A quick comment on notation: in this document, ^[ means "Escape" (hex code 0x1b) and ^G means "bel" (hex code 0x07).

You can use PHP chr() function, e.g.

php -r 'echo chr(27) . "]50;ClearScrollback" . chr(7);'

The chr function returns a specific ascii character. In this example I am using ASCII control characters "escape" (27) and "bell" (7). It is a cross-platform safe method to describe ascii control characters.