Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Introduce the Input class #51

Merged
merged 7 commits into from
Oct 26, 2015
Merged

Introduce the Input class #51

merged 7 commits into from
Oct 26, 2015

Conversation

Hywan
Copy link
Member

@Hywan Hywan commented Sep 24, 2015

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 Hywan assigned Hywan and jubianchi and unassigned Hywan Sep 24, 2015
@Hywan Hywan mentioned this pull request Oct 21, 2015
@Hywan Hywan changed the title Use /dev/stty instead of php://input if the latter is closed Introduce the Input class Oct 21, 2015
@Hywan
Copy link
Member Author

Hywan commented Oct 21, 2015

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

@Hywan
Copy link
Member Author

Hywan commented Oct 21, 2015

Ready for a review!

self::$_old = Processus::execute('stty -g');
Processus::execute('stty -echo -icanon min 1 time 0');
self::$_old = Processus::execute('stty -f /dev/tty -g');
Processus::execute('stty -f /dev/tty -echo -icanon min 1 time 0');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still don't know what to do here. -F on Linux, -f on BSD. Any idea how to test the OS efficiently?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am installing an Ubuntu OS to try finding a solution.

*
* @param int $length Length.
* @return string
* @throws \Hoa\Exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be present.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the underlying method we wrap can throw an exception, so…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this method don't throw any things, @throws tag is present in Hoa\File\Read and don't need to be duplicated.

@Hywan
Copy link
Member Author

Hywan commented Oct 22, 2015

I found a way to solve the issue -f vs. -F (respectively on BSD and Linux). See the commit message of Hywan@402b218:

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
Copy link
Member Author

Hywan commented Oct 22, 2015

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

@Metalaka
Copy link
Member

getStream issue not solved, see example on hoaproject/Stream#16

@Hywan
Copy link
Member Author

Hywan commented Oct 26, 2015

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

@Metalaka
Copy link
Member

Ok, let's go.

By default, the advanced interaction is not enabled if `STDIN` is not
direct. We can now bypass this guard in particular cases (for instance
if `STDIN` is closed and we would like to read from `/dev/tty`).
In case `STDIN` has been closed, `stty` will not be able to act as
expected. So we force it to use the `/dev/tty` file instead of the
regular standard input.
Exactly like the `Output` class, the `Input` interface represents a
stream of the program, here the input stream. We then create the
`Console::setInput` and `Console::getInput` methods.

By default, if no `Input` instance is provided, an `Hoa\File\Read`
instance will be used representing `php://stdin`. If it has been closed
then `/dev/tty` will be used instead.

This way, we are ensured methods will behave as expected, whether or not
the standard input has been closed or not.
The `getStream` method allows to access to the underlying stream that
the `Input` class wraps.
@Hywan
Copy link
Member Author

Hywan commented Oct 26, 2015

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

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.
As @Metalaka suggests, we can remove the `@throws` clause.
@Hywan
Copy link
Member Author

Hywan commented Oct 26, 2015

Fixed with this commit.

@Bhoat Bhoat merged commit 9600ab3 into hoaproject:master Oct 26, 2015
@Hywan Hywan removed the in progress label Oct 26, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

4 participants