Skip to content

Commit 7f0567d

Browse files
committed
multiple completions
1 parent c9110e6 commit 7f0567d

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

app/InputHandler.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class InputHandler {
88

99
async getInput() {
1010
return new Promise((resolve) => {
11+
let tabCounter = 0;
1112
const onKeypress = (char, key) => {
1213
if (key.name === 'return' || key.name === 'enter') {
1314
this.readline.input.removeListener('keypress', onKeypress);
@@ -22,22 +23,33 @@ class InputHandler {
2223
// Autocomplete if there's only one suggestion
2324
this.inputBuffer = suggestions[0] + ' ';
2425
this.readline.write(null, { ctrl: true, name: 'u' }); // Clear the current line
25-
this.readline.write(this.inputBuffer); // Write the autocompleted input
26+
this.readline.write(this.inputBuffer);
2627
} else if (suggestions.length > 1) {
27-
console.log('\n' + suggestions.join(' '));
28-
this.readline.write(null, { ctrl: true, name: 'u' });
29-
this.readline.prompt();
30-
this.readline.write(this.inputBuffer); // Reprint the current input
28+
if (tabCounter === 0) {
29+
// First <TAB> press: ring the bell
30+
process.stdout.write('\x07');
31+
this.readline.write(null, { ctrl: true, name: 'u' });
32+
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+
tabCounter = 0;
41+
}
3142
} else {
3243
// No valid suggestions, ring the bell
3344
process.stdout.write('\x07');
34-
this.readline.write(null, { ctrl: true, name: 'u' }); // Clear the current line
45+
this.readline.write(null, { ctrl: true, name: 'u' });
3546
this.readline.write(this.inputBuffer);
47+
tabCounter = 0;
3648
}
3749
} else if (key.name === 'backspace') {
3850
// Handle Backspace
3951
this.inputBuffer = this.inputBuffer.slice(0, -1);
40-
this.readline.write(null, { ctrl: true, name: 'u' }); // Clear the current line
52+
this.readline.write(null, { ctrl: true, name: 'u' });
4153
this.readline.write(this.inputBuffer);
4254
} else {
4355
// Add the normal character pressed, to the input buffer

0 commit comments

Comments
 (0)