Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Autocomplete prompt #265

Closed
wants to merge 6 commits into from

Conversation

mokkabonna
Copy link
Contributor

I have created a POC for a autocomplete prompt. See the example for a live example.

Is this something that would be considered for inclusion in this library?

I have tried to use the api as used by other prompts although when introducing async I feel it is a bit weird. Any suggestions on how to do this instead? like the choices:

If you now define a function as your choices (as with other prompts) you have to return a inner function that gets called whenever the users input changes. This works fine, but maybe could be done better, and might be confusing.

Fake search function below (from the example):

choices: function (answers) {
  return function(searchTerm) {
    return new Promise(function(resolve, reject) {
      setTimeout(function() {
        resolve(states.filter(function(state) {
          return new RegExp(searchTerm, 'i').exec(state) !== null;
        }));
      }, 400); // simulate 400 ms delay for async request
    });
  };
}

Since the choices also aren't available at creation of the prompt it won't create a Choices object like normally, so I am creating that each time the search function is returning it's result. Is this necessary? Ideally I would just use a simple array, but there might be good reasons why you want them created as choice objects.

It is also an assumption that the search function returns a promise. I feel that promises are the way to go with async these days. Thoughts?

This PR is not complete, but meant as a proof of concept and as as place to discuss it. I have however already some tests.

I have also added a grunt develop task, just for my own sake really, I'll rebase and remove if not wanted.

The style and formatting of the code might also be a bit off, will gladly comply to whatever style is desired, just wasn't my focus right now.

As suggested here #225 I have based it of the list prompt.

image

this.firstRender = true;
this.selected = 0;

var def = this.opt.default;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what to do with default for this prompt, since we have a external unknown at runtime resultset. If you want a default, maybe have the service return that choice if no match? Or should we allow a default choice(s) that is used as resultset if the search returns empty array?

@derhuerst
Copy link

Looking forward to see this getting merged. 👍

@lazabogdan
Copy link

+1

@SBoudrias
Copy link
Owner

Hey! Thanks for working on this.

This prompt should be published as it's own npm package. Inquirer allow to be extended with custom prompt type:

inquirer.registerPrompt('search', require('your-module');
inquirer.prompt({ type: 'search', ... });

This would allow you to stay the owner the that module and fixing, patching and releasing new versions of it!

@SBoudrias SBoudrias closed this Aug 14, 2015
@mokkabonna
Copy link
Contributor Author

For anyone interested, I have started progress on the prompt here: https://github.com/mokkabonna/inquirer-autocomplete-prompt

Not published as a npm package yet. Feedback is welcome.

@patrick-motard
Copy link

Looks like inquirer-autocomplete-prompt is now published on npm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants