[Console] Fix `stty` reset when using `DialogHelper#ask()` with autocomplete functionality
[Console] Split tests for `DialogHelper` that tests `ask()` method
Fix bad unit test with undefined offset (spotted by @stloyd)
Fix bug where backspacing to an empty string and using the arrow keys would fail. Added test to prevent in future
merged branch stloyd/bugfix/console_autocomplete (PR #6604)

This PR was merged into the master branch.

Commits
-------

4d7c895 Fix bug where backspacing to an empty string and using the arrow keys would fail. Added test to prevent in future
cd1def3 Fix bad unit test with undefined offset (spotted by @stloyd)
7f149ae [Console] Split tests for `DialogHelper` that tests `ask()` method
e6574de [Console] Fix `stty` reset when using `DialogHelper#ask()` with autocomplete functionality

Discussion
----------

[Console] Fix `DialogHelper#ask()` with autocomplete functionality

Bug fix: yes
Feature addition: no
BC break: no
Symfony2 tests pass: yes

This PR fixes failing test after: 9d94fc7 as well as correctly resets `stty` to prevent _strange_ data showing-up in console:
![console](https://f.cloud.github.com/assets/67402/47882/673a5dae-58ed-11e2-8bab-30a7c41733f5.png)

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

by fabpot at 2013-01-07T17:27:40Z

ping @lmcd

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

by lmcd at 2013-01-08T03:09:51Z

I'm using -1 as a neutral position when incrementing/decrementing the offset using arrow keys. If it started as zero, then it'd be incremented to 1 on down arrow and we'd miss the first (zero-index) match.

An easier fix for this is to replace all `if ($numMatches > 0)` with `if ($numMatches > 0 && -1 !== $ofs)`

Also, this:

    if ($i === 0) {
        $ofs = -1;
        $matches = $autocomplete;
        $numMatches = count($matches);
    }

    // Pop the last character off the end of our string
    $ret = substr($ret, 0, $i);

    $numMatches = 0;

Should be this:

    if ($i === 0) {
        $ofs = -1;
        $matches = $autocomplete;
        $numMatches = count($matches);
    }
    else {
        $numMatches = 0;
    }

    // Pop the last character off the end of our string
    $ret = substr($ret, 0, $i);

Edit: put these parts into a new pull request to avoid confusion https://github.com/symfony/symfony/pull/6614

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

by lmcd at 2013-01-08T03:11:20Z

Good catch on the stty issue :+1:

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

by stloyd at 2013-01-08T12:14:01Z

@fabpot @lmcd I have "merged" fixes from: #6614 and removed those `env`s when used with `stty`.

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

by lmcd at 2013-01-08T12:16:05Z

@stloyd Awesome :)