list-selector-cli

0.0.3 • Public • Published

Git-style interactive list selector for Node CLI apps

  • Does one thing: Lets the users of your NodeJS app interactively select/de-select items from a list by indices and ranges on the commandline.

  • Familiar "UI" -- to GitHubbers at least! Same interface as the trusty "git add --interactive"

  • Brain-dead simple to use in your project

  • Options can be any array of toString-able things

  • Get the user's response in a callback OR a jQuery-style promise

Example: Let's make a sandwich

Probably better for selecting filepaths, but I'm hungry right now so...

Let's choose the toppings on your sandwich via CLI.

What you do

  1. Install from NPM:
npm install list-selector-cli`
  1. Require it in your project:
var cliselect = require("list-selector-cli");
  1. Pick an array of options:
var options = ["lettuce", "tomatoes", "spinach", "ham"];
  1. Create your list:
var sandwichChoice = new cliselect(options);
  1. Prompt the user: console.log("What would you like on your sandwich?")

  2. Let them interactively choose their options and handle their response

  • In a callback:
sandwichChoice.prompt(function(selection) { /** do something **/ });
  • Or with a promise:
var promise = sandwichChoice.prompt();
promise.done(function(selection) { /** do something **/ });

What you get

$ node examples/sandwich.js
What would you like on your sandwich?

 0:  lettuce
 1:  tomatoes
 2:  spinach
 3:  avocado
 4:  ham
 5:  bacon
 6:  oil
 7:  vinegar

Update selection (blank to accept)>>: 

Hmm... All the veggies and some oil, please.

Update selection (blank to accept)>>: 0-4,6
*0:  lettuce
*1:  tomatoes
*2:  spinach
*3:  avocado
*4:  ham
 5:  bacon
*6:  oil
 7:  vinegar
Update selection (blank to accept)>>:

Crap, I don't eat ham. De-ham the sandwich, plz.

Update selection (blank to accept)>>:-4
-4
*0:  lettuce
*1:  tomatoes
*2:  spinach
*3:  avocado
 4:  ham
 5:  bacon
*6:  oil
 7:  vinegar
Update selection (blank to accept)>>:

Eat-able? Yes. <Enter>

[ 'lettuce', 'tomatoes', 'spinach', 'avocado', 'oil' ]

Totally pitching a commandline interface to Subway...

Maybe ssh.subway.com: "open 8am to 9pm and 24 hrs a day on port 22..." :-)

Package Sidebar

Install

npm i list-selector-cli

Weekly Downloads

2

Version

0.0.3

License

MIT

Last publish

Collaborators

  • willyb