Tuesday, 2 October 2018

node.js: readline.createInterface(options): How to use completer function?

Completer function is used to support autocomplete feature.

Step 1: Write a function that returns an array of matched strings for the given input.

/*
 * 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];
}

Step 2: Create a new readline.Interface instance by setting completer option to completer function.

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  completer: autoComplete
});

Find the below working example.

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

Sample Output
Type some character and press Tab key for auto completion....
Q
var           const         readline      console       globalObject
Type some character and press Tab key for auto completion....
cons
const    console
Type some character and press Tab key for auto completion....
console
Received: console


As you see the output,
a.   I entered Q followe by Tab key, since there is no matched strings, my application return all the help strings.
b.   I entered cons followed by Tab key, since there are two matched string const, console, application return the same.

The completer function can be called asynchronously if it accepts two arguments:

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

HelloWorld.js
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, callback) {
  const completions = 'var const readline console globalObject'.split(' ');
  const hits = completions.filter((c) => c.startsWith(line));
  
   setTimeout(
      () => callback(null, [hits.length ? hits : completions, line]),
      2000
    );
}


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


You do not need to write this 'callback' function, it just gets given to you as a parameter to your completer function. Node creates the function internally somewhere. The way Node has written the callback expects it to be given an error in the first position and the result in the second position. This (err, value) style is very common in Node and other libraries often will use it too.

I am calling the function 'callback' with null, since there is not error here.






Previous                                                 Next                                                 Home

No comments:

Post a Comment