-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
317 lines (277 loc) · 7.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* Requires
*/
const timestamp = require( 'time-stamp' );
/**
* @typedef {Object<string, string>} ASCIIReference
*/
/**
* ASCII code definitions
* @type {Object|ASCIIReference}
*/
const ASCIIREF = {
// Style and control
re : '\x1b[0m', // reset
bo : '\x1b[1m', // bold
th : '\x1b[2m', // thin
ul : '\x1b[4m', // underline
bl : '\x1b[5m', // blink
rv : '\x1b[7m', // reverse
hd : '\x1b[8m', // hidden
// Foreground colors
fblack : '\x1b[30m',
fred : '\x1b[31m',
fgreen : '\x1b[32m',
fyellow : '\x1b[33m',
fblue : '\x1b[34m',
fmagenta : '\x1b[35m',
fcyan : '\x1b[36m',
fwhite : '\x1b[37m',
// Background colors
bblack : '\x1b[40m',
bred : '\x1b[41m',
bgreen : '\x1b[42m',
byellow : '\x1b[43m',
bblue : '\x1b[44m',
bmagenta : '\x1b[45m',
bcyan : '\x1b[46m',
bwhite : '\x1b[47m',
};
/**
* @callback OutputLoggerMethod
* @param {...*} input - Output arguments
* @return {void}
*/
/**
* @typedef {Object} OutputLogger
* @property {Function|OutputLoggerMethod} log - Output any styled
* @property {null|Function|OutputLoggerMethod} error - Output error style
* @property {null|Function|OutputLoggerMethod} warn - Output warning style
* @property {null|Function|OutputLoggerMethod} info - Output info style
* @property {null|Function|OutputLoggerMethod} success - Output success style
*/
/**
* @typedef {Object<string, string>} OutputStylerDefaultStyles
* @property {string} error - Error style
* @property {string} warn - Warning style
* @property {string} success - Success style
* @property {string} info - Info style
*/
/**
* OutputStyler
* @class
*/
class OutputStyler {
/**
* Constructor
* @constructor
* @param {Object|ASCIIReference} reference - ASCII code reference object
* @param {Object|OutputLogger|Console} target - Target object, default: console
* @throws Error
*/
constructor( reference, target ) {
// Check target
if ( target && typeof this._.log !== 'function' ) {
throw new Error( 'Invalid target reference, method not found: target.log()' );
}
// Check reference
if ( reference && ( typeof reference !== 'object' || !Object.keys( reference ).length ) ) {
throw new Error( 'Invalid ascii reference object or no keys available' );
}
/**
* Output
* @protected
* @property
* @type {Object|Console}
*/
this._ = target || console;
/**
* Style reference
* @public
* @property
* @type {Object}
*/
this.reference = reference || ASCIIREF;
/**
* Timestamp format
* @public
* @property
* @type {string}
*/
this.timestampFormat = 'YYYY-MM-DD HH:mm:ss';
/**
* Timestamp prefix
* @public
* @property
* @type {string}
*/
this.timestampPrefix = '[fwhite][[re][th]';
/**
* Timestamp suffix
* @public
* @property
* @type {string}
*/
this.timestampSuffix = '[re][fwhite]][re] ';
/**
* Settable auto prepend time
* @public
* @property
* @type {boolean}
*/
this.prependTime = false;
/**
* Default reset string
* @public
* @property
* @type {string}
*/
this.defaultReset = ' [re]';
/**
* Default method styles
* @type {Object|OutputStylerDefaultStyles}
*/
this.style = {
error : '[bred][fwhite] ',
warn : '[byellow][fblack] ',
info : '[bblack][fcyan] ',
success : '[bgreen][fblack] ',
};
/**
* Style reset string
* @public
* @property
* @type {string}
*/
this.styleReset = ' [re]';
}
/**
* Set styles defined in string
* @public
* @param {string|number} value - Target string
* @return {string} - Styled string
*/
setStyle( value ) {
// Simple type
if ( ![ 'string', 'number', 'undefined' ].includes( typeof value ) ) {
return value;
}
// Convert to string
let styled = value + '';
// Replace all style references
Object.keys( this.reference ).forEach( ( key ) => {
const regex = new RegExp( '\\[(' + key + ')\\]', 'g' );
styled = styled.replace( regex, this.reference[ key ] );
} );
return styled;
}
/**
* Prepends time if required
* @protected
* @return {void}
*/
_prependTime() {
if ( this.prependTime ) {
process.stdout.write( this.setStyle( this.timestampPrefix
+ timestamp( this.timestampFormat ) + this.timestampSuffix ) );
}
}
/**
* Write styled
* @protected
* @param {string} fn - Target function name
* @param {Array<*>} data - Arguments
* @param {null|string} prepend - Style to prepend, default: null
* @param {string} append - Style to append, default: null|OutputStyler.defaultReset
* @return {void}
*/
_write( fn, data, prepend = null, append = null ) {
append = append || this.defaultReset;
let added = 0;
// Prepend time
this._prependTime();
// Set opening style
if ( prepend ) {
data.unshift( prepend );
data.push( append );
added += 2;
}
// Only handle style if one was added
if ( data.length > added ) {
for ( let i = 0; i < data.length; i++ ) {
const to = typeof data[ i + 1 ];
if ( typeof data[ i ] !== 'object' ) {
data[ i ] = this.setStyle( data[ i ] );
// End style for next complex object
if ( to === 'object' && prepend ) {
data.splice( i + 1, 0, this.setStyle( append ) );
i++;
}
} else if ( prepend && to !== 'undefined' && to !== 'object' ) {
// Reopen style for the next element
data.splice( i + 1, 0, this.setStyle( prepend ) );
i++;
}
}
}
// Ensure valid target function and call
if ( !this._[ fn ] ) fn = 'log';
this._[ fn ]( ...data );
}
/**
* Custom style
* @public
* @param {...*} data - Data to log
* @return {void}
*/
log( ...data ) {
this._write( 'log', data );
}
/**
* Red error style
* @public
* @param {...*} data - Data to log
* @return {void}
*/
error( ...data ) {
this._write( 'error', data, this.style.error, this.styleReset );
}
/**
* Yellow warning style
* @public
* @param {...*} data - Data to log
* @return {void}
*/
warn( ...data ) {
this._write( 'warn', data, this.style.warn, this.styleReset );
}
/**
* Cyan info style
* @public
* @param {...*} data - Data to log
* @return {void}
*/
info( ...data ) {
this._write( 'info', data, this.style.info, this.styleReset );
}
/**
* Green success style
* @public
* @param {...*} data - Data to log
* @return {void}
*/
success( ...data ) {
this._write( 'log', data, this.style.success, this.styleReset );
}
}
/**
* Default cfx instance
* @type {OutputStyler}
*/
const cfx = new OutputStyler();
// Export all public references
module.exports = {
ASCIIREF,
OutputStyler,
cfx,
};