简体   繁体   中英

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

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";' 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?

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, eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM