@@ -15,7 +15,6 @@ class Readline extends EventEmitter implements ReadableStreamInterface
1515 private $ linebuffer = '' ;
1616 private $ linepos = 0 ;
1717 private $ echo = true ;
18- private $ autocomplete = null ;
1918 private $ move = true ;
2019 private $ encoding = 'utf-8 ' ;
2120
@@ -29,6 +28,9 @@ class Readline extends EventEmitter implements ReadableStreamInterface
2928 private $ historyUnsaved = null ;
3029 private $ historyLimit = 500 ;
3130
31+ private $ autocomplete = null ;
32+ private $ autocompleteSuggestions = 8 ;
33+
3234 public function __construct (ReadableStreamInterface $ input , WritableStreamInterface $ output )
3335 {
3436 $ this ->input = $ input ;
@@ -546,30 +548,39 @@ public function onKeyTab()
546548
547549 // search longest common prefix among all possible matches
548550 $ found = reset ($ words );
549- array_shift ($ words );
550- $ others = count ($ words );
551+ $ all = count ($ words );
551552 while ($ found !== $ word ) {
552553 // count all words that start with $found
553554 $ matches = count (array_filter ($ words , function ($ word ) use ($ found ) {
554555 return strpos ($ word , $ found ) === 0 ;
555556 }));
556557
557558 // ALL words match $found => common substring found
558- if ($ others === $ matches ) {
559+ if ($ all === $ matches ) {
559560 break ;
560561 }
561562
562563 // remove last letter from $found and try again
563564 $ found = (string )substr ($ found , 0 , -1 );
564565 }
565566
566- // current prefix has multiple possible completions => abort
567- if ($ found === $ word && $ others ) {
567+ // found more than once possible match with this prefix => print options
568+ if ($ found === $ word && $ all > 1 ) {
569+ // limit number of possible matches
570+ if (count ($ words ) > $ this ->autocompleteSuggestions ) {
571+ $ more = count ($ words ) - ($ this ->autocompleteSuggestions - 1 );
572+ $ words = array_slice ($ words , 0 , $ this ->autocompleteSuggestions - 1 );
573+ $ words []= '(+ ' . $ more . ' others) ' ;
574+ }
575+
576+ $ this ->output ->write ("\n" . implode (' ' , $ words ) . "\n" );
577+ $ this ->redraw ();
578+
568579 return ;
569580 }
570581
571582 // append single space after match unless there's a postfix or there are multiple completions
572- if ($ postfix === '' && $ others === 0 ) {
583+ if ($ postfix === '' && $ all === 1 ) {
573584 $ found .= ' ' ;
574585 }
575586
0 commit comments