Skip to content

Commit c2585e6

Browse files
committed
partial completions
1 parent a26c7ee commit c2585e6

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

app/InputHandler.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,29 @@ class InputHandler {
2424
this.inputBuffer = suggestions[0] + ' ';
2525
this.readline.write(null, { ctrl: true, name: 'u' }); // Clear the current line
2626
this.readline.write(this.inputBuffer);
27+
tabCounter = 0;
2728
} 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;
3132
this.readline.write(null, { ctrl: true, name: 'u' });
3233
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);
4034
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+
}
4150
}
4251
} else {
4352
// No valid suggestions, ring the bell
@@ -60,6 +69,18 @@ class InputHandler {
6069
});
6170
}
6271

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+
6384
isExecutable(stats) {
6485
return (stats.mode & 0o111) !== 0;
6586
}

0 commit comments

Comments
 (0)