hoaproject / Console

The Hoa\Console library.
https://hoa-project.net/
366 stars 32 forks source link

Introduce the `Input` class #51

Closed Hywan closed 8 years ago

Hywan commented 9 years ago

Old title

Imagine this case:

$ php foo.php

where foo.php is a program that reads from STDIN. It works perfectly with Hoa\Console.

Now, imagine this other case:

$ echo "foo\nbar" | php foo.php

STDIN will contain foo and bar but the Hoa\Console\Cursor —for instance— will no longer work since it reads from STDIN too.

The idea is then to close STDIN (aka php://input) and open /dev/tty, which is the real standard input in this case.

So, to address these needs, we introduce the Hoa\Console::getStdin method that is a helper to choose the appopriate standard input. We also change the Hoa\Console::advancedInteraction behavior: 👷 stty uses the -f option to specify /dev/tty instead of the default standard input. However, on Linux, this is -F. I don't know how to address this right now (I mean: Making something cross-platform).

Finally, all Hoa\Console's classes that were using STDIN directly are now using Hoa\Console::getStdin.

New title

Actually, since #55, this PR has taken a new path. We also introduced an Input class. And it considers whether to use Hoa\File\Read('php://stdin') as the default input, or Hoa\File\Read('/dev/tty') in case the former has been closed. This is a basic wrapper.

Hywan commented 8 years ago

@jubianchi, @Metalaka: Please, review it. I will add tests today.

Hywan commented 8 years ago

Ready for a review!

Hywan commented 8 years ago

I found a way to solve the issue -f vs. -F (respectively on BSD and Linux). See the commit message of https://github.com/Hywan/Console/commit/402b21835aa907e1df333e2b79c3f7c9fb5831b2:

On Linux, this is the stty -F $file or stty --file $file options. On BSD, this is the stty -f $file option.

There is a conflict here and this is difficult to have something cross-platform. Fortunately, both architectures use the standard input as the -F, --file or -f value. So writing stty < $file seems to fix the issue.

Hywan commented 8 years ago

We are ready for a merge. @Metalaka, @jubianchi: Is it OK for you?

Metalaka commented 8 years ago

getStream issue not solved, see example on https://github.com/hoaproject/Stream/issues/16

Hywan commented 8 years ago

I guess we can merge this patch before, though? This is safe enough for now.

Metalaka commented 8 years ago

Ok, let's go.

Hywan commented 8 years ago

We still have an issue with stty -f, -F and <

Hywan commented 8 years ago

Fixed with this commit.