@@ -24,20 +24,29 @@ class InputHandler {
24
24
this . inputBuffer = suggestions [ 0 ] + ' ' ;
25
25
this . readline . write ( null , { ctrl : true , name : 'u' } ) ; // Clear the current line
26
26
this . readline . write ( this . inputBuffer ) ;
27
+ tabCounter = 0 ;
27
28
} else if ( suggestions . length > 1 ) {
28
- if ( tabCounter === 0 ) {
29
- // First <TAB> press: ring the bell
30
- process . stdout . write ( '\x07' ) ;
29
+ const commonPrefix = this . getLongestCommonPrefix ( suggestions ) ;
30
+ if ( commonPrefix . length > this . inputBuffer . length ) {
31
+ this . inputBuffer = commonPrefix ;
31
32
this . readline . write ( null , { ctrl : true , name : 'u' } ) ;
32
33
this . readline . write ( this . inputBuffer ) ;
33
- tabCounter ++ ;
34
- } else {
35
- // Second <TAB> press: display suggestions
36
- console . log ( '\n' + suggestions . join ( ' ' ) ) ;
37
- this . readline . write ( null , { ctrl : true , name : 'u' } ) ;
38
- this . readline . prompt ( ) ;
39
- this . readline . write ( this . inputBuffer ) ;
40
34
tabCounter = 0 ;
35
+ } else {
36
+ if ( tabCounter === 0 ) {
37
+ // First <TAB> press: ring the bell
38
+ process . stdout . write ( '\x07' ) ;
39
+ this . readline . write ( null , { ctrl : true , name : 'u' } ) ;
40
+ this . readline . write ( this . inputBuffer ) ;
41
+ tabCounter ++ ;
42
+ } else {
43
+ // Second <TAB> press: display suggestions
44
+ console . log ( '\n' + suggestions . join ( ' ' ) ) ;
45
+ this . readline . write ( null , { ctrl : true , name : 'u' } ) ;
46
+ this . readline . prompt ( ) ;
47
+ this . readline . write ( this . inputBuffer ) ;
48
+ tabCounter = 0 ; // Reset the counter
49
+ }
41
50
}
42
51
} else {
43
52
// No valid suggestions, ring the bell
@@ -60,6 +69,18 @@ class InputHandler {
60
69
} ) ;
61
70
}
62
71
72
+ getLongestCommonPrefix ( suggestions ) {
73
+ if ( suggestions . length === 0 ) return '' ;
74
+ let prefix = suggestions [ 0 ] ;
75
+ for ( let i = 1 ; i < suggestions . length ; i ++ ) {
76
+ while ( ! suggestions [ i ] . startsWith ( prefix ) ) {
77
+ prefix = prefix . slice ( 0 , - 1 ) ;
78
+ if ( prefix === '' ) return '' ;
79
+ }
80
+ }
81
+ return prefix ;
82
+ }
83
+
63
84
isExecutable ( stats ) {
64
85
return ( stats . mode & 0o111 ) !== 0 ;
65
86
}
0 commit comments