Giter Site home page Giter Site logo

enquirer / prompt-expand Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 2.0 92 KB

List prompt, where the answer is selected using a shortcut. Can be used as a standalone prompt, or as a plugin for Enquirer.

Home Page: http://enquirer.io

License: MIT License

JavaScript 100.00%
expand prompt command-line terminal question ask

prompt-expand's Introduction

prompt-expand NPM version NPM monthly downloads NPM total downloads

Expand prompt. Can be used as a standalone prompt, or with a prompt system like Enquirer.

prompt-expand example

Install

Install with npm:

$ npm install --save prompt-expand

Usage

var Prompt = require('prompt-expand');
var prompt = new Prompt({
  message: 'What action should be taken on file.js?',
  name: 'conflict',
  default: 'x',
  choices: [
    {
      key: 'y',
      name: 'Overwrite',
      value: 'overwrite'
    },
    {
      key: 'a',
      name: 'Overwrite this one and all next',
      value: 'overwrite_all'
    },
    {
      key: 'd',
      name: 'Show diff',
      value: 'diff'
    },
    new Prompt.Separator(),
    {
      key: 'x',
      name: 'Abort',
      value: 'abort'
    }
  ]
});

// async
prompt.ask(function(answer) {
  console.log({file: answer});
});

// promise
prompt.run()
  .then(function(answer) {
    console.log({file: answer});
  })
  .catch(function(err) {
    console.log(err);
  });

Enquirer usage

Register as a plugin with enquirer.

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

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

Functional-style questions

Define questions using the .question method.

enquirer.question('file', 'Conflict on `file.js`: ', {
  type: 'expand',
  // "default" can be the index of the default choice, 
  // or the `key` or `name` of the default choice
  default: 'x', 
  choices: [
    {
      key: 'y',
      name: 'Overwrite',
      value: 'overwrite'
    },
    {
      key: 'a',
      name: 'Overwrite this one and all next',
      value: 'overwrite_all'
    },
    {
      key: 'd',
      name: 'Show diff',
      value: 'diff'
    },
    enquirer.separator(),
    {
      key: 'x',
      name: 'Abort',
      value: 'abort'
    }
  ]
});

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

Declarative-style questions

Define questions using a declarative, Inquirer-style format.

var questions = [
  {
    type: 'expand',
    message: 'Conflict on `file.js`: ',
    default: 'x',
    name: 'file',
    choices: [
      {
        key: 'y',
        name: 'Overwrite',
        value: 'overwrite'
      },
      {
        key: 'a',
        name: 'Overwrite this one and all next',
        value: 'overwrite_all'
      },
      {
        key: 'd',
        name: 'Show diff',
        value: 'diff'
      },
      enquirer.separator(),
      {
        key: 'x',
        name: 'Abort',
        value: 'abort'
      }
    ]
  }
];

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

Attribution

Based on the expand prompt in inquirer.

About

Related projects

Contributing

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

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 May 27, 2017.

prompt-expand's People

Contributors

jonschlinkert avatar thetutlage avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

prompt-expand's Issues

Letters not automatically included in expanded options display

The animated gif referenced by the README.md shows that I should expect to see the letters in the expanded options display, like this:

? What action should be taken on file.js?  (yadXh)
  y) Overwrite
  a) Overwrite this one and all next
  d) Show diff
 ────────
  x) Abort
  h) Help
  Answer:

However, when I run the sample code, I get this instead:

? What action should be taken on file.js?  (yadXh)
  overwrite
  overwrite_all
  diff
 ────────
  abort
  Help
  Answer:

Ignoring issue #1 (the name and value are reversed), it doesn't seem to be adding the letters to the display (e.g. "y) ")

Default doesn't work

If I run node ./examples/prompt.js from the repo, it starts with this prompt:

? Conflict on file.js  (yadXh)

Because 'x' is the default, and the letter X is capitalized in the prompt, I expect that pressing ENTER at this point would choose option 'x'. Instead I get this when I press ENTER:

? Conflict on file.js
>> Please enter a valid letter

Option name and value are reversed

The sample code in README.md shows how the displayed option string and the corresponding answer string can be different. But running the example seems to have the two strings reversed.

For instance:

    {
      key: 'a',
      name: 'Overwrite this one and all next',
      value: 'overwrite_all'
    },

I would expect this to display as Overwrite this one and all next. But here is what I get when I run that sample:

? What action should be taken on file.js?  (yadXh)
  overwrite
  overwrite_all
  diff
 ────────
  abort
  Help
  Answer:

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.