0

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?

Community
  • 1
  • 1
Gajus
  • 69,002
  • 70
  • 275
  • 438

1 Answers1

0

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.

Gajus
  • 69,002
  • 70
  • 275
  • 438
Samuel
  • 3,631
  • 5
  • 37
  • 71
  • This prints the output, as it should, but does not trigger the command in CLI. https://gist.github.com/gajus/427c1314e58c326e90d7 – Gajus Jun 16 '15 at 12:47
  • Your suggestion using `exec` executes an external program, as opposed to triggering a change in the current CLI session. – Gajus Jun 16 '15 at 14:39
  • Ok, check the modified answer – Samuel Jun 16 '15 at 15:16
  • @Samule This works. Please, can you explain why does it work? Does a terminal such as iTerm interpret literally all of the program output and suppose it contains is an escape code, it will execute it? – Gajus Jun 16 '15 at 16:23
  • 1
    I have edited your answer to include explanation as I understand it. I trust that you will correct it if I am wrong about anything. – Gajus Jun 16 '15 at 16:33
  • For my own future references (and possibly useful to others too), a simple PHP function to detect the terminal type and action the corresponding equivalent of clear scrollback. https://gist.github.com/gajus/2f2d7cb9c674a8b8e86c – Gajus Jun 17 '15 at 14:54