[Console] Use readline for user input when available

This allows to use arrow keys in the terminal instead of having weird characters
This commit is contained in:
Michaël Perrin 2015-07-28 11:41:56 +02:00 committed by Michaël Perrin
parent 96e211d2da
commit 8b63d6209d
2 changed files with 31 additions and 5 deletions

View File

@ -1,6 +1,12 @@
CHANGELOG
=========
2.8.0
-----
* use readline for user input in the question helper when available to allow
the use of arrow keys
2.6.0
-----

View File

@ -127,11 +127,7 @@ class QuestionHelper extends Helper
}
if (false === $ret) {
$ret = fgets($inputStream, 4096);
if (false === $ret) {
throw new \RuntimeException('Aborted');
}
$ret = trim($ret);
$ret = $this->readFromInput($inputStream);
}
} else {
$ret = trim($this->autocomplete($output, $question, $inputStream));
@ -423,6 +419,30 @@ class QuestionHelper extends Helper
return self::$shell;
}
/**
* Reads user input.
*
* @param resource $stream The input stream
*
* @return string User input
*
* @throws \RuntimeException
*/
private function readFromInput($stream)
{
if (STDIN === $stream && function_exists('readline')) {
$ret = readline();
} else {
$ret = fgets($stream, 4096);
if (false === $ret) {
throw new \RuntimeException('Aborted');
}
}
return trim($ret);
}
/**
* Returns whether Stty is available or not.
*