How do I implement tab completion in node.js shell? How do I implement tab completion in node.js shell? shell shell

How do I implement tab completion in node.js shell?


You could monkey-patch the REPL. Note that you must use the callback version of the completer, otherwise it won't work correctly:

var repl = require('repl').start()var _completer = repl.completer.bind(repl)repl.completer = function(line, cb) {  // ...  _completer(line, cb)}


Just as a reference.

readline module has readline.createInterface(options) method that accepts an optional completer function that makes a tab completion.

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]}

and

function completer(linePartial, callback) {  callback(null, [['123'], linePartial]);}

link to the api docs: http://nodejs.org/api/readline.html#readline_readline_createinterface_options


You can implement tab functionality using completer function like below.

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}`);});

Reference :https://self-learning-java-tutorial.blogspot.com/2018/10/nodejs-readlinecreateinterfaceoptions_2.html