Skip to content

Commit d3399d3

Browse files
committed
executable completion
1 parent 93f044b commit d3399d3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

app/InputHandler.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const path = require('path');
2+
const fs = require("fs");
13
class InputHandler {
24
constructor(readline) {
35
this.readline = readline;
@@ -23,6 +25,7 @@ class InputHandler {
2325
this.readline.write(this.inputBuffer); // Write the autocompleted input
2426
} else if (suggestions.length > 1) {
2527
console.log('\n' + suggestions.join(' '));
28+
this.readline.write(null, { ctrl: true, name: 'u' });
2629
this.readline.prompt();
2730
this.readline.write(this.inputBuffer); // Reprint the current input
2831
} else {
@@ -43,12 +46,32 @@ class InputHandler {
4346
});
4447
}
4548

49+
isExecutable(stats) {
50+
return (stats.mode & 0o111) !== 0;
51+
}
52+
4653
getSuggestions(input) {
4754
const suggestions = [];
4855

4956
const commands = ['echo', 'exit', 'pwd', 'cd', 'type'];
5057
suggestions.push(...commands.filter((cmd) => cmd.startsWith(input)));
5158

59+
const pathDirs = process.env.PATH.split(path.delimiter);
60+
for (const dir of pathDirs) {
61+
try {
62+
const files = fs.readdirSync(dir); // Read files in the directory
63+
for (const file of files) {
64+
const filePath = path.join(dir, file);
65+
const stats = fs.statSync(filePath);
66+
if (file.startsWith(input) && stats.isFile() && this.isExecutable(stats)) {
67+
if(!suggestions.includes(file)) suggestions.push(file);
68+
}
69+
}
70+
} catch (err) {
71+
continue;
72+
}
73+
}
74+
5275
return suggestions;
5376
}
5477
}

0 commit comments

Comments
 (0)