A primitive autocomplete prompt that provides full control over its UI and behaviors.
In one of my projects, I was trying to create the jest-watch-typeahead style autocomplete prompt. My initial attempt was to find a library to implement it. I found several good CLI prompt libraries, and most of them offered UI, behavior customization to some extent. However, those customizations were not enough to build the needed prompt. Therefore, I created this autocomplete prompt that extendable to control its UI and behaviors.
npm install --save cli-autocomplete-prompt
The autocomplete prompt can be created using the autoComplete
factory function. See the default-autocomplete directory for reference.
const { autoComplete } = require('cli-autocomplete-prompt');
const list = [
{ label: 'lib/cli/index.js', value: 'lib/cli/index.js' },
{ label: 'lib/cli/print.js', value: 'lib/cli/print.js' },
{ label: 'lib/mocha/run.js', value: 'lib/mocha/run.js' },
{ ... },
];
(async () => {
const options = { list };
const results = await autoComplete(options);
console.log(results);
})();
Use ↑ up
/ ↓ down
arrow keys to navigate. Use ↵ enter
key to
get the matched item[s].
Default behaviors:
- If the input is empty, it matches all items when the prompt is submitted.
- The suggestion logic uses the
String.includes
method on items' label property to update the list while the user types. - It prevents prompt submission when there is no match found.
Type: Function
Returns: Promise
It takes an options (optional) object and returns the matched items when the prompt is submitted.
Property | Type | Default | Description |
---|---|---|---|
promptLabel | string |
'' |
Prompt label to display |
limit | number |
10 | Max number of results to show |
list | array |
[] |
An array of list items. A list item should be an object with the label , value property. The label should be a type string , and the value can be anything. |
onSubmit | function |
undefined |
A hook to transform the matched items before they get submitted. It receives the matched items (array ) as an argument. |
In addition to the factory function, the package exports a class
and
a couple of utility functions to customize the prompt's default UI and behaviors.
View CUSTOMIZATION.md to learn more about the customization.
- Support wordwrap
- Tests
- CI Setup
Prompts - is a lightweight library to create beautiful and user-friendly interactive prompts. Some ideas have been taken from this project to build the autocomplete prompt.
MIT License © Sureshraj