diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 4cdecfd41a49..1edc2d68cbbc 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console\Helper; +use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -303,7 +304,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu // Save cursor position $output->write("\0337"); // Write highlighted text - $output->write(''.substr($matches[$ofs], $i).''); + $output->write(''.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $i)).''); // Restore cursor position $output->write("\0338"); } diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index 4fb462cc1048..69a2256efad2 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -156,6 +156,46 @@ public function testAskWithAutocompleteWithNonSequentialKeys() $this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); } + public function testAutocompleteWithTrailingBackslash() + { + if (!$this->hasSttyAvailable()) { + $this->markTestSkipped('`stty` is required to test autocomplete functionality'); + } + + $inputStream = $this->getInputStream('E'); + + $dialog = new QuestionHelper(); + $dialog->setInputStream($inputStream); + $helperSet = new HelperSet(array(new FormatterHelper())); + $dialog->setHelperSet($helperSet); + + $question = new Question(''); + $expectedCompletion = 'ExampleNamespace\\'; + $question->setAutocompleterValues(array($expectedCompletion)); + + $output = $this->createOutputInterface(); + $dialog->ask($this->createInputInterfaceMock(), $output, $question); + + $outputStream = $output->getStream(); + rewind($outputStream); + $actualOutput = stream_get_contents($outputStream); + + // Shell control (esc) sequences are not so important: we only care that + // tag is interpreted correctly and replaced + $irrelevantEscSequences = array( + "\0337" => '', // Save cursor position + "\0338" => '', // Restore cursor position + "\033[K" => '', // Clear line from cursor till the end + ); + + $importantActualOutput = strtr($actualOutput, $irrelevantEscSequences); + + // Remove colors (e.g. "\033[30m", "\033[31;41m") + $importantActualOutput = preg_replace('/\033\[\d+(;\d+)?m/', '', $importantActualOutput); + + $this->assertEquals($expectedCompletion, $importantActualOutput); + } + public function testAskHiddenResponse() { if ('\\' === DIRECTORY_SEPARATOR) {