Skip to content

Docs | Autocomplete

dc edited this page Jan 26, 2016 · 5 revisions

Autocomplete

Vorpal supports powerful, robust tabbed autocompletion by default, along with custom autocompletion on both commands and options.

This feature is designed to exactly mirror the Linux-style tabbed autocompletion you are familiar with.

How it works

Let's suppose you are running a Vorpal app with three commands registered: eat, feed and water. When you start the app and press tab twice, you'll get those three commands suggested:

$ node ./myapp.js
app$ [tab] [tab]
eat  feed  water

Typing f and then pressing tab will autocomplete to feed:

app$ f [tab]
app$ feed 

Command Autocompletion

Now suppose we want to suggest that one should feed the cat, dog or horse. We would register the following command in our app:

vorpal.command('feed [animal]')
  .autocomplete(['cat', 'dog', 'horse'])
  .action(feedThem);

On pressing tab now, we would get those suggestions:

$ node ./myapp.js
app$ feed [tab] [tab]
cat  dog  horse
Using a Function

If the list of animals we feed is somewhat dynamic, we can run a function to determine it:

vorpal.command('feed [animal]')
  .autocomplete({
    data: function () {
      return getAnimals;
    }
  })
  .action(feedThem);
Using a Callback

If we need it to be async, we can pass in a callback as the second parameter (the first is what the user has typed so far):

vorpal.command('feed [animal]')
  .autocomplete({
    data: function (input, callback) {
      getAnimals(function (array) {
        callback(array);
      });
    }
  })
  .action(feedThem);
Using a Promise

We can also do a Promise:

vorpal.command('feed [animal]')
  .autocomplete({
    data: function () {
      return getAnimalsPromise;
    }
  })
  .action(feedThem);

Options

Clone this wiki locally