@@ -200,6 +200,7 @@ protected function writeError(OutputInterface $output, \Exception $error)
200
200
*/
201
201
private function autocomplete (OutputInterface $ output , Question $ question , $ inputStream , array $ autocomplete ): string
202
202
{
203
+ $ fullChoice = '' ;
203
204
$ ret = '' ;
204
205
205
206
$ i = 0 ;
@@ -226,6 +227,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
226
227
} elseif ("\177" === $ c ) { // Backspace Character
227
228
if (0 === $ numMatches && 0 !== $ i ) {
228
229
--$ i ;
230
+ $ fullChoice = substr ($ fullChoice , 0 , -1 );
229
231
// Move cursor backwards
230
232
$ output ->write ("\033[1D " );
231
233
}
@@ -262,8 +264,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
262
264
if ($ numMatches > 0 && -1 !== $ ofs ) {
263
265
$ ret = $ matches [$ ofs ];
264
266
// Echo out remaining chars for current match
265
- $ output ->write (substr ($ ret , $ i ));
266
- $ i = \strlen ($ ret );
267
+ $ remainingCharacters = substr ($ ret , \strlen (trim ($ this ->mostRecentlyEnteredValue ($ fullChoice ))));
268
+ $ output ->write ($ remainingCharacters );
269
+ $ fullChoice .= $ remainingCharacters ;
270
+ $ i = \strlen ($ fullChoice );
267
271
}
268
272
269
273
if ("\n" === $ c ) {
@@ -282,14 +286,21 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
282
286
283
287
$ output ->write ($ c );
284
288
$ ret .= $ c ;
289
+ $ fullChoice .= $ c ;
285
290
++$ i ;
286
291
292
+ $ tempRet = $ ret ;
293
+
294
+ if ($ question instanceof ChoiceQuestion && $ question ->isMultiselect ()) {
295
+ $ tempRet = $ this ->mostRecentlyEnteredValue ($ fullChoice );
296
+ }
297
+
287
298
$ numMatches = 0 ;
288
299
$ ofs = 0 ;
289
300
290
301
foreach ($ autocomplete as $ value ) {
291
302
// If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle)
292
- if (0 === strpos ($ value , $ ret )) {
303
+ if (0 === strpos ($ value , $ tempRet )) {
293
304
$ matches [$ numMatches ++] = $ value ;
294
305
}
295
306
}
@@ -301,8 +312,9 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
301
312
if ($ numMatches > 0 && -1 !== $ ofs ) {
302
313
// Save cursor position
303
314
$ output ->write ("\0337 " );
304
- // Write highlighted text
305
- $ output ->write ('<hl> ' .OutputFormatter::escapeTrailingBackslash (substr ($ matches [$ ofs ], $ i )).'</hl> ' );
315
+ // Write highlighted text, complete the partially entered response
316
+ $ charactersEntered = \strlen (trim ($ this ->mostRecentlyEnteredValue ($ fullChoice )));
317
+ $ output ->write ('<hl> ' .OutputFormatter::escapeTrailingBackslash (substr ($ matches [$ ofs ], $ charactersEntered )).'</hl> ' );
306
318
// Restore cursor position
307
319
$ output ->write ("\0338 " );
308
320
}
@@ -311,7 +323,24 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
311
323
// Reset stty so it behaves normally again
312
324
shell_exec (sprintf ('stty %s ' , $ sttyMode ));
313
325
314
- return $ ret ;
326
+ return $ fullChoice ;
327
+ }
328
+
329
+ private function mostRecentlyEnteredValue ($ entered )
330
+ {
331
+ $ tempEntered = $ entered ;
332
+
333
+ // Determine the most recent value that the user entered
334
+ if (false !== strpos ($ entered , ', ' )) {
335
+ $ choices = explode (', ' , $ entered );
336
+ $ lastChoice = trim ($ choices [\count ($ choices ) - 1 ]);
337
+
338
+ if (\strlen ($ lastChoice ) > 0 ) {
339
+ $ tempEntered = $ lastChoice ;
340
+ }
341
+ }
342
+
343
+ return $ tempEntered ;
315
344
}
316
345
317
346
/**
0 commit comments