8000 [Console] Autocomplete cleanups/optimisations · symfony/symfony@9d94fc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d94fc7

Browse files
lmcdfabpot
authored andcommitted
[Console] Autocomplete cleanups/optimisations
1 parent 64bf80c commit 9d94fc7

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

src/Symfony/Component/Console/Helper/DialogHelper.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public function ask(OutputInterface $output, $question, $default = null, array $
8989
$ret = '';
9090

9191
$i = 0;
92-
$matches = array();
93-
$numMatches = 0;
9492
$ofs = -1;
93+
$matches = $autocomplete;
94+
$numMatches = count($matches);
9595

9696
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
9797
shell_exec('stty -icanon -echo');
@@ -111,29 +111,21 @@ public function ask(OutputInterface $output, $question, $default = null, array $
111111

112112
if ($i === 0) {
113113
$ofs = -1;
114+
$matches = $autocomplete;
115+
$numMatches = count($matches);
114116
}
115117

116-
// Erase characters from cursor to end of line
117-
$output->write("\033[K");
118+
// Pop the last character off the end of our string
118119
$ret = substr($ret, 0, $i);
119120

120121
$numMatches = 0;
121-
122-
continue;
123-
}
124-
125-
// Did we read an escape sequence?
126-
if ("\033" === $c) {
122+
} elseif ("\033" === $c) { // Did we read an escape sequence?
127123
$c .= fread($inputStream, 2);
128124

125+
// A = Up Arrow. B = Down Arrow
129126
if ('A' === $c[2] || 'B' === $c[2]) {
130-
if (0 === $i) {
131-
$matches = $autocomplete;
132-
$numMatches = count($matches);
133-
134-
if ('A' === $c[2] && -1 === $ofs) {
135-
$ofs = 0;
136-
}
127+
if ('A' === $c[2] && -1 === $ofs) {
128+
$ofs = 0;
137129
}
138130

139131
if (0 === $numMatches) {
@@ -143,11 +135,11 @@ public function ask(OutputInterface $output, $question, $default = null, array $
143135
$ofs += ('A' === $c[2]) ? -1 : 1;
144136
$ofs = ($numMatches + $ofs) % $numMatches;
145137
}
146-
} else if (ord($c) < 32) {
138+
} elseif (ord($c) < 32) {
147139
if ("\t" === $c || "\n" === $c) {
148140
if ($numMatches > 0) {
149141
$ret = $matches[$ofs];
150-
// Echo out completed match
142+
// Echo out remaining chars for current match
151143
$output->write(substr($ret, $i));
152144
$i = strlen($ret);
153145
}

0 commit comments

Comments
 (0)
0