I'ld like to hide ugly data from being shown by command line tools like cat
(and maybe simple text editors too) which often get confused by binary data. For example a VT100 terminal sometimes gets misconfigured by binary outputs.
<?php
// PHP code shown by text tools on the command line
__halt_compiler();
// here some fake EOF mark for simple text processing tools
// hidden ugly data
Can end of file be spoofed to simple stream-based text viewer tools, especially to the linux command line tools (but maybe also to some windows tools)?
I am looking for a solution from within the mixed text/binary file, so that other people using cat
or similar do not get trash on their screen.
Best Answer
One 'solution' would be to use the alternative screen buffer which many (but not all) terminals support. Consider the following command:
On a terminal supporting alternative screen buffers, you would see
possibly with a very sudden flash of the terminal.
The
\e[?1049h
sequence will cause the terminal to switch to the alternative screen buffer, where everything printed afterwards will end up. The\e[?1049l
sequence switches back to the main screen buffer.An example with php:
where
^[
represends the escape character.The alternative screen buffer is used by many programs which like to create terminal user interfaces, but want to restore the contents of the terminal after closing. This is how less can display contents using the entire window, but upon exiting, all of the previous commands are still visible. If you have unbuffer installed, you can verify this:
as you can see, the first thing printed is
\x1b[?1049h
which causes the terminal to switch screen buffers.This would not work on any editor (that I'm aware of) since most editors do not attempt to display non-printable characters.