Skip to content

Commit

Permalink
Add new tests and fix problem with the second option being chosen on …
Browse files Browse the repository at this point in the history
…down arrow
  • Loading branch information
lmcd committed Jan 6, 2013
1 parent 8a0bcfb commit 2b73975
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/Symfony/Component/Console/Helper/DialogHelper.php
Expand Up @@ -91,7 +91,7 @@ public function ask(OutputInterface $output, $question, $default = null, array $
$i = 0;
$matches = array();
$numMatches = 0;
$ofs = 0;
$ofs = -1;

// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
Expand All @@ -109,6 +109,10 @@ public function ask(OutputInterface $output, $question, $default = null, array $
$output->write("\033[1D");
}

if ($i === 0) {
$ofs = -1;
}

// Erase characters from cursor to end of line
$output->write("\033[K");
$ret = substr($ret, 0, $i);
Expand All @@ -126,6 +130,10 @@ public function ask(OutputInterface $output, $question, $default = null, array $
if (0 === $i) {
$matches = $autocomplete;
$numMatches = count($matches);

if ('A' === $c[2] && -1 === $ofs) {
$ofs = 0;
}
}

if (0 === $numMatches) {
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php
Expand Up @@ -55,18 +55,24 @@ public function testAsk()
rewind($output->getStream());
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));

$bundles = array('AcmeDemoBundle', 'AsseticBundle');
$bundles = array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle');

// Acm<NEWLINE>
// Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
// <NEWLINE>
$inputStream = $this->getInputStream("Acm\nAc\177\177s\tTest\n\n");
// <UP ARROW><UP ARROW><NEWLINE>
// <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB><NEWLINE>
// <DOWN ARROW><NEWLINE>
$inputStream = $this->getInputStream("Acm\nAc\177\177s\tTest\n\n\033[A\033[A\n\033[A\033[A\033[A\033[A\033[A\tTest\n\033[B\n");
$dialog->setInputStream($inputStream);

if ($this->hasSttyAvailable()) {
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
$this->assertEquals('AsseticBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
$this->assertEquals('FrameworkBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
$this->assertEquals('SecurityBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
$this->assertEquals('FooBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
} else {
$this->markTestSkipped();
}
Expand Down

0 comments on commit 2b73975

Please sign in to comment.