nategood / commando

An Elegant CLI Library for PHP
MIT License
798 stars 80 forks source link

Windows – Color not working #28

Open agung-wete opened 10 years ago

agung-wete commented 10 years ago

Hi

Using windows environment try to use --help, got this errors, just right before filename appeared=

The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.

Yes, 3 in a row.

Thanks

nategood commented 10 years ago

Does this happen with the examples in /examples?

agung-wete commented 10 years ago

yes

stratease commented 10 years ago

Does this lib support windows env? I imagine that would be quite difficult to create parallel feature support

nategood commented 10 years ago

I don't have a windows machine or VM handy to debug. Might have something to do with the Colors.php library. Have you tried running examples directly from the Colors.php library?

taishar commented 10 years ago

Hi, This is happening to me as well. It looks like its coming from Terminal::tput().

morganprecision commented 10 years ago

Specifically I think the issue is that /dev/null cannot be found in the Windows environment (the Terminal::tput() method redirects to /dev/null).

I created a C:\dev\null folder on the Windows box I am working with to see what would happen. I then started getting "Access is denied" messages instead of "The system cannot find the path specified."

cpalm1974 commented 7 years ago

This happens when executing "tput" to get the width of the terminal in Terminal.php line 51 ff.

    private static function tput($default, $param = 'cols')
    {
        $test = exec('tput ' . $param . ' 2>/dev/null');
        if (empty($test))
            return $default;
        $result = intval(exec('tput ' . $param));
        return empty($result) ? $default : $result;
    }

It would be more optimal if at first the os is detected with the PHP_OS constant and check for the first 3 characters = "WIN". Even for Linux environments it could be optimal to execute "which tput" at first to see if it's there and accessible. For Win Environments I think there is not an optimal way to get the width. I think it's best to execute "MODE CON:" as this is available in every Windows installation (I think even since MS-DOS 3.3 :) ) The returned string gives console parameters like the width, lines and so on. But the output is language dependent. So in my German installation something like:

Status von Gerät CON:
---------------------
    Zeilen:          300
    Spalten:         99
    Wiederholrate:   31
    Verzögerungszeit:1
    Codepage:        850

So you'd have to maybe regex to get the second line after "-----".

cpalm1974 commented 7 years ago

I think the tput function could be optimized like the following. It's working good in my environment.

    private static function tput($default, $param = 'cols')
    {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
            return $default;
        $path = trim(shell_exec('which tput'));
        if (empty($path))
            return $default;
        $test = exec('tput ' . $param . ' 2>/dev/null');
        if (empty($test))
            return $default;
        $result = intval(exec('tput ' . $param));
        return empty($result) ? $default : $result;
    }

Maybe you can import the changes...