Blame view
node_modules/inquirer/lib/prompts/list.js
3.75 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
/** * `list` type prompt */ var _ = require("lodash"); var util = require("util"); var chalk = require("chalk"); var figures = require("figures"); var cliCursor = require("cli-cursor"); var Base = require("./base"); var observe = require("../utils/events"); var Paginator = require("../utils/paginator"); /** * Module exports */ module.exports = Prompt; /** * Constructor */ function Prompt() { Base.apply( this, arguments ); if (!this.opt.choices) { this.throwParamError("choices"); } this.firstRender = true; this.selected = 0; var def = this.opt.default; // Default being a Number if ( _.isNumber(def) && def >= 0 && def < this.opt.choices.realLength ) { this.selected = def; } // Default being a String if ( _.isString(def) ) { this.selected = this.opt.choices.pluck("value").indexOf( def ); } // Make sure no default is set (so it won't be printed) this.opt.default = null; this.paginator = new Paginator(); } util.inherits( Prompt, Base ); /** * Start the Inquiry session * @param {Function} cb Callback when prompt is done * @return {this} */ Prompt.prototype._run = function( cb ) { this.done = cb; var events = observe(this.rl); events.normalizedUpKey.takeUntil( events.line ).forEach( this.onUpKey.bind(this) ); events.normalizedDownKey.takeUntil( events.line ).forEach( this.onDownKey.bind(this) ); events.numberKey.takeUntil( events.line ).forEach( this.onNumberKey.bind(this) ); events.line.take(1).forEach( this.onSubmit.bind(this) ); // Init the prompt cliCursor.hide(); this.render(); return this; }; /** * Render the prompt to screen * @return {Prompt} self */ Prompt.prototype.render = function() { // Render question var message = this.getQuestion(); if ( this.firstRender ) { message += chalk.dim( "(Use arrow keys)" ); } // Render choices or answer depending on the state if ( this.status === "answered" ) { message += chalk.cyan( this.opt.choices.getChoice(this.selected).short ); } else { var choicesStr = listRender(this.opt.choices, this.selected ); var indexPosition = this.opt.choices.indexOf(this.opt.choices.getChoice(this.selected)); message += " " + this.paginator.paginate(choicesStr, indexPosition, this.opt.pageSize); } this.firstRender = false; this.screen.render(message); }; /** * When user press `enter` key */ Prompt.prototype.onSubmit = function() { var choice = this.opt.choices.getChoice( this.selected ); this.status = "answered"; // Rerender prompt this.render(); this.screen.done(); cliCursor.show(); this.done( choice.value ); }; /** * When user press a key */ Prompt.prototype.onUpKey = function() { var len = this.opt.choices.realLength; this.selected = (this.selected > 0) ? this.selected - 1 : len - 1; this.render(); }; Prompt.prototype.onDownKey = function() { var len = this.opt.choices.realLength; this.selected = (this.selected < len - 1) ? this.selected + 1 : 0; this.render(); }; Prompt.prototype.onNumberKey = function( input ) { if ( input <= this.opt.choices.realLength ) { this.selected = input - 1; } this.render(); }; /** * Function for rendering list choices * @param {Number} pointer Position of the pointer * @return {String} Rendered content */ function listRender(choices, pointer) { var output = ''; var separatorOffset = 0; choices.forEach(function (choice, i) { if (choice.type === 'separator') { separatorOffset++; output += ' ' + choice + ' '; return; } var isSelected = (i - separatorOffset === pointer); var line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name; if (isSelected) { line = chalk.cyan(line); } output += line + ' '; }); return output.replace(/ $/, ''); } |