1.3.2 • Published 6 years ago

@moyuyc/inquirer-autocomplete-prompt v1.3.2

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

@moyuyc/inquirer-autocomplete-prompt

Forked from inquirer-autocomplete-prompt, but exported readline as third argument in source for suggesting matched text

Greenkeeper badge

Autocomplete prompt for inquirer

build status dependency status

Installation

npm install --save @moyuyc/inquirer-autocomplete-prompt

Usage

This prompt is anonymous, meaning you can register this prompt with the type name you please:

inquirer.registerPrompt('autocomplete', require('@moyuyc/inquirer-autocomplete-prompt'));
inquirer.prompt({
  type: 'autocomplete',
  ...
})

Change autocomplete to whatever you might prefer.

Options

Note: allowed options written inside square brackets ([]) are optional. Others are required.

type, name, message, source, pageSize, filter, when, suggestOnly, validate, default, noResultText, searchText, throttleWaitMilliseconds

See inquirer readme for meaning of all except source and suggestOnly.

Source will be called with previous answers object and the current user input each time the user types.

Source will be called once at at first before the user types anything with undefined as the value. If a new search is triggered by user input it maintains the correct order, meaning that if the first call completes after the second starts, the results of the first call are never displayed.

suggestOnly is default false. Setting it to true turns the input into a normal text input. Meaning that pressing enter selects whatever value you currently have. And pressing tab autocompletes the currently selected value in the list. This way you can accept manual input instead of forcing a selection from the list.

validate is only active when suggestOnly is set to true. It behaves like validate for the input prompt.

default is type of string. Setting it as default value.

noResultText is type of string | null. Setting it as text to view (hide it when null) when no results. ('No results...' by default)

searchText is type of string | null. Setting it as text to view (hide it when null) when is searching. ('Searching...' by default)

throttleWaitMilliseconds is type of number for throttle search call times (400 by default)

Example

const autoComplete = require('@moyuyc/inquirer-autocomplete-prompt');
inquirer.registerPrompt('autocomplete', autoComplete);
inquirer
  .prompt([
    {
      type: 'autocomplete',
      name: 'from',
      message: 'Select a state to travel from',
      source: function(answersSoFar, input, rl) {
        // e.g. sliceInput('closed #123,#222', { delimiter: '\\s,', cursor: 7 })
        // output: { leftIndex: 7, matching: '#123', rightIndex: 11 }
        //  input.slice(0, leftIndex) + matching + input.slice(rightIndex) === input
        const { matching, leftIndex, rightIndex } = autoComplete.sliceInput(
          input,
          // delimiter:
          //    sliceInput
          //    type: string
          //    default: `'\\s'`
          { ...rl, delimiter: ',\\s' }
        );
        // rl.cursor
        return myApi.searchStates(input);
      },
    },
  ])
  .then(function(answers) {
    //etc
  });

See also example.js for a working example.

I recommend using this package with fuzzy if you want fuzzy search. Again, see the example for a demonstration of this.

Autocomplete prompt

Credits

Martin Hansen

License

ISC