8000 [Console] Use readline for user input when available #DX by michaelperrin · Pull Request #15382 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----

Expand Down
38 changes: 29 additions & 9 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function setInputStream($stream)
}

/**
* Returns the helper's input stream
* Returns the helper's input stream.
*
* @return resource
*/
Expand Down Expand Up @@ -127,11 +127,7 @@ public function doAsk(OutputInterface $output, Question $question)
}

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));
Expand All @@ -150,7 +146,7 @@ public function doAsk(OutputInterface $output, Question $question)
* Outputs the question prompt.
*
* @param OutputInterface $output
* @param Question $question
* @param Question $question
*/
protected function writePrompt(OutputInterface $output, Question $question)
{
Expand Down Expand Up @@ -222,7 +218,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
// Backspace Character
if ("\177" === $c) {
if (0 === $numMatches && 0 !== $i) {
$i--;
--$i;
// Move cursor backwards
$output->write("\033[1D");
}
Expand Down Expand Up @@ -275,7 +271,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
} else {
$output->write($c);
$ret .= $c;
$i++;
++$i;

$numMatches = 0;
$ofs = 0;
Expand Down Expand Up @@ -423,6 +419,30 @@ private function getShell()
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.
*
Expand Down
0