Skip to content

This repository has been archived, use the autocomplete prompt in Enquirer instead.

License

Notifications You must be signed in to change notification settings

enquirer/prompt-autocompletion

Repository files navigation

prompt-autocompletion NPM version NPM downloads

Prompt that autocompletes as you type. Can be used standalone or with a prompt system like enquirer

prompt-autocompletion example

Install

Install with npm:

$ npm install --save prompt-autocompletion

Example usage

var states = ['Alabama', 'Alaska', 'American Samoa', ...];
var AutoComplete = require('prompt-autocompletion');
var autocomplete = new AutoComplete({
  type: 'autocomplete',
  name: 'from',
  message: 'Select a state to travel from',
  source: searchStates
});

// promise
autocomplete.run()
  .then(function(answer) {
    console.log(answer);
  });

// or async
autocomplete
  .ask(function(answer) {
    console.log(answer);
  });

function searchStates(answers, input) {
  return new Promise(function(resolve) {
    resolve(states.filter(filter(input)));
  });
}

function filter(input) {
  return function(state) {
    return new RegExp(input, 'i').exec(state) !== null;
  };
}

Enquirer usage

var states = ['Alabama', 'Alaska', 'American Samoa', ...];
var Enquirer = require('enquirer');
var enquirer = new Enquirer();

enquirer.register('autocomplete', require('prompt-autocompletion'));
var questions = [
  {
    type: 'autocomplete',
    name: 'from',
    message: 'Select a state to travel from',
    source: searchStates
  },
  {
    type: 'autocomplete',
    name: 'to',
    message: 'Select a state to travel to',
    source: searchStates
  }
];

enquirer.ask(questions)
  .then(function(answers) {
    console.log(answers);
  })
  .catch(function(err) {
    console.log(err);
  });

function searchStates(answers, input) {
  return new Promise(function(resolve) {
    resolve(states.filter(filter(input)));
  });
}

function filter(input) {
  return function(state) {
    return new RegExp(input, 'i').exec(state) !== null;
  };
}

Prompts

Other prompt modules:

Attribution

This is entirely based on https://www.npmjs.com/package/inquirer-autocomplete-prompt Copyright (c) 2015, Martin Hansen martin@martinhansen.no

Thanks, Martin!

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for avice on opening issues, pull requests, and coding standards.

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb-generate-readme, v0.1.31, on October 13, 2016.