number.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const color = require('kleur');
  2. const Prompt = require('./prompt');
  3. const { cursor, erase } = require('sisteransi');
  4. const { style, figures, clear, lines } = require('../util');
  5. const isNumber = /[0-9]/;
  6. const isDef = any => any !== undefined;
  7. const round = (number, precision) => {
  8. let factor = Math.pow(10, precision);
  9. return Math.round(number * factor) / factor;
  10. }
  11. /**
  12. * NumberPrompt Base Element
  13. * @param {Object} opts Options
  14. * @param {String} opts.message Message
  15. * @param {String} [opts.style='default'] Render style
  16. * @param {Number} [opts.initial] Default value
  17. * @param {Number} [opts.max=+Infinity] Max value
  18. * @param {Number} [opts.min=-Infinity] Min value
  19. * @param {Boolean} [opts.float=false] Parse input as floats
  20. * @param {Number} [opts.round=2] Round floats to x decimals
  21. * @param {Number} [opts.increment=1] Number to increment by when using arrow-keys
  22. * @param {Function} [opts.validate] Validate function
  23. * @param {Stream} [opts.stdin] The Readable stream to listen to
  24. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  25. * @param {String} [opts.error] The invalid error label
  26. */
  27. class NumberPrompt extends Prompt {
  28. constructor(opts={}) {
  29. super(opts);
  30. this.transform = style.render(opts.style);
  31. this.msg = opts.message;
  32. this.initial = isDef(opts.initial) ? opts.initial : '';
  33. this.float = !!opts.float;
  34. this.round = opts.round || 2;
  35. this.inc = opts.increment || 1;
  36. this.min = isDef(opts.min) ? opts.min : -Infinity;
  37. this.max = isDef(opts.max) ? opts.max : Infinity;
  38. this.errorMsg = opts.error || `Please Enter A Valid Value`;
  39. this.validator = opts.validate || (() => true);
  40. this.color = `cyan`;
  41. this.value = ``;
  42. this.typed = ``;
  43. this.lastHit = 0;
  44. this.render();
  45. }
  46. set value(v) {
  47. if (!v && v !== 0) {
  48. this.placeholder = true;
  49. this.rendered = color.gray(this.transform.render(`${this.initial}`));
  50. this._value = ``;
  51. } else {
  52. this.placeholder = false;
  53. this.rendered = this.transform.render(`${round(v, this.round)}`);
  54. this._value = round(v, this.round);
  55. }
  56. this.fire();
  57. }
  58. get value() {
  59. return this._value;
  60. }
  61. parse(x) {
  62. return this.float ? parseFloat(x) : parseInt(x);
  63. }
  64. valid(c) {
  65. return c === `-` || c === `.` && this.float || isNumber.test(c)
  66. }
  67. reset() {
  68. this.typed = ``;
  69. this.value = ``;
  70. this.fire();
  71. this.render();
  72. }
  73. exit() {
  74. this.abort();
  75. }
  76. abort() {
  77. let x = this.value;
  78. this.value = x !== `` ? x : this.initial;
  79. this.done = this.aborted = true;
  80. this.error = false;
  81. this.fire();
  82. this.render();
  83. this.out.write(`\n`);
  84. this.close();
  85. }
  86. async validate() {
  87. let valid = await this.validator(this.value);
  88. if (typeof valid === `string`) {
  89. this.errorMsg = valid;
  90. valid = false;
  91. }
  92. this.error = !valid;
  93. }
  94. async submit() {
  95. await this.validate();
  96. if (this.error) {
  97. this.color = `red`;
  98. this.fire();
  99. this.render();
  100. return;
  101. }
  102. let x = this.value;
  103. this.value = x !== `` ? x : this.initial;
  104. this.done = true;
  105. this.aborted = false;
  106. this.error = false;
  107. this.fire();
  108. this.render();
  109. this.out.write(`\n`);
  110. this.close();
  111. }
  112. up() {
  113. this.typed = ``;
  114. if(this.value === '') {
  115. this.value = this.min - this.inc;
  116. }
  117. if (this.value >= this.max) return this.bell();
  118. this.value += this.inc;
  119. this.color = `cyan`;
  120. this.fire();
  121. this.render();
  122. }
  123. down() {
  124. this.typed = ``;
  125. if(this.value === '') {
  126. this.value = this.min + this.inc;
  127. }
  128. if (this.value <= this.min) return this.bell();
  129. this.value -= this.inc;
  130. this.color = `cyan`;
  131. this.fire();
  132. this.render();
  133. }
  134. delete() {
  135. let val = this.value.toString();
  136. if (val.length === 0) return this.bell();
  137. this.value = this.parse((val = val.slice(0, -1))) || ``;
  138. if (this.value !== '' && this.value < this.min) {
  139. this.value = this.min;
  140. }
  141. this.color = `cyan`;
  142. this.fire();
  143. this.render();
  144. }
  145. next() {
  146. this.value = this.initial;
  147. this.fire();
  148. this.render();
  149. }
  150. _(c, key) {
  151. if (!this.valid(c)) return this.bell();
  152. const now = Date.now();
  153. if (now - this.lastHit > 1000) this.typed = ``; // 1s elapsed
  154. this.typed += c;
  155. this.lastHit = now;
  156. this.color = `cyan`;
  157. if (c === `.`) return this.fire();
  158. this.value = Math.min(this.parse(this.typed), this.max);
  159. if (this.value > this.max) this.value = this.max;
  160. if (this.value < this.min) this.value = this.min;
  161. this.fire();
  162. this.render();
  163. }
  164. render() {
  165. if (this.closed) return;
  166. if (!this.firstRender) {
  167. if (this.outputError)
  168. this.out.write(cursor.down(lines(this.outputError, this.out.columns) - 1) + clear(this.outputError, this.out.columns));
  169. this.out.write(clear(this.outputText, this.out.columns));
  170. }
  171. super.render();
  172. this.outputError = '';
  173. // Print prompt
  174. this.outputText = [
  175. style.symbol(this.done, this.aborted),
  176. color.bold(this.msg),
  177. style.delimiter(this.done),
  178. !this.done || (!this.done && !this.placeholder)
  179. ? color[this.color]().underline(this.rendered) : this.rendered
  180. ].join(` `);
  181. // Print error
  182. if (this.error) {
  183. this.outputError += this.errorMsg.split(`\n`)
  184. .reduce((a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);
  185. }
  186. this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore);
  187. }
  188. }
  189. module.exports = NumberPrompt;