source: frontend/node_modules/prompts/lib/elements/number.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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