prompt.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. 'use strict';
  2. const Events = require('events');
  3. const stripAnsi = require('strip-ansi');
  4. const keypress = require('./keypress');
  5. const timer = require('./timer');
  6. const State = require('./state');
  7. const theme = require('./theme');
  8. const utils = require('./utils');
  9. const ansi = require('./ansi');
  10. /**
  11. * Base class for creating a new Prompt.
  12. * @param {Object} `options` Question object.
  13. */
  14. class Prompt extends Events {
  15. constructor(options = {}) {
  16. super();
  17. this.name = options.name;
  18. this.type = options.type;
  19. this.options = options;
  20. theme(this);
  21. timer(this);
  22. this.state = new State(this);
  23. this.initial = [options.initial, options.default].find(v => v != null);
  24. this.stdout = options.stdout || process.stdout;
  25. this.stdin = options.stdin || process.stdin;
  26. this.scale = options.scale || 1;
  27. this.term = this.options.term || process.env.TERM_PROGRAM;
  28. this.margin = margin(this.options.margin);
  29. this.setMaxListeners(0);
  30. setOptions(this);
  31. }
  32. async keypress(input, event = {}) {
  33. this.keypressed = true;
  34. let key = keypress.action(input, keypress(input, event), this.options.actions);
  35. this.state.keypress = key;
  36. this.emit('keypress', input, key);
  37. this.emit('state', this.state.clone());
  38. const fn = this.options[key.action] || this[key.action] || this.dispatch;
  39. if (typeof fn === 'function') {
  40. return await fn.call(this, input, key);
  41. }
  42. this.alert();
  43. }
  44. alert() {
  45. delete this.state.alert;
  46. if (this.options.show === false) {
  47. this.emit('alert');
  48. } else {
  49. this.stdout.write(ansi.code.beep);
  50. }
  51. }
  52. cursorHide() {
  53. this.stdout.write(ansi.cursor.hide());
  54. const releaseOnExit = utils.onExit(() => this.cursorShow());
  55. this.on('close', () => {
  56. this.cursorShow();
  57. releaseOnExit();
  58. });
  59. }
  60. cursorShow() {
  61. this.stdout.write(ansi.cursor.show());
  62. }
  63. write(str) {
  64. if (!str) return;
  65. if (this.stdout && this.state.show !== false) {
  66. this.stdout.write(str);
  67. }
  68. this.state.buffer += str;
  69. }
  70. clear(lines = 0) {
  71. let buffer = this.state.buffer;
  72. this.state.buffer = '';
  73. if ((!buffer && !lines) || this.options.show === false) return;
  74. this.stdout.write(ansi.cursor.down(lines) + ansi.clear(buffer, this.width));
  75. }
  76. restore() {
  77. if (this.state.closed || this.options.show === false) return;
  78. let { prompt, after, rest } = this.sections();
  79. let { cursor, initial = '', input = '', value = '' } = this;
  80. let size = this.state.size = rest.length;
  81. let state = { after, cursor, initial, input, prompt, size, value };
  82. let codes = ansi.cursor.restore(state);
  83. if (codes) {
  84. this.stdout.write(codes);
  85. }
  86. }
  87. sections() {
  88. let { buffer, input, prompt } = this.state;
  89. prompt = stripAnsi(prompt);
  90. let buf = stripAnsi(buffer);
  91. let idx = buf.indexOf(prompt);
  92. let header = buf.slice(0, idx);
  93. let rest = buf.slice(idx);
  94. let lines = rest.split('\n');
  95. let first = lines[0];
  96. let last = lines[lines.length - 1];
  97. let promptLine = prompt + (input ? ' ' + input : '');
  98. let len = promptLine.length;
  99. let after = len < first.length ? first.slice(len + 1) : '';
  100. return { header, prompt: first, after, rest: lines.slice(1), last };
  101. }
  102. async submit() {
  103. this.state.submitted = true;
  104. this.state.validating = true;
  105. // this will only be called when the prompt is directly submitted
  106. // without initializing, i.e. when the prompt is skipped, etc. Otherwize,
  107. // "options.onSubmit" is will be handled by the "initialize()" method.
  108. if (this.options.onSubmit) {
  109. await this.options.onSubmit.call(this, this.name, this.value, this);
  110. }
  111. let result = this.state.error || await this.validate(this.value, this.state);
  112. if (result !== true) {
  113. let error = '\n' + this.symbols.pointer + ' ';
  114. if (typeof result === 'string') {
  115. error += result.trim();
  116. } else {
  117. error += 'Invalid input';
  118. }
  119. this.state.error = '\n' + this.styles.danger(error);
  120. this.state.submitted = false;
  121. await this.render();
  122. await this.alert();
  123. this.state.validating = false;
  124. this.state.error = void 0;
  125. return;
  126. }
  127. this.state.validating = false;
  128. await this.render();
  129. await this.close();
  130. this.value = await this.result(this.value);
  131. this.emit('submit', this.value);
  132. }
  133. async cancel(err) {
  134. this.state.cancelled = this.state.submitted = true;
  135. await this.render();
  136. await this.close();
  137. if (typeof this.options.onCancel === 'function') {
  138. await this.options.onCancel.call(this, this.name, this.value, this);
  139. }
  140. this.emit('cancel', await this.error(err));
  141. }
  142. async close() {
  143. this.state.closed = true;
  144. try {
  145. let sections = this.sections();
  146. let lines = Math.ceil(sections.prompt.length / this.width);
  147. if (sections.rest) {
  148. this.write(ansi.cursor.down(sections.rest.length));
  149. }
  150. this.write('\n'.repeat(lines));
  151. } catch (err) { /* do nothing */ }
  152. this.emit('close');
  153. }
  154. start() {
  155. if (!this.stop && this.options.show !== false) {
  156. this.stop = keypress.listen(this, this.keypress.bind(this));
  157. this.once('close', this.stop);
  158. this.emit('start', this);
  159. }
  160. }
  161. async skip() {
  162. this.skipped = this.options.skip === true;
  163. if (typeof this.options.skip === 'function') {
  164. this.skipped = await this.options.skip.call(this, this.name, this.value);
  165. }
  166. return this.skipped;
  167. }
  168. async initialize() {
  169. let { format, options, result } = this;
  170. this.format = () => format.call(this, this.value);
  171. this.result = () => result.call(this, this.value);
  172. if (typeof options.initial === 'function') {
  173. this.initial = await options.initial.call(this, this);
  174. }
  175. if (typeof options.onRun === 'function') {
  176. await options.onRun.call(this, this);
  177. }
  178. // if "options.onSubmit" is defined, we wrap the "submit" method to guarantee
  179. // that "onSubmit" will always called first thing inside the submit
  180. // method, regardless of how it's handled in inheriting prompts.
  181. if (typeof options.onSubmit === 'function') {
  182. let onSubmit = options.onSubmit.bind(this);
  183. let submit = this.submit.bind(this);
  184. delete this.options.onSubmit;
  185. this.submit = async() => {
  186. await onSubmit(this.name, this.value, this);
  187. return submit();
  188. };
  189. }
  190. await this.start();
  191. await this.render();
  192. }
  193. render() {
  194. throw new Error('expected prompt to have a custom render method');
  195. }
  196. run() {
  197. return new Promise(async(resolve, reject) => {
  198. this.once('submit', resolve);
  199. this.once('cancel', reject);
  200. if (await this.skip()) {
  201. this.render = () => {};
  202. return this.submit();
  203. }
  204. await this.initialize();
  205. this.emit('run');
  206. });
  207. }
  208. async element(name, choice, i) {
  209. let { options, state, symbols, timers } = this;
  210. let timer = timers && timers[name];
  211. state.timer = timer;
  212. let value = options[name] || state[name] || symbols[name];
  213. let val = choice && choice[name] != null ? choice[name] : await value;
  214. if (val === '') return val;
  215. let res = await this.resolve(val, state, choice, i);
  216. if (!res && choice && choice[name]) {
  217. return this.resolve(value, state, choice, i);
  218. }
  219. return res;
  220. }
  221. async prefix() {
  222. let element = await this.element('prefix') || this.symbols;
  223. let timer = this.timers && this.timers.prefix;
  224. let state = this.state;
  225. state.timer = timer;
  226. if (utils.isObject(element)) element = element[state.status] || element.pending;
  227. if (!utils.hasColor(element)) {
  228. let style = this.styles[state.status] || this.styles.pending;
  229. return style(element);
  230. }
  231. return element;
  232. }
  233. async message() {
  234. let message = await this.element('message');
  235. if (!utils.hasColor(message)) {
  236. return this.styles.strong(message);
  237. }
  238. return message;
  239. }
  240. async separator() {
  241. let element = await this.element('separator') || this.symbols;
  242. let timer = this.timers && this.timers.separator;
  243. let state = this.state;
  244. state.timer = timer;
  245. let value = element[state.status] || element.pending || state.separator;
  246. let ele = await this.resolve(value, state);
  247. if (utils.isObject(ele)) ele = ele[state.status] || ele.pending;
  248. if (!utils.hasColor(ele)) {
  249. return this.styles.muted(ele);
  250. }
  251. return ele;
  252. }
  253. async pointer(choice, i) {
  254. let val = await this.element('pointer', choice, i);
  255. if (typeof val === 'string' && utils.hasColor(val)) {
  256. return val;
  257. }
  258. if (val) {
  259. let styles = this.styles;
  260. let focused = this.index === i;
  261. let style = focused ? styles.primary : val => val;
  262. let ele = await this.resolve(val[focused ? 'on' : 'off'] || val, this.state);
  263. let styled = !utils.hasColor(ele) ? style(ele) : ele;
  264. return focused ? styled : ' '.repeat(ele.length);
  265. }
  266. }
  267. async indicator(choice, i) {
  268. let val = await this.element('indicator', choice, i);
  269. if (typeof val === 'string' && utils.hasColor(val)) {
  270. return val;
  271. }
  272. if (val) {
  273. let styles = this.styles;
  274. let enabled = choice.enabled === true;
  275. let style = enabled ? styles.success : styles.dark;
  276. let ele = val[enabled ? 'on' : 'off'] || val;
  277. return !utils.hasColor(ele) ? style(ele) : ele;
  278. }
  279. return '';
  280. }
  281. body() {
  282. return null;
  283. }
  284. footer() {
  285. if (this.state.status === 'pending') {
  286. return this.element('footer');
  287. }
  288. }
  289. header() {
  290. if (this.state.status === 'pending') {
  291. return this.element('header');
  292. }
  293. }
  294. async hint() {
  295. if (this.state.status === 'pending' && !this.isValue(this.state.input)) {
  296. let hint = await this.element('hint');
  297. if (!utils.hasColor(hint)) {
  298. return this.styles.muted(hint);
  299. }
  300. return hint;
  301. }
  302. }
  303. error(err) {
  304. return !this.state.submitted ? (err || this.state.error) : '';
  305. }
  306. format(value) {
  307. return value;
  308. }
  309. result(value) {
  310. return value;
  311. }
  312. validate(value) {
  313. if (this.options.required === true) {
  314. return this.isValue(value);
  315. }
  316. return true;
  317. }
  318. isValue(value) {
  319. return value != null && value !== '';
  320. }
  321. resolve(value, ...args) {
  322. return utils.resolve(this, value, ...args);
  323. }
  324. get base() {
  325. return Prompt.prototype;
  326. }
  327. get style() {
  328. return this.styles[this.state.status];
  329. }
  330. get height() {
  331. return this.options.rows || utils.height(this.stdout, 25);
  332. }
  333. get width() {
  334. return this.options.columns || utils.width(this.stdout, 80);
  335. }
  336. get size() {
  337. return { width: this.width, height: this.height };
  338. }
  339. set cursor(value) {
  340. this.state.cursor = value;
  341. }
  342. get cursor() {
  343. return this.state.cursor;
  344. }
  345. set input(value) {
  346. this.state.input = value;
  347. }
  348. get input() {
  349. return this.state.input;
  350. }
  351. set value(value) {
  352. this.state.value = value;
  353. }
  354. get value() {
  355. let { input, value } = this.state;
  356. let result = [value, input].find(this.isValue.bind(this));
  357. return this.isValue(result) ? result : this.initial;
  358. }
  359. static get prompt() {
  360. return options => new this(options).run();
  361. }
  362. }
  363. function setOptions(prompt) {
  364. let isValidKey = key => {
  365. return prompt[key] === void 0 || typeof prompt[key] === 'function';
  366. };
  367. let ignore = [
  368. 'actions',
  369. 'choices',
  370. 'initial',
  371. 'margin',
  372. 'roles',
  373. 'styles',
  374. 'symbols',
  375. 'theme',
  376. 'timers',
  377. 'value'
  378. ];
  379. let ignoreFn = [
  380. 'body',
  381. 'footer',
  382. 'error',
  383. 'header',
  384. 'hint',
  385. 'indicator',
  386. 'message',
  387. 'prefix',
  388. 'separator',
  389. 'skip'
  390. ];
  391. for (let key of Object.keys(prompt.options)) {
  392. if (ignore.includes(key)) continue;
  393. if (/^on[A-Z]/.test(key)) continue;
  394. let option = prompt.options[key];
  395. if (typeof option === 'function' && isValidKey(key)) {
  396. if (!ignoreFn.includes(key)) {
  397. prompt[key] = option.bind(prompt);
  398. }
  399. } else if (typeof prompt[key] !== 'function') {
  400. prompt[key] = option;
  401. }
  402. }
  403. }
  404. function margin(value) {
  405. if (typeof value === 'number') {
  406. value = [value, value, value, value];
  407. }
  408. let arr = [].concat(value || []);
  409. let pad = i => i % 2 === 0 ? '\n' : ' ';
  410. let res = [];
  411. for (let i = 0; i < 4; i++) {
  412. let char = pad(i);
  413. if (arr[i]) {
  414. res.push(char.repeat(arr[i]));
  415. } else {
  416. res.push('');
  417. }
  418. }
  419. return res;
  420. }
  421. module.exports = Prompt;