Code Monkey home page Code Monkey logo

prompt-list's Introduction

prompt-list NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

List-style prompt. Can be used as a standalone prompt, or with a prompt system like enquirer.

Follow this project's author, Jon Schlinkert, for updates on this project and others.

prompt-list example

Install

Install with npm:

$ npm install --save prompt-list

Install

Install with npm:

$ npm install --save prompt-list

Example usage

var List = require('prompt-list');
var list = new List({
  name: 'order',
  message: 'What would you like to order?',
  // choices may be defined as an array or a function that returns an array
  choices: [
    'Coke',
    'Diet Coke',
    'Cherry Coke',
    {name: 'Sprite', disabled: 'Temporarily unavailable'},
    'Water'
  ]
});

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

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

Enquirer usage

Register the prompt as an enquirer plugin:

var Enquirer = require('enquirer');
var enquirer = new Enquirer();

enquirer.register('list', require('prompt-list'));

Enquirer examples

Enquirer supports both declarative, inquirer-style questions, and an expressive format, using the .question method:

Declarative

var questions = [
  {
    type: 'list',
    name: 'order',
    message: 'What would you like to order?',
    choices: [
      'Coke',
      'Diet Coke',
      'Cherry Coke',
      {name: 'Sprite', disabled: 'Temporarily unavailable'},
      'Water'
    ]
  }
];

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

Expressive

enquirer.question('order', 'What would you like to order?', {
  type: 'list',
  choices: [
    'Coke',
    'Diet Coke',
    'Cherry Coke',
    {name: 'Sprite', disabled: 'Temporarily unavailable'},
    'Water'
  ]
});

enquirer.ask(['order'])
  .then(function(answers) {
    console.log(answers);
  })
  .catch(function(err) {
    console.log(err);
  });

About

Related projects

You might also be interested in these projects:

Contributing

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

Contributors

Commits Contributor
45 jonschlinkert
6 doowb
3 albizures

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on December 28, 2017.

prompt-list's People

Contributors

albizures avatar doowb avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

prompt-list's Issues

Choices: what can I put in there?

Hi,

so I saw in your examples that in your array of choices, you can also put in objects:
{name: 'Sprite', disabled: 'Temporarily unavailable'}

Are these the only 2 attributes that are supported?
What would be lovely is that we would be able to have a name and a value, like an option in an HTML select element.
Is this possible with the current version?

onError handler missing

I was trying to use this prompt when I saw that error handling was not right. As in if there is a validation on the question and it returns "an error" you could still hit enter and it would terminate the process...

After looking at the source there seems to be an onError handler missing

enquirer.register("list", ...) throws

TypeError: Cannot set property 'errorMessage' of undefined
index.js:14
    at Checkbox (d:\create-adapter\node_modules\prompt-checkbox\index.js:14:21)
    at Radio (d:\create-adapter\node_modules\prompt-radio\index.js:12:12)
    at List (d:\create-adapter\node_modules\prompt-list\index.js:12:9)
    at Enquirer.register (d:\create-adapter\node_modules\enquirer\index.js:52:28)

Debugging shows that this is undefined at the above location.

Object with id and name as an option

Hi,

I've been using this excellent package in my project. I have noticed that when an item is selected then it is the name of the item that is returned. I have to then iterate my data source to find the item with the name selected to get the id that can be used later (kind of SELECT and OPTION HTML elements).

async chooseCourse(enquirer) {
  const courses = await this.getCourses();
  const choices = [...courses].map(o => {
    return {
      name: o.text,
      id: o.value
    };
  });

  var questions = [
    {
      type: "list",
      name: "action",
      message: "Choose a course ...",
      choices
    }
  ];

  const answer = await enquirer.ask(questions);
  const selectedCourse = _.find(choices, i => i.name == answer.action); // find the course

  await this.selectCourse(selectedCourse.id); // use the id
}

Is there a way how to force enquirer to return the whole object or this is not how it was designed and how it works?

Thanks,
Karel

Unexpected behavior using 'when'?

Hi, I'm working with a bunch of list questions using the when function, but I'm facing a weird behavior.
For example:

const questions = [{
  name: 'order',
  type: 'list',
  message: 'What would you like to order?',
  // choices may be defined as an array or a function that returns an array
  choices: [
    'Coke',
    'Diet Coke',
    'Cherry Coke',
    {name: 'Sprite', disabled: 'Temporarily unavailable'},
    'Water'
  ],
  when: function () {
    return false
  }
}]

enquirer
   .ask(questions)
   .then(answers => {
     console.log(answers)
   })
   .catch(err => {
     console.log(err)
   })

and the answers object will be:

{ order: 'Coke' }

this is unexpected at least for me, I was expecting the order's answer to be undefined
so, I'm not sure if this is the right behavior and if it is, how can I avoid this case?

Thanks

Prompt is eating ctrl+c inputs and SIGINT handlers

Node Version: 8.9.4
prompt-list Version: 3.2.0
OS: macOS 10.13.6, Windows 10

I've noticed that ctrl+c fails to work after a prompt has been run. Here's a simple reproduction:
https://gist.github.com/rkorsak/48c3e302cb37613cfc4b771827cdda30

I noticed a slightly similar issue had been reported and closed back in 2017, but I haven't yet had time to try to dig in and find a solution.

So far I've noticed this happening for both prompt-list and prompt-confirm, and I suspect it's more of an issue with one of the shared dependencies. If there's a better repo to report this in, or any other info I can provide, I'll be happy to help. Enquirer (and its various prompts) has a been a huge benefit to my project.

Feature request: prompt-list + cli-table

Hi there. Finding I would really like if there were a way to combine the selection mechanism of prompt-list with the display layout of something like cli-table (see the image at https://www.npmjs.com/package/cli-table). So, pressing up and down would highlight different items in the table. Not sure if there would be a way to work that in to prompt-list, or if it would have to be a separate package.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.