@@ -239,6 +239,7 @@ protected function writeError(OutputInterface $output, \Exception $error)
239
239
*/
240
240
private function autocomplete (OutputInterface $ output , Question $ question , $ inputStream , array $ autocomplete )
241
241
{
242
+ $ fullChoice = '' ;
242
243
$ ret = '' ;
243
244
244
245
$ i = 0 ;
@@ -265,6 +266,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
265
266
} elseif ("\177" === $ c ) { // Backspace Character
266
267
if (0 === $ numMatches && 0 !== $ i ) {
267
268
--$ i ;
269
+ $ fullChoice = substr ($ fullChoice , 0 , -1 );
268
270
// Move cursor backwards
269
271
$ output ->write ("\033[1D " );
270
272
}
@@ -301,8 +303,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
301
303
if ($ numMatches > 0 && -1 !== $ ofs ) {
302
304
$ ret = $ matches [$ ofs ];
303
305
// Echo out remaining chars for current match
304
- $ output ->write (substr ($ ret , $ i ));
305
- $ i = \strlen ($ ret );
306
+ $ remainingCharacters = substr ($ ret , \strlen (trim ($ this ->mostRecentlyEnteredValue ($ fullChoice ))));
307
+ $ output ->write ($ remainingCharacters );
308
+ $ fullChoice .= $ remainingCharacters ;
309
+ $ i = \strlen ($ fullChoice );
306
310
}
307
311
308
312
if ("\n" === $ c ) {
@@ -321,14 +325,21 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
321
325
322
326
$ output ->write ($ c );
323
327
$ ret .= $ c ;
328
+ $ fullChoice .= $ c ;
324
329
++$ i ;
325
330
331
+ $ tempRet = $ ret ;
332
+
333
+ if ($ question instanceof ChoiceQuestion && $ question ->isMultiselect ()) {
334
+ $ tempRet = $ this ->mostRecentlyEnteredValue ($ fullChoice );
335
+ }
336
+
326
337
$ numMatches = 0 ;
327
338
$ ofs = 0 ;
328
339
329
340
foreach ($ autocomplete as $ value ) {
330
341
// If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle)
331
- if (0 === strpos ($ value , $ ret )) {
342
+ if (0 === strpos ($ value , $ tempRet )) {
332
343
$ matches [$ numMatches ++] = $ value ;
333
344
}
334
345
}
@@ -340,8 +351,9 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
340
351
if ($ numMatches > 0 && -1 !== $ ofs ) {
341
352
// Save cursor position
342
353
$ output ->write ("\0337 " );
343
- // Write highlighted text
344
- $ output ->write ('<hl> ' .OutputFormatter::escapeTrailingBackslash (substr ($ matches [$ ofs ], $ i )).'</hl> ' );
354
+ // Write highlighted text, complete the partially entered response
355
+ $ charactersEntered = \strlen (trim ($ this ->mostRecentlyEnteredValue ($ fullChoice )));
356
+ $ output ->write ('<hl> ' .OutputFormatter::escapeTrailingBackslash (substr ($ matches [$ ofs ], $ charactersEntered )).'</hl> ' );
345
357
// Restore cursor position
346
358
$ output ->write ("\0338 " );
347
359
}
@@ -350,7 +362,24 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
350
362
// Reset stty so it behaves normally again
351
363
shell_exec (sprintf ('stty %s ' , $ sttyMode ));
352
364
353
- return $ ret ;
365
+ return $ fullChoice ;
366
+ }
367
+
368
+ private function mostRecentlyEnteredValue ($ entered )
369
+ {
370
+ $ tempEntered = $ entered ;
371
+
372
+ // Determine the most recent value that the user entered
373
+ if (false !== strpos ($ entered , ', ' )) {
374
+ $ choices = explode (', ' , $ entered );
375
+ $ lastChoice = trim ($ choices [\count ($ choices ) - 1 ]);
376
+
377
+ if (\strlen ($ lastChoice ) > 0 ) {
378
+ $ tempEntered = $ lastChoice ;
379
+ }
380
+ }
381
+
382
+ return $ tempEntered ;
354
383
}
355
384
356
385
/**
0 commit comments