number.js 3.8 KB

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