Skip to content

Commit c856a01

Browse files
committed
feature #15382 [Console] Use readline for user input when available #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
2 parents 48aa3e1 + 0534899 commit c856a01

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.8.0
5+
-----
6+
7+
* use readline for user input in the question helper when available to allow
8+
the use of arrow keys
9+
410
2.6.0
511
-----
612

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function setInputStream($stream)
7777
}
7878

7979
/**
80-
* Returns the helper's input stream
80+
* Returns the helper's input stream.
8181
*
8282
* @return resource
8383
*/
@@ -127,11 +127,7 @@ public function doAsk(OutputInterface $output, Question $question)
127127
}
128128

129129
if (false === $ret) {
130-
$ret = fgets($inputStream, 4096);
131-
if (false === $ret) {
132-
throw new \RuntimeException('Aborted');
133-
}
134-
$ret = trim($ret);
130+
$ret = $this->readFromInput($inputStream);
135131
}
136132
} else {
137133
$ret = trim($this->autocomplete($output, $question, $inputStream));
@@ -150,7 +146,7 @@ public function doAsk(OutputInterface $output, Question $question)
150146
* Outputs the question prompt.
151147
*
152148
* @param OutputInterface $output
153-
* @param Question $question
149+
* @param Question $question
154150
*/
155151
protected function writePrompt(OutputInterface $output, Question $question)
156152
{
@@ -222,7 +218,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
222218
// Backspace Character
223219
if ("\177" === $c) {
224220
if (0 === $numMatches && 0 !== $i) {
225-
$i--;
221+
--$i;
226222
// Move cursor backwards
227223
$output->write("\033[1D");
228224
}
@@ -275,7 +271,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
275271
} else {
276272
$output->write($c);
277273
$ret .= $c;
278-
$i++;
274+
++$i;
279275

280276
$numMatches = 0;
281277
$ofs = 0;
@@ -423,6 +419,30 @@ private function getShell()
423419
return self::$shell;
424420
}
425421

422+
/**
423+
* Reads user input.
424+
*
425+
* @param resource $stream The input stream
426+
*
427+
* @return string User input
428+
*
429+
* @throws \RuntimeException
430+
*/
431+
private function readFromInput($stream)
432+
{
433+
if (STDIN === $stream && function_exists('readline')) {
434+
$ret = readline();
435+
} else {
436+
$ret = fgets($stream, 4096);
437+
438+
if (false === $ret) {
439+
throw new \RuntimeException('Aborted');
440+
}
441+
}
442+
443+
return trim($ret);
444+
}
445+
426446
/**
427447
* Returns whether Stty is available or not.
428448
*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy