From 8b63d6209d7426b79c79fb3a9e88436db634ae92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Perrin?= Date: Tue, 28 Jul 2015 11:41:56 +0200 Subject: [PATCH 1/2] [Console] Use readline for user input when available This allows to use arrow keys in the terminal instead of having weird characters --- src/Symfony/Component/Console/CHANGELOG.md | 6 ++++ .../Console/Helper/QuestionHelper.php | 30 +++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 07254c6720..4e29d2ed45 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 9e4fa02f2c..17605fe64c 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -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. * From 05348991c263e714aac848b119e99d2f8ff77507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Perrin?= Date: Tue, 28 Jul 2015 14:45:38 +0200 Subject: [PATCH 2/2] [Console] Fix Symfony coding standards violations --- src/Symfony/Component/Console/Helper/QuestionHelper.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 17605fe64c..dc68fe6e71 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -77,7 +77,7 @@ class QuestionHelper extends Helper } /** - * Returns the helper's input stream + * Returns the helper's input stream. * * @return resource */ @@ -146,7 +146,7 @@ class QuestionHelper extends Helper * Outputs the question prompt. * * @param OutputInterface $output - * @param Question $question + * @param Question $question */ protected function writePrompt(OutputInterface $output, Question $question) { @@ -218,7 +218,7 @@ class QuestionHelper extends Helper // Backspace Character if ("\177" === $c) { if (0 === $numMatches && 0 !== $i) { - $i--; + --$i; // Move cursor backwards $output->write("\033[1D"); } @@ -271,7 +271,7 @@ class QuestionHelper extends Helper } else { $output->write($c); $ret .= $c; - $i++; + ++$i; $numMatches = 0; $ofs = 0;