merged branch lmcd/autocomplete (PR #6391)

This PR was squashed before being merged into the master branch (closes #6391).

Commits
-------

7bad0ef [Console] Add autocomplete as you type

Discussion
----------

[Console] Add autocomplete as you type

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
License of the code: MIT

Finally got around to reviving the console autocomplete code.
Is now up to date with Symfony master. Also changed backspace behaviour to remove one character instead of two.

stty stuff is a mystery to a lot of people, so I've commented verbosely.

See also: https://github.com/symfony/symfony/pull/2364

---------------------------------------------------------------------------

by lmcd at 2012-12-17T10:11:16Z

@stof - updated with a better solution

---------------------------------------------------------------------------

by Seldaek at 2012-12-17T10:25:15Z

Seems pretty cool, but could you replace all `/usr/bin/env stty` calls by simply `stty`? That way it would also work on windows - if you have mingw or cygwin installed and stty is in the path at least. I don't see the benefit of doing the /usr/bin/env trick here. That's good for shebang lines because you need an absolute path, but in an exec/shell_exec call, you can rely on the PATH and just type the command name.

---------------------------------------------------------------------------

by lmcd at 2012-12-17T18:33:06Z

@Seldaek makes sense. Changed.

---------------------------------------------------------------------------

by lmcd at 2012-12-17T21:32:17Z

Tested on Mac OS X 10.8 and Ubuntu 12.04. Would be great to hear from people on Windows, cygwin and those with exotic terminal setups.

I'll update my fork of SensioGeneratorBundle a little later with support for this.

@fabpot - is there still time for this to land in 2.2?

---------------------------------------------------------------------------

by fabpot at 2012-12-17T21:34:23Z

If we have good feedback from Windows users, yes it can land in 2.2. ping @pborreli

---------------------------------------------------------------------------

by michelsalib at 2012-12-17T23:39:50Z

A am about to try on windows 7 with cmd, powershell and cygwin. Any other way to test without writing a new command using the helper ?

---------------------------------------------------------------------------

by michelsalib at 2012-12-18T00:01:42Z

I tried on Windows 7 with cmd, powershell and cygwin and got this error: `Le chemin d'accès spécifié est introuvable.`. You can translate it to `The specified path could not be found`.

---------------------------------------------------------------------------

by lmcd at 2012-12-18T00:01:43Z

I've updated SensioGeneratorBundle to support autocompletion on the `generate:doctrine:entity` command. It autocompletes bundle names, configuration formats and field types. See here: c627c67ce7

@michelsalib ping

---------------------------------------------------------------------------

by lmcd at 2012-12-18T00:03:43Z

@michelsalib - hmm. I imagine it's either a problem locating stty or some configuration issue on your end. Do you have file/line number?

---------------------------------------------------------------------------

by michelsalib at 2012-12-18T00:04:41Z

Verbose mode did not help. Let me try with some dirty line by line check to see if I can give you a line.

---------------------------------------------------------------------------

by michelsalib at 2012-12-18T00:09:54Z

My bad, I should have guessed that line 144 `exec('/usr/bin/env stty', $output, $exitcode);` cannot work on regular Windows environment. This should at least fails silently for users using cmd or powershell. Apparently cygwin users can activate stty. Let me investigate.

---------------------------------------------------------------------------

by michelsalib at 2012-12-18T00:16:30Z

Ok, cygwin comes pre-bundled with stty. I applied the fix recommended by @Seldaek and it fixed the cygwin command.
The only remaining problem is that your redirect the output of the exec call to the console, in this case cmd and powershell output the error telling that stty is not defined in the system: `'stty' n'est pas reconnu en tant que commande interne ou externe, un programme exécutable ou un fichier de commandes.`.

---------------------------------------------------------------------------

by lmcd at 2012-12-18T00:17:41Z

Ah, I see you're running the unit tests. The `hasSttyAvailable` method was lifted from `DialogHelper` where it is also used in `askHiddenResponse`. Question: is `defined('PHP_WINDOWS_VERSION_BUILD')` true for cygwin?

---------------------------------------------------------------------------

by michelsalib at 2012-12-18T00:22:14Z

I am not running test, I am actually running a homemade command:
```
$dialog = $this->getHelper('dialog');

$ask = $dialog->ask($output, 'Autocomplete example', null, array(
    'French', 'English', 'Chineese',
));

$output->writeln($ask);
```

`hasSttyAvailable` is called in the ask function at line 80. The incriminated function is here : 9ebcd4bac9/src/Symfony/Component/Console/Helper/DialogHelper.php (L411).

Also `defined('PHP_WINDOWS_VERSION_BUILD')` is true in the three of my consoles.

---------------------------------------------------------------------------

by lmcd at 2012-12-18T00:27:16Z

@michelsalib see 7be142481c

---------------------------------------------------------------------------

by michelsalib at 2012-12-18T00:28:20Z

Why keeping `/usr/bin/env` in your calls ? Cygwin cannot interpret it as long a stty is not in this folder.

---------------------------------------------------------------------------

by lmcd at 2012-12-18T00:29:30Z

@michelsalib `/usr/bin/env` was put there by someone else for the `askHiddenResponse` method. I can remove it, so long as it doesn't break something else.

---------------------------------------------------------------------------

by michelsalib at 2012-12-18T00:34:11Z

IMO users who want's to use stty should have it configured in the PATH, as @Seldaek said. Prefixing the folder where the stty "should" be is a nonsense to me. Moreover it might work well on *nix systems, but will never be compatible with cygwin.
I tested it locally (without the `/usr/bin/env` prefix) and now I have a nice autocomplete on Cygwin, and nothing on cmd or powershell. Which is just what I expected.

@lmcd very nice work :)

---------------------------------------------------------------------------

by lmcd at 2012-12-18T05:17:32Z

For anyone interested, you can scroll through available autocomplete options that match typed characters by using up and down arrow keys. This has been implemented in a seperate branch here: https://github.com/lmcd/symfony/tree/autocomplete-arrows

---------------------------------------------------------------------------

by drak at 2012-12-18T19:13:34Z

@lmcd - The console PRs never cease to amaze me. Really well done!

---------------------------------------------------------------------------

by fabpot at 2012-12-19T13:58:33Z

@lmcd Is it mergeable now?

---------------------------------------------------------------------------

by lmcd at 2012-12-19T17:59:09Z

@fabpot Yes.

---------------------------------------------------------------------------

by lmcd at 2012-12-19T20:03:31Z

Edit: commits squashed

---------------------------------------------------------------------------

by lmcd at 2012-12-19T21:29:07Z

@stloyd I have addressed the two things mentioned. I'm now using $i in place of strlen($ret) as it held the same value.

---------------------------------------------------------------------------

by fabpot at 2012-12-20T07:09:27Z

@lmcd: Thanks a lot for finishing this in time for 2.2. Before I can merge, there are two remaining tasks:

 * add a note about the new feature in the CHANGELOG file of the Console component;
 * create a PR on `symfony/symfony-docs` to explain the new feature.

Can you take care of that?

---------------------------------------------------------------------------

by lmcd at 2012-12-20T07:11:15Z

@fabpot sure

---------------------------------------------------------------------------

by stloyd at 2012-12-29T09:15:23Z

@lmcd You should squash your "merge" commit.

---------------------------------------------------------------------------

by lmcd at 2012-12-29T13:49:37Z

Well that screwed up. File Changed: 216 :S
Edit: hard reset to an earlier hash and reapplied the CHANGELOG commit. Should be good now ping @stloyd

---------------------------------------------------------------------------

by stloyd at 2012-12-29T14:33:58Z

@lmcd `ask()` method is quite long now, but in overall looks ok =)

---------------------------------------------------------------------------

by lmcd at 2013-01-02T20:49:49Z

Anything preventing this being merged? Ping @fabpot
This commit is contained in:
Fabien Potencier 2013-01-03 23:03:04 +01:00
commit 7863f8890f
3 changed files with 151 additions and 17 deletions

View File

@ -7,6 +7,7 @@ CHANGELOG
* added support for colorization on Windows via ConEmu
* add a method to Dialog Helper to ask for a question and hide the response
* added support for interactive selections in console (DialogHelper::select())
* added support for autocompletion as you type in Dialog Helper
2.1.0
-----

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Helper;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
/**
* The Dialog class provides helpers to interact with the user.
@ -63,23 +64,131 @@ class DialogHelper extends Helper
/**
* Asks a question to the user.
*
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param string $default The default answer if none is given by the user
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param string $default The default answer if none is given by the user
* @param array $autocomplete List of values to autocomplete
*
* @return string The user answer
*
* @throws \RuntimeException If there is no data to read in the input stream
*/
public function ask(OutputInterface $output, $question, $default = null)
public function ask(OutputInterface $output, $question, $default = null, array $autocomplete = null)
{
$output->write($question);
$ret = fgets($this->inputStream ?: STDIN, 4096);
if (false === $ret) {
throw new \RuntimeException('Aborted');
$inputStream = $this->inputStream ?: STDIN;
if (null === $autocomplete || !$this->hasSttyAvailable()) {
$ret = fgets($inputStream, 4096);
if (false === $ret) {
throw new \RuntimeException('Aborted');
}
$ret = trim($ret);
} else {
$i = 0;
$currentMatched = false;
$ret = '';
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
// Add highlighted text style
$output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white'));
// Read a keypress
while ($c = fread($inputStream, 1)) {
// Did we read an escape sequence?
if ("\033" === $c) {
$c .= fread($inputStream, 2);
// Escape sequences for arrow keys
if ('A' === $c[2] || 'B' === $c[2] || 'C' === $c[2] || 'D' === $c[2]) {
// todo
}
continue;
}
// Backspace Character
if ("\177" === $c) {
if ($i === 0) {
continue;
}
if (false === $currentMatched) {
$i--;
// Move cursor backwards
$output->write("\033[1D");
}
// Erase characters from cursor to end of line
$output->write("\033[K");
$ret = substr($ret, 0, $i);
$currentMatched = false;
continue;
}
if ("\t" === $c || "\n" === $c) {
if (false !== $currentMatched) {
// Echo out completed match
$output->write(substr($autocomplete[$currentMatched], strlen($ret)));
$ret = $autocomplete[$currentMatched];
$i = strlen($ret);
}
if ("\n" === $c) {
$output->write($c);
break;
}
$currentMatched = false;
continue;
}
if (ord($c) < 32) {
continue;
}
$output->write($c);
$ret .= $c;
$i++;
// Erase characters from cursor to end of line
$output->write("\033[K");
foreach ($autocomplete as $j => $value) {
// Get a substring of the current autocomplete item based on number of chars typed (e.g. AcmeDemoBundle = Acme)
$matchTest = substr($value, 0, $i);
if ($ret === $matchTest) {
if ($i === strlen($value)) {
$currentMatched = false;
break;
}
// Save cursor position
$output->write("\0337");
$output->write('<hl>' . substr($value, $i) . '</hl>');
// Restore cursor position
$output->write("\0338");
$currentMatched = $j;
break;
}
$currentMatched = false;
}
}
// Reset stty so it behaves normally again
shell_exec('stty icanon echo');
}
$ret = trim($ret);
return strlen($ret) > 0 ? $ret : $default;
}
@ -186,22 +295,23 @@ class DialogHelper extends Helper
* validated data when the data is valid and throw an exception
* otherwise.
*
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param callable $validator A PHP callback
* @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
* @param string $default The default answer if none is given by the user
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param callable $validator A PHP callback
* @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
* @param string $default The default answer if none is given by the user
* @param array $autocomplete List of values to autocomplete
*
* @return mixed
*
* @throws \Exception When any of the validators return an error
*/
public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null, array $autocomplete = null)
{
$that = $this;
$interviewer = function() use ($output, $question, $default, $that) {
return $that->ask($output, $question, $default);
$interviewer = function() use ($output, $question, $default, $autocomplete, $that) {
return $that->ask($output, $question, $default, $autocomplete);
};
return $this->validateAttempts($interviewer, $output, $validator, $attempts);
@ -300,7 +410,7 @@ class DialogHelper extends Helper
return self::$stty;
}
exec('/usr/bin/env stty', $output, $exitcode);
exec('stty 2>&1', $output, $exitcode);
return self::$stty = $exitcode === 0;
}

View File

@ -54,6 +54,22 @@ class DialogHelperTest extends \PHPUnit_Framework_TestCase
rewind($output->getStream());
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
$bundles = array('AcmeDemoBundle', 'AsseticBundle');
// Acm<NEWLINE>
// Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
// <NEWLINE>
$inputStream = $this->getInputStream("Acm\nAc\177\177s\tTest\n\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));
} else {
$this->markTestSkipped();
}
}
public function testAskHiddenResponse()
@ -128,4 +144,11 @@ class DialogHelperTest extends \PHPUnit_Framework_TestCase
{
return new StreamOutput(fopen('php://memory', 'r+', false));
}
private function hasSttyAvailable()
{
exec('stty 2>&1', $output, $exitcode);
return $exitcode === 0;
}
}