我在node.js中寻找此功能,但我还没有找到它
我可以自己实施吗?据我所知,node.js在启动时没有加载任何文件(就像Bash与.bashrc
一样)并且我没有注意到以某种方式覆盖shell提示符。
有没有办法在不编写自定义shell的情况下实现它?
答案 0 :(得分:10)
你可以修补REPL:
var repl = require('repl').start()
var _complete = repl.complete
repl.complete = function(line) {
...
_complete.apply(this, arguments)
}
答案 1 :(得分:6)
仅作为参考。
readline
模块具有readline.createInterface(options)
方法,该方法接受使标签完成的可选completer
函数。
function completer(line) {
var completions = '.help .error .exit .quit .q'.split(' ')
var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })
// show all completions if none found
return [hits.length ? hits : completions, line]
}
和
function completer(linePartial, callback) {
callback(null, [['123'], linePartial]);
}
链接到api文档:http://nodejs.org/api/readline.html#readline_readline_createinterface_options
答案 2 :(得分:0)
您可以使用如下所示的完成函数来实现制表符功能。
const readline = require('readline');
/*
* This function returns an array of matched strings that starts with given
* line, if there is not matched string then it return all the options
*/
var autoComplete = function completer(line) {
const completions = 'var const readline console globalObject'.split(' ');
const hits = completions.filter((c) => c.startsWith(line));
// show all completions if none found
return [hits.length ? hits : completions, line];
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: autoComplete
});
rl.setPrompt("Type some character and press Tab key for auto completion....\n");
rl.prompt();
rl.on('line', (data) => {
console.log(`Received: ${data}`);
});