1 (edited by rbro 2019-03-14 15:29:06)

Topic: readline and printing color text

I am running CentOS 7 with PHP 7.2.  Not sure if this is configuration, but when I compile php manually with --with-readline, I can run:

<?php
readline("\033[0;31maaa\033[0m");
?>

and it prints the text "aaa" in red.

When I do it with the PHP 7.2 RPM's, it prints:

[0;31maaa[0m

Comparing phpinfo(), my manual compile shows:

Readline Support => enabled
Readline library => 6.2

and the RPM shows:

Readline Support => enabled
Readline library => EditLine wrapper

Is there a way to use the RPM's and have the text print in color?

Thanks again for your help.

Ryan

Re: readline and printing color text

Indeed, this doesn't seems to be supported by libedit.


Notice: libreadline is licensed under GPL version 3 which is NOT compatible with PHP license, so nobody is allowed to build and distribute packages using this library.
libedit is (sadly) the only supported and compatible alternative

Sorry


P.S. FYI, there is a PR to totally drop libreadline support from PHP https://github.com/php/php-src/pull/3823

Laptop:  Fedora 38 + rpmfusion + remi (SCL only)
x86_64 builder: Fedora 39 + rpmfusion + remi-test
aarch64 builder: RHEL 9 with EPEL
Hosting Server: CentOS 8 Stream with EPEL, rpmfusion, remi

Re: readline and printing color text

Thanks for your help.   I realized there is a easy workaround by doing:

<?php
echo "\033[0;31maaa\033[0m";
$a = readline();
?>

The only other side effect I've seen with libedit so far is when I run "php -a", if I press the Home or End keys on my keyword, I get a tilde character ~.  I am running PuTTY on Windows and my TERM is currently set as xterm.  If I run "export TERM=linux" first, then it seems to fix it.

Re: readline and printing color text

Hi Remi,

I'm running into another side effect of libedit.  I know this is not directly related to your RPM packaging, which is working great for me, but if you had any thoughts on this, I'd appreciate it, especially since libreadline will be removed in the future.

Using libreadline, I was able to read a password on a cli script using the below, and the password would not be echo'ed on the screen.  With libedit, the password displays on the screen.

Do you know of a way to read a password with libedit without having it display on the screen?

<?php
`/bin/stty -echo`;
$line = readline('Enter password: ');
`/bin/stty echo`;
var_dump($line);
?>

Thanks for your help.

Ryan

Re: readline and printing color text

For now, I'm doing this, which I suppose is effectively using libreadline:

<?php
echo 'Enter password: ';
$line = trim(shell_exec('read -s value; echo $value'));
var_dump($line);
?>

Re: readline and printing color text

No need to reinvent the wheel...

=> https://packagist.org/packages/seld/cli-prompt

echo 'Say hello: ';

$answer = Seld\CliPrompt\CliPrompt::hiddenPrompt();

echo 'You answered: '.$answer . PHP_EOL;
Laptop:  Fedora 38 + rpmfusion + remi (SCL only)
x86_64 builder: Fedora 39 + rpmfusion + remi-test
aarch64 builder: RHEL 9 with EPEL
Hosting Server: CentOS 8 Stream with EPEL, rpmfusion, remi

Re: readline and printing color text

Thanks, it looks like cli-prompt is also using libreadline via shell_exec:

$readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read -r mypassword';