Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Shell color output


I'm trying to make my first php extension with zephir then I've made a class which output string in shell with some colors.
This class works fine in php but in zephir that output the code string and not set the color ... dont know why and how to do the right way.
For example if you do that in linux shell :

echo "\033[0;32mHello"

That output only Hello in green.
When i do the same in zephir that output the entire string without the color interpreted. Someone can help ?


You're getting the nice colors in PHP and the shell because both interpret \033 as a character expressed in octal. That character happens to be the console control character that tells the terminal that what follows is a command for it to interpret, rather than for the user to see. The terminal then parses the characters that follow until it recognizes the end of the sequence (in this case, m, which tells it to treat the preceding characters as a color code), performs the requested action, and then proceeds to treat any further text normally again.

The problem, here, is that Zephir doesn't recognize or interpret those kinds of escapes. There are a few options for working around this. One is to generate the string within PHP, where the escapes will be translated into the appropriate control characters before they are stored anywhere, and pass that through to your Zephir output. Another is to pass the string through a shell command, and let it interpret the characters for you.

The best option, though, is probably to set up your colorizer function(s) to use a char value set to 27 (the decimal version of octal 33) as a prefix in place of the \033. So something like:

protected function echoGreen(string message)
    char control = 27;
    echo control . "[0;32m" . message . control . "[0m";


Thx for your answer, I've tested your solution but I've got :

char cannot be used in concat operation

So I used chr(27) instead and that work ! Thx!