如何在node.js shell中实现tab完成?

Dr *_*Kay 13 javascript shell node.js

我在node.js中寻找这个功能,但我还没有找到它.
我可以自己实施吗?据我所知,node.js在启动时没有加载任何文件(就像Bash那样.bashrc),我没有注意到以任何方式覆盖shell提示符.

有没有办法在不编写自定义shell的情况下实现它?

the*_*ejh 9

你可以修补REPL:

var repl = require('repl').start()
var _complete = repl.complete
repl.complete = function(line) {
  ...
  _complete.apply(this, arguments)
}
Run Code Online (Sandbox Code Playgroud)


jiy*_*ong 7

仅作为参考.

readlinemodule具有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]
}
Run Code Online (Sandbox Code Playgroud)

function completer(linePartial, callback) {
  callback(null, [['123'], linePartial]);
}
Run Code Online (Sandbox Code Playgroud)

链接到api文档:http://nodejs.org/api/readline.html#readline_readline_createinterface_options