Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

inquirer

Package Overview
Dependencies
Maintainers
1
Versions
183
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inquirer - npm Package Compare versions

Comparing version 0.2.5 to 0.3.0

examples/long-list.js

9

examples/checkbox.js

@@ -14,2 +14,3 @@ /**

choices: [
new inquirer.Separator("The usual:"),
{

@@ -22,9 +23,13 @@ name: "Peperonni"

{
name: "Mushroom"
},
new inquirer.Separator("The extras:"),
{
name: "Pineapple"
},
{
name: "Mushroom"
name: "Bacon"
},
{
name: "Bacon"
name: "Extra cheese"
}

@@ -31,0 +36,0 @@ ],

@@ -29,2 +29,3 @@ /**

},
new inquirer.Separator(),
{

@@ -31,0 +32,0 @@ key: "x",

@@ -0,0 +0,0 @@ /**

@@ -8,10 +8,24 @@ /**

inquirer.prompt({
type: "list",
name: "size",
message: "What size do you need",
choices: [ "Jumbo", "Large", "Standard", "Medium", "Small", "Micro" ],
filter: function( val ) { return val.toLowerCase(); }
}, function( answers ) {
console.log( JSON.stringify(answers, null, " ") );
});
inquirer.prompt([
{
type: "list",
name: "theme",
message: "What do you want to do?",
choices: [
"Order a pizza",
"Make a reservation",
new inquirer.Separator(),
"Ask opening hours",
"Talk to the receptionnist"
]
},
{
type: "list",
name: "size",
message: "What size do you need",
choices: [ "Jumbo", "Large", "Standard", "Medium", "Small", "Micro" ],
filter: function( val ) { return val.toLowerCase(); }
}
], function( answers ) {
console.log( JSON.stringify(answers, null, " ") );
});

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /*jshint strict:false */

@@ -17,3 +17,3 @@ /**

var cli = module.exports;
var inquirer = module.exports;

@@ -25,3 +25,3 @@

cli.prompts = {
inquirer.prompts = {
list : require("./prompts/list"),

@@ -36,3 +36,5 @@ input : require("./prompts/input"),

inquirer.Separator = require("./objects/separator");
/**

@@ -45,3 +47,3 @@ * Public CLI helper interface

cli.prompt = function( questions, allDone ) {
inquirer.prompt = function( questions, allDone ) {

@@ -64,6 +66,6 @@ var self = this;

// Propagate keypress events directly on the readline
process.stdin.on( "keypress", this.onKeypress );
process.stdin.on( "keypress", this.onKeypress.bind(this) );
// Make sure new prompt start on a newline when closing
self.rl.on( "SIGINT", this.onForceClose );
self.rl.on( "SIGINT", this.onForceClose.bind(this) );

@@ -130,3 +132,3 @@ // Control flow functions

cli.onKeypress = function( s, key ) {
inquirer.onKeypress = function( s, key ) {

@@ -139,3 +141,3 @@ // Ignore `enter` key (readline `line` event is the only one we care for)

}
}.bind(cli);
};

@@ -148,3 +150,3 @@

cli.onForceClose = function() {
inquirer.onForceClose = function() {
this.rl.output.unmute();

@@ -154,2 +156,2 @@ process.stdout.write("\033[?25h"); // show cursor

console.log("\n"); // Line return
}.bind(cli);
};

@@ -7,6 +7,7 @@ /**

var _ = require("lodash");
var utils = require("../utils/utils");
var clc = require("cli-color");
var ansiTrim = require("cli-color/lib/trim");
var readline = require("readline");
var utils = require("../utils/utils");
var Choices = require("../objects/choices");

@@ -50,3 +51,3 @@

if ( _.isArray(this.opt.choices) ) {
this.opt.choices = utils.normalizeChoices( this.opt.choices );
this.opt.choices = new Choices( this.opt.choices );
}

@@ -274,33 +275,1 @@

};
/**
* Get the pointer char
* @return {String} the pointer char
*/
Prompt.prototype.getPointer = function() {
if ( process.platform === "win32" ) return ">";
if ( process.platform === "linux" ) return "‣";
return "❯";
};
/**
* Get the checkbox
* @param {Boolean} checked - add a X or not to the checkbox
* @param {String} after - Text to append after the check char
* @return {String} - Composited checkbox string
*/
Prompt.prototype.getCheckbox = function( checked, after ) {
var win32 = (process.platform === "win32");
var check = "";
after || (after = "");
if ( checked ) {
check = clc.green( win32 ? "[X]" : "⬢" );
} else {
check = win32 ? "[ ]" : "⬡";
}
return check + " " + after;
};

@@ -9,2 +9,4 @@ /**

var Base = require("./base");
var Separator = require("../objects/separator");
var utils = require("../utils/utils");

@@ -33,2 +35,4 @@

this.opt.choices.setRender( renderChoices );
// Make sure no default is set (so it won't be printed)

@@ -77,3 +81,3 @@ this.opt.default = null;

var message = this.getQuestion();
var choicesStr = this.getChoices();
var choicesStr = "\n" + this.opt.choices.render( this.pointer );

@@ -105,21 +109,2 @@ if ( this.firstRender ) {

/**
* Generate the prompt choices string
* @return {String} Choices string
*/
Prompt.prototype.getChoices = function() {
var output = "";
this.opt.choices.forEach(function( choice, i ) {
output += "\n ";
var isSelected = (i === this.pointer);
output += isSelected ? clc.cyan(this.getPointer()) : " ";
output += this.getCheckbox( choice.checked, choice.name );
}.bind(this));
return output;
};
/**
* When user press `enter` key

@@ -129,3 +114,3 @@ */

Prompt.prototype.onSubmit = function() {
var choices = _.where(this.opt.choices, { checked: true });
var choices = this.opt.choices.where({ checked: true });

@@ -154,4 +139,2 @@ this.selection = _.pluck(choices, "name");

}.bind(this));
};

@@ -165,14 +148,22 @@

Prompt.prototype.onKeypress = function( s, key ) {
// Only process up and down key
if (!key || (key.name !== "up" && key.name !== "down" && key.name !== "space")) return;
// Only process up, down, space and 1-9 key
if ( key && (key.name !== "up" && key.name !== "down" && key.name !== "space") ) return;
if ( key && key.name === "space" ) s = undefined;
if ( s && "123456789".indexOf(s) < 0 ) return;
var len = this.opt.choices.length;
var len = this.opt.choices.realLength;
this.rl.output.unmute();
if ( key.name === "space" ) {
var checked = this.opt.choices[ this.pointer ].checked;
this.opt.choices[ this.pointer ].checked = !checked;
} else if ( key.name === "up" ) {
var shortcut = Number(s);
if ( shortcut <= len && shortcut > 0 ) {
this.pointer = shortcut - 1;
key = { name: "space" };
}
if ( key && key.name === "space" ) {
var checked = this.opt.choices.getChoice(this.pointer).checked;
this.opt.choices.getChoice(this.pointer).checked = !checked;
} else if ( key && key.name === "up" ) {
(this.pointer > 0) ? this.pointer-- : (this.pointer = len - 1);
} else if ( key.name === "down" ) {
} else if ( key && key.name === "down" ) {
(this.pointer < len - 1) ? this.pointer++ : (this.pointer = 0);

@@ -186,1 +177,28 @@ }

};
/**
* Function for rendering checkbox choices
* @param {Number} pointer Position of the pointer
* @return {String} Rendered content
*/
function renderChoices( pointer ) {
var output = "";
var separatorOffset = 0;
this.choices.forEach(function( choice, i ) {
if ( choice instanceof Separator ) {
separatorOffset++;
output += " " + choice + "\n";
return;
}
var isSelected = (i - separatorOffset === pointer);
output += isSelected ? clc.cyan(utils.getPointer()) : " ";
output += utils.getCheckbox( choice.checked, choice.name );
output += "\n";
}.bind(this));
return output.replace(/\n$/, "");
}

@@ -0,0 +0,0 @@ /**

@@ -9,2 +9,3 @@ /**

var Base = require("./base");
var Separator = require("../objects/separator");

@@ -39,8 +40,10 @@

this.opt.choices.setRender( renderChoice );
// Setup the default string (capitalize the default key)
var defIndex = 0;
if ( _.isNumber(this.opt.default) && this.opt.choices[this.opt.default] ) {
if ( _.isNumber(this.opt.default) && this.opt.choices.getChoice(this.opt.default) ) {
defIndex = this.opt.default;
}
var defStr = _.pluck( this.opt.choices, "key" );
var defStr = this.opt.choices.pluck("key");
this.rawDefault = defStr[ defIndex ];

@@ -83,3 +86,3 @@ defStr[ defIndex ] = String( defStr[defIndex] ).toUpperCase();

// Render question
var message = this.getQuestion();
var message = this.getQuestion();

@@ -89,3 +92,3 @@ if ( this.status === "answered" ) {

} else if ( this.status === "expanded" ) {
message += this.getChoices();
message += this.opt.choices.render( this.selectedKey );
message += "\n Answer: ";

@@ -113,3 +116,10 @@ }

this.opt.choices.forEach(function( choice, i ) {
var choiceStr = "\n " + choice.key + ") " + choice.name;
output += "\n ";
if ( choice instanceof Separator ) {
output += " " + choice;
return;
}
var choiceStr = choice.key + ") " + choice.name;
if ( this.selectedKey === choice.key ) {

@@ -134,3 +144,3 @@ choiceStr = clc.cyan( choiceStr );

var selected = _.where( this.opt.choices, { key : input.toLowerCase() })[0];
var selected = this.opt.choices.where({ key : input.toLowerCase() })[0];

@@ -171,3 +181,3 @@ if ( selected != null && selected.key === "h" ) {

this.selectedKey = this.rl.line.toLowerCase();
var selected = _.where( this.opt.choices, { key : this.selectedKey })[0];
var selected = this.opt.choices.where({ key : this.selectedKey })[0];

@@ -196,3 +206,3 @@ if ( this.status === "expanded" ) {

var keymap = {};
this.opt.choices.map(function( choice ) {
this.opt.choices.filter(Separator.exclude).map(function( choice ) {
if ( !choice.key || choice.key.length !== 1 ) {

@@ -219,1 +229,29 @@ formatError = true;

};
/**
* Function for rendering checkbox choices
* @param {String} pointer Selected key
* @return {String} Rendered content
*/
function renderChoice( pointer ) {
var output = "";
this.choices.forEach(function( choice, i ) {
output += "\n ";
if ( choice instanceof Separator ) {
output += " " + choice;
return;
}
var choiceStr = choice.key + ") " + choice.name;
if ( pointer === choice.key ) {
choiceStr = clc.cyan( choiceStr );
}
output += choiceStr;
}.bind(this));
return output;
}

@@ -0,0 +0,0 @@ /**

@@ -9,2 +9,4 @@ /**

var Base = require("./base");
var Separator = require("../objects/separator");
var utils = require("../utils/utils");

@@ -34,6 +36,8 @@

var def = this.opt.default;
if ( _.isNumber(def) && def >= 0 && def < this.opt.choices.length ) {
if ( _.isNumber(def) && def >= 0 && def < this.opt.choices.realLength ) {
this.selected = def;
}
this.opt.choices.setRender( listRender );
// Make sure no default is set (so it won't be printed)

@@ -82,3 +86,3 @@ this.opt.default = null;

var message = this.getQuestion();
var choicesStr = this.getChoices();
var choicesStr = "\n" + this.opt.choices.render( this.selected );

@@ -91,3 +95,3 @@ if ( this.firstRender ) {

if ( this.status === "answered" ) {
message += clc.cyan( this.opt.choices[this.selected].name ) + "\n";
message += clc.cyan( this.opt.choices.getChoice(this.selected).name ) + "\n";
} else {

@@ -111,24 +115,2 @@ message += choicesStr;

/**
* Generate the prompt choices string
* @return {String} Choices string
*/
Prompt.prototype.getChoices = function() {
var output = "";
this.opt.choices.forEach(function( choice, i ) {
output += "\n ";
var isSelected = (i === this.selected);
var line = (isSelected ? this.getPointer() + " " : " ") + choice.name;
if ( isSelected ) {
line = clc.cyan( line );
}
output += line + " ";
}.bind(this));
return output;
};
/**
* When user press `enter` key

@@ -138,3 +120,3 @@ */

Prompt.prototype.onSubmit = function() {
var choice = this.opt.choices[ this.selected ];
var choice = this.opt.choices.getChoice( this.selected );
this.status = "answered";

@@ -158,12 +140,15 @@

Prompt.prototype.onKeypress = function( s, key ) {
// Only process up and down key
if ( !key || (key.name !== "up" && key.name !== "down") ) return;
// Only process up, down and 1-9 key
if ( key && (key.name !== "up" && key.name !== "down") ) return;
if ( s && "123456789".indexOf(s) < 0 ) return;
this.rl.output.unmute();
var len = this.opt.choices.length;
if ( key.name === "up" ) {
var len = this.opt.choices.realLength;
if ( key && key.name === "up" ) {
(this.selected > 0) ? this.selected-- : (this.selected = len - 1);
} else if ( key.name === "down" ) {
} else if ( key && key.name === "down" ) {
(this.selected < len - 1) ? this.selected++ : (this.selected = 0);
} else if ( Number(s) <= len ) {
this.selected = Number(s) - 1;
}

@@ -176,1 +161,30 @@

};
/**
* Function for rendering list choices
* @param {Number} pointer Position of the pointer
* @return {String} Rendered content
*/
function listRender( pointer ) {
var output = "";
var separatorOffset = 0;
this.choices.forEach(function( choice, i ) {
if ( choice instanceof Separator ) {
separatorOffset++;
output += " " + choice + "\n";
return;
}
var isSelected = (i - separatorOffset === pointer);
var line = (isSelected ? utils.getPointer() + " " : " ") + choice.name;
if ( isSelected ) {
line = clc.cyan( line );
}
output += line + " \n";
}.bind(this));
return output.replace(/\n$/, "");
}

@@ -0,0 +0,0 @@ /**

@@ -9,2 +9,3 @@ /**

var Base = require("./base");
var Separator = require("../objects/separator");

@@ -30,7 +31,11 @@

this.opt.validChoices = this.opt.choices.filter(Separator.exclude);
this.selected = 0;
this.rawDefault = 0;
this.opt.choices.setRender( renderChoices );
var def = this.opt.default;
if ( _.isNumber(def) && def >= 0 && def < this.opt.choices.length ) {
if ( _.isNumber(def) && def >= 0 && def < this.opt.choices.realLength ) {
this.selected = this.rawDefault = def;

@@ -73,9 +78,8 @@ }

Prompt.prototype.render = function() {
// Render question
var message = this.getQuestion();
var choicesStr = this.getChoices();
var choicesStr = this.opt.choices.render( this.selected );
if ( this.status === "answered" ) {
message += clc.cyan(this.opt.choices[this.selected].name) + "\n";
message += clc.cyan(this.opt.choices.getChoice(this.selected).name) + "\n";
} else {

@@ -97,22 +101,2 @@ message += choicesStr;

/**
* Generate the prompt choices string
* @return {String} Choices string
*/
Prompt.prototype.getChoices = function() {
var output = "";
this.opt.choices.forEach(function( choice, i ) {
var display = "\n " + (i + 1) + ") " + choice.name;
if ( i === this.selected ) {
display = clc.cyan( display );
}
output += display;
}.bind(this));
return output;
};
/**
* When user press `enter` key

@@ -128,4 +112,6 @@ */

var selectedChoice = this.opt.choices.getChoice(input);
// Input is valid
if ( this.opt.choices[input] != null ) {
if ( selectedChoice != null ) {
this.status = "answered";

@@ -139,3 +125,3 @@ this.selected = input;

this.rl.removeAllListeners("keypress");
this.done( this.opt.choices[this.selected].value );
this.done( selectedChoice.value );
return;

@@ -160,3 +146,3 @@ }

if ( this.opt.choices[index] ) {
if ( this.opt.choices.getChoice(index) ) {
this.selected = index;

@@ -169,1 +155,32 @@ } else {

};
/**
* Function for rendering list choices
* @param {Number} pointer Position of the pointer
* @return {String} Rendered content
*/
function renderChoices( pointer ) {
var output = "";
var separatorOffset = 0;
this.choices.forEach(function( choice, i ) {
output += "\n ";
if ( choice instanceof Separator ) {
separatorOffset++;
output += " " + choice;
return;
}
var index = i - separatorOffset;
var display = (index + 1) + ") " + choice.name;
if ( index === pointer ) {
display = clc.cyan( display );
}
output += display;
}.bind(this));
return output;
}

@@ -0,0 +0,0 @@ /**

@@ -7,2 +7,5 @@ /**

var _ = require("lodash");
var clc = require("cli-color");
var Separator = require("../objects/separator");
var Choice = require("../objects/choice");

@@ -18,22 +21,2 @@

/**
* Normalize choices array
* @param {Array} choices The avaiable prompt choices
* @return {Array} Normalized choices containing `name`/`value` hash
*/
utils.normalizeChoices = function normalizeChoices( choices ) {
return _.map( choices, function( val ) {
if ( _.isString(val) ) {
return { name : val, value: val };
}
return _.extend(val, {
name: val.name || val.value,
value: val.value || val.name
});
});
};
/**
* Run a function asynchronously or synchronously

@@ -66,1 +49,33 @@ * @param {Function} func Function to run

};
/**
* Get the pointer char
* @return {String} the pointer char
*/
utils.getPointer = function() {
if ( process.platform === "win32" ) return ">";
if ( process.platform === "linux" ) return "‣";
return "❯";
};
/**
* Get the checkbox
* @param {Boolean} checked - add a X or not to the checkbox
* @param {String} after - Text to append after the check char
* @return {String} - Composited checkbox string
*/
utils.getCheckbox = function( checked, after ) {
var win32 = (process.platform === "win32");
var check = "";
after || (after = "");
if ( checked ) {
check = clc.green( win32 ? "[X]" : "⬢" );
} else {
check = win32 ? "[ ]" : "⬡";
}
return check + " " + after;
};
{
"name": "inquirer",
"version": "0.2.5",
"version": "0.3.0",
"description": "A collection of common interactive command line user interfaces.",

@@ -5,0 +5,0 @@ "main": "lib/inquirer.js",

@@ -71,3 +71,3 @@ Inquirer.js [![Build Status](https://travis-ci.org/SBoudrias/Inquirer.js.png?branch=master)](http://travis-ci.org/SBoudrias/Inquirer.js)

+ **choices**: (Array|Function) Choices array or a function returning a choices array. If defined as a function, the first parameter will be the current inquirer session answers.
Array values can be simple `strings`, or `objects` containing a `name` (to display) and a `value` properties (to save in the answers hash).
Array values can be simple `strings`, or `objects` containing a `name` (to display) and a `value` properties (to save in the answers hash). Values can also be [a `Separator`](#separator).
+ **validate**: (Function) Receive the user input and should return `true` if the value is valid, and an error message (`String`) otherwise. If `false` is returned, a default error message is provided.

@@ -109,3 +109,20 @@ + **filter**: (Function) Receive the user input and return the filtered value to be used inside the program. The value returned will be added to the _Answers_ hash.

### Separator
A separator can be added to any `choices` array:
```
// In the question object
choices: [ "Choice A", new inquirer.Separator(), "choice B" ]
// Which'll be displayed this way
[?] What do you want to do?
> Order a pizza
Make a reservation
--------
Ask opening hours
Talk to the receptionnist
```
The constructor takes a facultative `String` value that'll be use as the separator. If omitted, the separator will be `--------`.
Prompts type

@@ -112,0 +129,0 @@ ---------------------

@@ -0,1 +1,3 @@

var inquirer = require("../../lib/inquirer");
module.exports = {

@@ -21,3 +23,3 @@

name: "name",
choices: [ "foo", "bar", "bum" ]
choices: [ "foo", new inquirer.Separator(), "bar", "bum" ]
},

@@ -28,3 +30,3 @@

name: "name",
choices: [ "foo", "bar", "bum" ]
choices: [ "foo", "bar", new inquirer.Separator(), "bum" ]
},

@@ -37,2 +39,3 @@

{ key: "a", name: "acab" },
new inquirer.Separator(),
{ key: "b", name: "bar" },

@@ -48,2 +51,3 @@ { key: "c", name: "chile" }

"choice 1",
new inquirer.Separator(),
"choice 2",

@@ -50,0 +54,0 @@ "choice 3"

@@ -0,0 +0,0 @@ var EventEmitter = require("events").EventEmitter;

@@ -282,3 +282,3 @@ /**

_.each( choices, function( choice ) {
_.each( choices.filter(inquirer.Separator.exclude), function( choice ) {
expect( this.output ).to.contain( choice.name );

@@ -285,0 +285,0 @@ }, this );

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ var expect = require("chai").expect;

@@ -84,2 +84,14 @@ var expect = require("chai").expect;

it("should allow 1-9 shortcut key", function( done ) {
this.checkbox.run(function( answer ) {
expect(answer.length).to.equal(1);
expect(answer[0]).to.equal("choice 2");
done();
});
this.rl.emit("keypress", "2");
this.rl.emit("line");
});
});

@@ -0,0 +0,0 @@ var expect = require("chai").expect;

@@ -0,0 +0,0 @@ var expect = require("chai").expect;

@@ -0,0 +0,0 @@ var expect = require("chai").expect;

@@ -135,2 +135,13 @@ var expect = require("chai").expect;

it("should allow 1-9 shortcut key", function( done ) {
this.list.run(function( answer ) {
expect(answer).to.equal("bar");
done();
});
this.rl.emit("keypress", "2");
this.rl.emit("line");
});
});

@@ -0,0 +0,0 @@ var expect = require("chai").expect;

@@ -0,0 +0,0 @@ var expect = require("chai").expect;

var expect = require("chai").expect;
var sinon = require("sinon");
var utils = require("../../lib/utils/utils");
describe("normalizeChoices", function() {
it("should normalize array containing strings", function() {
var initial = [ "foo", "bar" ];
var normalized = utils.normalizeChoices(initial);
expect(normalized).to.eql([{
name: "foo",
value: "foo"
}, {
name: "bar",
value: "bar"
}]);
});
it("should keep extra keys", function() {
var initial = [{ name: "foo", extra: "1" }, { name: "bar", key: "z" }];
var normalized = utils.normalizeChoices(initial);
expect(normalized).to.eql([{
name: "foo",
value: "foo",
extra: "1"
}, {
name: "bar",
value: "bar",
key: "z"
}]);
});
});
describe("runAsync", function() {

@@ -35,0 +7,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc