10000 [Console] Use readline for user input when available · symfony/symfony@8b63d62 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b63d62

Browse files
Michaël Perrinmichaelperrin
authored andcommitted
[Console] Use readline for user input when available
This allows to use arrow keys in the terminal instead of having weird characters
1 parent 96e211d commit 8b63d62

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
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: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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));
@@ -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)
0