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