number.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. const color = require('kleur');
  3. const Prompt = require('./prompt');
  4. var _require = require('sisteransi');
  5. const cursor = _require.cursor,
  6. erase = _require.erase;
  7. var _require2 = require('../util');
  8. const style = _require2.style,
  9. clear = _require2.clear;
  10. const isNumber = /[0-9]/;
  11. const isValidChar = /\.|-/;
  12. const isDef = any => any !== undefined;
  13. const round = (number, precision) => {
  14. let factor = Math.pow(10, precision);
  15. return Math.round(number * factor) / factor;
  16. };
  17. /**
  18. * NumberPrompt Base Element
  19. * @param {Object} opts Options
  20. * @param {String} opts.message Message
  21. * @param {String} [opts.style='default'] Render style
  22. * @param {Number} [opts.initial] Default value
  23. * @param {Number} [opts.max=+Infinity] Max value
  24. * @param {Number} [opts.min=-Infinity] Min value
  25. * @param {Boolean} [opts.float=false] Parse input as floats
  26. * @param {Number} [opts.round=2] Round floats to x decimals
  27. * @param {Number} [opts.increment=1] Number to increment by when using arrow-keys
  28. */
  29. class NumberPrompt extends Prompt {
  30. constructor(opts = {}) {
  31. super(opts);
  32. this.transform = style.render(opts.style);
  33. this.msg = opts.message;
  34. this.initial = isDef(opts.initial) ? opts.initial : '';
  35. this.float = !!opts.float;
  36. this.round = opts.round || 2;
  37. this.inc = opts.increment || 1;
  38. this.min = isDef(opts.min) ? opts.min : -Infinity;
  39. this.max = isDef(opts.max) ? opts.max : Infinity;
  40. this.value = '';
  41. this.typed = '';
  42. this.lastHit = 0;
  43. this.render();
  44. }
  45. set value(v) {
  46. if (!v && v !== 0) {
  47. this.placeholder = true;
  48. this.rendered = color.gray(this.transform.render(`${this.initial}`));
  49. this._value = '';
  50. } else {
  51. this.placeholder = false;
  52. this.rendered = this.transform.render(`${round(v, this.round)}`);
  53. this._value = round(v, this.round);
  54. }
  55. this.fire();
  56. }
  57. get value() {
  58. return this._value;
  59. }
  60. parse(x) {
  61. return this.float ? parseFloat(x) : parseInt(x);
  62. }
  63. valid(c) {
  64. return c === '-' || c === '.' && this.float || isNumber.test(c);
  65. }
  66. reset() {
  67. this.typed = '';
  68. this.value = '';
  69. this.fire();
  70. this.render();
  71. }
  72. abort() {
  73. let x = this.value;
  74. this.value = x !== '' ? x : this.initial;
  75. this.done = this.aborted = true;
  76. this.fire();
  77. this.render();
  78. this.out.write('\n');
  79. this.close();
  80. }
  81. submit() {
  82. let x = this.value;
  83. this.value = x !== '' ? x : this.initial;
  84. this.done = true;
  85. this.aborted = false;
  86. this.fire();
  87. this.render();
  88. this.out.write('\n');
  89. this.close();
  90. }
  91. up() {
  92. this.typed = '';
  93. if (this.value >= this.max) return this.bell();
  94. this.value += this.inc;
  95. this.fire();
  96. this.render();
  97. }
  98. down() {
  99. this.typed = '';
  100. if (this.value <= this.min) return this.bell();
  101. this.value -= this.inc;
  102. this.fire();
  103. this.render();
  104. }
  105. delete() {
  106. let val = this.value.toString();
  107. if (val.length === 0) return this.bell();
  108. this.value = this.parse(val = val.slice(0, -1)) || '';
  109. this.fire();
  110. this.render();
  111. }
  112. next() {
  113. this.value = this.initial;
  114. this.fire();
  115. this.render();
  116. }
  117. _(c, key) {
  118. if (!this.valid(c)) return this.bell();
  119. const now = Date.now();
  120. if (now - this.lastHit > 1000) this.typed = ''; // 1s elapsed
  121. this.typed += c;
  122. this.lastHit = now;
  123. if (c === '.') return this.fire();
  124. this.value = Math.min(this.parse(this.typed), this.max);
  125. if (this.value > this.max) this.value = this.max;
  126. if (this.value < this.min) this.value = this.min;
  127. this.fire();
  128. this.render();
  129. }
  130. render() {
  131. let underline = !this.done || !this.done && !this.placeholder;
  132. this.out.write(erase.line + cursor.to(0) + [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(this.done), underline ? color.cyan.underline(this.rendered) : this.rendered].join(' '));
  133. }
  134. }
  135. module.exports = NumberPrompt;