Skip to content

[Console] Use readline for user input when available #DX #15382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2015

Conversation

michaelperrin
Copy link
Contributor

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Given I am entering data in an user input
When I use left and right keys to make some changes in what I have typed
Then the cursor should move accordingly instead of adding characters at the end of the line

To make it simple: using the arrow keys (← →) to make changes to what I already typed would be much handier than getting ^[[D and ^[[C characters in the terminal and having to delete all chars to type everything again.

I could not add any extra tests to this as the STDIN can't be used during tests. But they are not breaking and I tried again all types of questions (text, choices, hidden) by myself.

Note that readline can't be used for hidden questions, as stty -echo is not taken into account.

*/
private function hasReadline()
{
return function_exists('readline');
Copy link
Member

Choose a reason for hiding this comment

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

I would inline that instead of creating a method for that.

@fabpot
Copy link
Member

fabpot commented Aug 1, 2015

👍

@michaelperrin michaelperrin force-pushed the feature/readline-input branch 2 times, most recently from ea1e0b4 to aeead4d Compare August 1, 2015 08:35
@michaelperrin
Copy link
Contributor Author

@fabpot I inlined the check on the availability of readline.

throw new \RuntimeException('Aborted');
}

$ret = trim($ret);
Copy link
Member

Choose a reason for hiding this comment

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

What about using stream_get_line() for platforms not supporting readline()? We could then get rid of the trim() call.

Copy link
Member

Choose a reason for hiding this comment

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

By the way, the behaviour is now inconsistent as the string returned by readline() is not trimmed (for example, when containing extra spaces before or after the actual input).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your review.
I didn't know about stream_get_line() method. Would there be an advantage of using it on platforms not supporting readline()? The string would need to be trimmed as well if we want to keep the same behavior (removing whitespaces around the string) and arrow keys can't be used as well, so it doesn't seem there is any benefit.

I agree with you on the second point I should trim the string both with readline and with the usual fgets functions.

@xabbuh
Copy link
Member

xabbuh commented Aug 1, 2015

Status: Needs work

Michaël Perrin added 2 commits August 1, 2015 11:46
This allows to use arrow keys in the terminal instead of having weird characters
@michaelperrin michaelperrin force-pushed the feature/readline-input branch from aeead4d to 0534899 Compare August 1, 2015 09:47
@michaelperrin
Copy link
Contributor Author

@xabbuh I made the change according to you second comment. My comments about your first one are above.

@xabbuh
Copy link
Member

xabbuh commented Aug 1, 2015

@michaelperrin I could imagine that people find stream_get_line() more readable. But I don't have any hard feelings about it. 👍

Status: Reviewed

@fabpot
Copy link
Member

fabpot commented Aug 1, 2015

Thank you @michaelperrin.

@fabpot fabpot merged commit 0534899 into symfony:2.8 Aug 1, 2015
fabpot added a commit that referenced this pull request Aug 1, 2015
…DX (michaelperrin)

This PR was merged into the 2.8 branch.

Discussion
----------

[Console] Use readline for user input when available #DX

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Given I am entering data in an user input
When I use left and right keys to make some changes in what I have typed
Then the cursor should move accordingly instead of adding characters at the end of the line

To make it simple: using the arrow keys (←  →)  to make changes to what I already typed would be much handier than getting `^[[D` and `^[[C` characters in the terminal and having to delete all chars to type everything again.

I could not add any extra tests to this as the STDIN can't be used during tests. But they are not breaking and I tried again all types of questions (text, choices, hidden) by myself.

Note that `readline` can't be used for hidden questions, as `stty -echo` is not taken into account.

Commits
-------

0534899 [Console] Fix Symfony coding standards violations
8b63d62 [Console] Use readline for user input when available
@michaelperrin
Copy link
Contributor Author

Thank you!

@michaelperrin michaelperrin deleted the feature/readline-input branch August 1, 2015 13:09
@stof
Copy link
Member

stof commented Aug 4, 2015

this is missing the handling of false return value when readline is used

@michaelperrin
Copy link
Contributor Author

@stof readline() never returns false (does it?), that's why I didn't handle this value.

@stof
Copy link
Member

stof commented Aug 4, 2015

The comments on the documentation page of the function are saying it does: http://php.net/manual/en/function.readline.php#86719

@michaelperrin
Copy link
Contributor Author

@stof You're right! Too bad it was hidden in the PHP doc comments. I opened #15450

nicolas-grekas added a commit that referenced this pull request Aug 5, 2015
…elperrin)

This PR was merged into the 2.8 branch.

Discussion
----------

[Console] Handle false return value from readline

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

Following #15382 and as @stof mentioned, the `false` value can be returned by `readline()` and should be handled. This fixes the problem.

Commits
-------

f5ca270 [Console] Handle false return value from readline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants