expand.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * `rawlist` type prompt
  3. */
  4. var _ = require("lodash");
  5. var util = require("util");
  6. var chalk = require("chalk");
  7. var Base = require("./base");
  8. var Separator = require("../objects/separator");
  9. var observe = require("../utils/events");
  10. var Paginator = require("../utils/paginator");
  11. /**
  12. * Module exports
  13. */
  14. module.exports = Prompt;
  15. /**
  16. * Constructor
  17. */
  18. function Prompt() {
  19. Base.apply( this, arguments );
  20. if ( !this.opt.choices ) {
  21. this.throwParamError("choices");
  22. }
  23. this.validateChoices( this.opt.choices );
  24. // Add the default `help` (/expand) option
  25. this.opt.choices.push({
  26. key : "h",
  27. name : "Help, list all options",
  28. value : "help"
  29. });
  30. // Setup the default string (capitalize the default key)
  31. this.opt.default = this.generateChoicesString( this.opt.choices, this.opt.default );
  32. this.paginator = new Paginator();
  33. }
  34. util.inherits( Prompt, Base );
  35. /**
  36. * Start the Inquiry session
  37. * @param {Function} cb Callback when prompt is done
  38. * @return {this}
  39. */
  40. Prompt.prototype._run = function( cb ) {
  41. this.done = cb;
  42. // Save user answer and update prompt to show selected option.
  43. var events = observe(this.rl);
  44. this.lineObs = events.line.forEach( this.onSubmit.bind(this) );
  45. this.keypressObs = events.keypress.forEach( this.onKeypress.bind(this) );
  46. // Init the prompt
  47. this.render();
  48. return this;
  49. };
  50. /**
  51. * Render the prompt to screen
  52. * @return {Prompt} self
  53. */
  54. Prompt.prototype.render = function (error, hint) {
  55. var cursor = 0;
  56. var message = this.getQuestion();
  57. if ( this.status === "answered" ) {
  58. message += chalk.cyan( this.selected.name );
  59. } else if ( this.status === "expanded" ) {
  60. var choicesStr = renderChoices(this.opt.choices, this.selectedKey);
  61. message += this.paginator.paginate(choicesStr, this.selectedKey);
  62. message += "\n Answer: ";
  63. }
  64. message += this.rl.line;
  65. if (error) {
  66. message += '\n' + chalk.red('>> ') + error;
  67. cursor++;
  68. }
  69. if (hint) {
  70. message += '\n' + chalk.cyan('>> ') + hint;
  71. cursor++;
  72. }
  73. this.screen.render(message, { cursor: cursor });
  74. };
  75. /**
  76. * Generate the prompt choices string
  77. * @return {String} Choices string
  78. */
  79. Prompt.prototype.getChoices = function() {
  80. var output = "";
  81. this.opt.choices.forEach(function( choice, i ) {
  82. output += "\n ";
  83. if ( choice.type === "separator" ) {
  84. output += " " + choice;
  85. return;
  86. }
  87. var choiceStr = choice.key + ") " + choice.name;
  88. if ( this.selectedKey === choice.key ) {
  89. choiceStr = chalk.cyan( choiceStr );
  90. }
  91. output += choiceStr;
  92. }.bind(this));
  93. return output;
  94. };
  95. /**
  96. * When user press `enter` key
  97. */
  98. Prompt.prototype.onSubmit = function( input ) {
  99. if ( input == null || input === "" ) {
  100. input = this.rawDefault;
  101. }
  102. var selected = this.opt.choices.where({ key : input.toLowerCase() })[0];
  103. if ( selected != null && selected.key === "h" ) {
  104. this.selectedKey = "";
  105. this.status = "expanded";
  106. this.render();
  107. return;
  108. }
  109. if ( selected != null ) {
  110. this.status = "answered";
  111. this.selected = selected;
  112. // Re-render prompt
  113. this.render();
  114. this.lineObs.dispose();
  115. this.keypressObs.dispose();
  116. this.screen.done();
  117. this.done( this.selected.value );
  118. return;
  119. }
  120. // Input is invalid
  121. this.render("Please enter a valid command");
  122. };
  123. /**
  124. * When user press a key
  125. */
  126. Prompt.prototype.onKeypress = function( s, key ) {
  127. this.selectedKey = this.rl.line.toLowerCase();
  128. var selected = this.opt.choices.where({ key : this.selectedKey })[0];
  129. if ( this.status === "expanded" ) {
  130. this.render();
  131. } else {
  132. this.render(null, selected ? selected.name : null);
  133. }
  134. };
  135. /**
  136. * Validate the choices
  137. * @param {Array} choices
  138. */
  139. Prompt.prototype.validateChoices = function( choices ) {
  140. var formatError;
  141. var errors = [];
  142. var keymap = {};
  143. choices.filter(Separator.exclude).map(function( choice ) {
  144. if ( !choice.key || choice.key.length !== 1 ) {
  145. formatError = true;
  146. }
  147. if ( keymap[choice.key] ) {
  148. errors.push(choice.key);
  149. }
  150. keymap[ choice.key ] = true;
  151. choice.key = String( choice.key ).toLowerCase();
  152. });
  153. if ( formatError ) {
  154. throw new Error("Format error: `key` param must be a single letter and is required.");
  155. }
  156. if ( keymap.h ) {
  157. throw new Error("Reserved key error: `key` param cannot be `h` - this value is reserved.");
  158. }
  159. if ( errors.length ) {
  160. throw new Error( "Duplicate key error: `key` param must be unique. Duplicates: " +
  161. _.uniq(errors).join(", ") );
  162. }
  163. };
  164. /**
  165. * Generate a string out of the choices keys
  166. * @param {Array} choices
  167. * @param {Number} defaultIndex - the choice index to capitalize
  168. * @return {String} The rendered choices key string
  169. */
  170. Prompt.prototype.generateChoicesString = function( choices, defaultIndex ) {
  171. var defIndex = 0;
  172. if ( _.isNumber(defaultIndex) && this.opt.choices.getChoice(defaultIndex) ) {
  173. defIndex = defaultIndex;
  174. }
  175. var defStr = this.opt.choices.pluck("key");
  176. this.rawDefault = defStr[ defIndex ];
  177. defStr[ defIndex ] = String( defStr[defIndex] ).toUpperCase();
  178. return defStr.join("");
  179. };
  180. /**
  181. * Function for rendering checkbox choices
  182. * @param {String} pointer Selected key
  183. * @return {String} Rendered content
  184. */
  185. function renderChoices (choices, pointer) {
  186. var output = '';
  187. choices.forEach(function (choice, i) {
  188. output += '\n ';
  189. if (choice.type === 'separator') {
  190. output += ' ' + choice;
  191. return;
  192. }
  193. var choiceStr = choice.key + ') ' + choice.name;
  194. if (pointer === choice.key) {
  195. choiceStr = chalk.cyan(choiceStr);
  196. }
  197. output += choiceStr;
  198. });
  199. return output;
  200. }