PageRenderTime 26ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/bower/node_modules/inquirer/lib/prompts/expand.js

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