Search code examples
phpterminalconsolecharreadline

Strange char with readline on terminal app


I am writing php-console app with readline use for user input commands.

This is how I am using it:

$input = readline("\033$label\033\n");

And it looks like this in cmd terminal:

enter image description here

But with this same terminal in vscode this questions marks are not visible. When I remove color codes ("\033", "\033") they dissapears.

I tested this app in cygwin terminal too and questions marks are not visible, but in this places are two space symbols.

Please for help.


Solution

  • You need to output correct control codes in accordance with the terminal emulator being used. What works in an xterm emulator may not in vt100 or whatever Windows uses.

    1. You can use the function stream_isatty(STDOUT) to detect if the output is a terminal.
    2. You can refer to the constants PHP_OS_FAMILY or PHP_OS to detect the OS you're currently running on.
    3. On linux [or emulated equivalent, like cygwin] you can refer to the environment variable $_ENV['TERM'] to determine which terminal emulator is in use.

    Based on these three pieces of information you can determine whether or not to use "fancy" output formatting, and output the control codes appropriate to a given terminal emulator.