source: frontend/node_modules/prompts/lib/elements/date.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
RevLine 
[9af201e]1'use strict';
2
3const color = require('kleur');
4const Prompt = require('./prompt');
5const { style, clear, figures } = require('../util');
6const { erase, cursor } = require('sisteransi');
7const { DatePart, Meridiem, Day, Hours, Milliseconds, Minutes, Month, Seconds, Year } = require('../dateparts');
8
9const regex = /\\(.)|"((?:\\["\\]|[^"])+)"|(D[Do]?|d{3,4}|d)|(M{1,4})|(YY(?:YY)?)|([aA])|([Hh]{1,2})|(m{1,2})|(s{1,2})|(S{1,4})|./g;
10const regexGroups = {
11 1: ({token}) => token.replace(/\\(.)/g, '$1'),
12 2: (opts) => new Day(opts), // Day // TODO
13 3: (opts) => new Month(opts), // Month
14 4: (opts) => new Year(opts), // Year
15 5: (opts) => new Meridiem(opts), // AM/PM // TODO (special)
16 6: (opts) => new Hours(opts), // Hours
17 7: (opts) => new Minutes(opts), // Minutes
18 8: (opts) => new Seconds(opts), // Seconds
19 9: (opts) => new Milliseconds(opts), // Fractional seconds
20}
21
22const dfltLocales = {
23 months: 'January,February,March,April,May,June,July,August,September,October,November,December'.split(','),
24 monthsShort: 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(','),
25 weekdays: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(','),
26 weekdaysShort: 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split(',')
27}
28
29
30/**
31 * DatePrompt Base Element
32 * @param {Object} opts Options
33 * @param {String} opts.message Message
34 * @param {Number} [opts.initial] Index of default value
35 * @param {String} [opts.mask] The format mask
36 * @param {object} [opts.locales] The date locales
37 * @param {String} [opts.error] The error message shown on invalid value
38 * @param {Function} [opts.validate] Function to validate the submitted value
39 * @param {Stream} [opts.stdin] The Readable stream to listen to
40 * @param {Stream} [opts.stdout] The Writable stream to write readline data to
41 */
42class DatePrompt extends Prompt {
43 constructor(opts={}) {
44 super(opts);
45 this.msg = opts.message;
46 this.cursor = 0;
47 this.typed = '';
48 this.locales = Object.assign(dfltLocales, opts.locales);
49 this._date = opts.initial || new Date();
50 this.errorMsg = opts.error || 'Please Enter A Valid Value';
51 this.validator = opts.validate || (() => true);
52 this.mask = opts.mask || 'YYYY-MM-DD HH:mm:ss';
53 this.clear = clear('', this.out.columns);
54 this.render();
55 }
56
57 get value() {
58 return this.date
59 }
60
61 get date() {
62 return this._date;
63 }
64
65 set date(date) {
66 if (date) this._date.setTime(date.getTime());
67 }
68
69 set mask(mask) {
70 let result;
71 this.parts = [];
72 while(result = regex.exec(mask)) {
73 let match = result.shift();
74 let idx = result.findIndex(gr => gr != null);
75 this.parts.push(idx in regexGroups
76 ? regexGroups[idx]({ token: result[idx] || match, date: this.date, parts: this.parts, locales: this.locales })
77 : result[idx] || match);
78 }
79
80 let parts = this.parts.reduce((arr, i) => {
81 if (typeof i === 'string' && typeof arr[arr.length - 1] === 'string')
82 arr[arr.length - 1] += i;
83 else arr.push(i);
84 return arr;
85 }, []);
86
87 this.parts.splice(0);
88 this.parts.push(...parts);
89 this.reset();
90 }
91
92 moveCursor(n) {
93 this.typed = '';
94 this.cursor = n;
95 this.fire();
96 }
97
98 reset() {
99 this.moveCursor(this.parts.findIndex(p => p instanceof DatePart));
100 this.fire();
101 this.render();
102 }
103
104 exit() {
105 this.abort();
106 }
107
108 abort() {
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 async validate() {
118 let valid = await this.validator(this.value);
119 if (typeof valid === 'string') {
120 this.errorMsg = valid;
121 valid = false;
122 }
123 this.error = !valid;
124 }
125
126 async submit() {
127 await this.validate();
128 if (this.error) {
129 this.color = 'red';
130 this.fire();
131 this.render();
132 return;
133 }
134 this.done = true;
135 this.aborted = false;
136 this.fire();
137 this.render();
138 this.out.write('\n');
139 this.close();
140 }
141
142 up() {
143 this.typed = '';
144 this.parts[this.cursor].up();
145 this.render();
146 }
147
148 down() {
149 this.typed = '';
150 this.parts[this.cursor].down();
151 this.render();
152 }
153
154 left() {
155 let prev = this.parts[this.cursor].prev();
156 if (prev == null) return this.bell();
157 this.moveCursor(this.parts.indexOf(prev));
158 this.render();
159 }
160
161 right() {
162 let next = this.parts[this.cursor].next();
163 if (next == null) return this.bell();
164 this.moveCursor(this.parts.indexOf(next));
165 this.render();
166 }
167
168 next() {
169 let next = this.parts[this.cursor].next();
170 this.moveCursor(next
171 ? this.parts.indexOf(next)
172 : this.parts.findIndex((part) => part instanceof DatePart));
173 this.render();
174 }
175
176 _(c) {
177 if (/\d/.test(c)) {
178 this.typed += c;
179 this.parts[this.cursor].setTo(this.typed);
180 this.render();
181 }
182 }
183
184 render() {
185 if (this.closed) return;
186 if (this.firstRender) this.out.write(cursor.hide);
187 else this.out.write(clear(this.outputText, this.out.columns));
188 super.render();
189
190 // Print prompt
191 this.outputText = [
192 style.symbol(this.done, this.aborted),
193 color.bold(this.msg),
194 style.delimiter(false),
195 this.parts.reduce((arr, p, idx) => arr.concat(idx === this.cursor && !this.done ? color.cyan().underline(p.toString()) : p), [])
196 .join('')
197 ].join(' ');
198
199 // Print error
200 if (this.error) {
201 this.outputText += this.errorMsg.split('\n').reduce(
202 (a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);
203 }
204
205 this.out.write(erase.line + cursor.to(0) + this.outputText);
206 }
207}
208
209module.exports = DatePrompt;
Note: See TracBrowser for help on using the repository browser.