-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
920 lines (767 loc) · 25 KB
/
index.js
File metadata and controls
920 lines (767 loc) · 25 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
import crypto from 'node:crypto';
import readline from 'node:readline';
import wrapAnsi from 'wrap-ansi';
import stripAnsi from 'strip-ansi';
import interactive from 'is-interactive';
import chalk from 'chalk';
/**
* @module logion
*/
/** Class representing a logger. */
export class Logion {
#codes = {
nl: '\n',
screenClear: '\u001B[3J',
screenNorm: '\u001B[?1049l',
screenAlt: '\u001B[?1049h',
cursorShow: '\u001B[?25h',
cursorHide: '\u001B[?25l',
beep: '\u0007',
};
#initialized = false;
#endedWith = {
newline: 0,
separator: 0,
};
#chunks = new Map();
#spinners = new Map();
#separators = new Map();
#activeSpinners = new Set();
#updateTimout = null;
#startPos = NaN;
#input = null;
#streamIn;
#streamOut;
#write;
#isDisabled;
#isPaused;
#renderInterval;
#spinOpts;
/**
* A logger instance.
* @typedef {object} Instance
*/
/**
* Available style names.
* @typedef {"success"|"error"|"info"|"bold"|"underline"|"italic"|"strikethrough"} styleName
*/
/**
* Supported color names.
* @typedef {"black"|"red"|"green"|"yellow"|"blue"|"magenta"|"cyan"|"white"|"gray"|"blackBright"|"redBright"|"greenBright"|"yellowBright"|"blueBright"|"magentaBright"|"cyanBright"|"whiteBright"} color
*/
/**
* Format the spinner text when it is marked as done.
* @callback spinnerFormatDone
* @param {string} - Current spinner text.
* @returns {string} a formated spinner text.
*/
/**
* Create a logger.
* @param {object} options - Options object.
* @param {object} [options.streamIn=process.stdin] - Input stream (for scrolling and shortcuts).
* @param {object} [options.streamOut=process.stdout] - Output stream.
* @param {boolean} [options.disabled=auto] - Is logger disabled (no logging). By default, disabled if not ran in TTY.
* @param {boolean} [options.paused=false] - Is logger paused (not outputting to console and not listening for key presses, but still collecting logs).
* @param {number} [options.renderInterval=80] - Render interval in ms to update spinners.
* @param {color} [options.spinnerColor=white] - Spinner char {@link #module_logion--module.exports..color|color}.
* @param {number} [options.spinnerIndent=0] - Spinner indent length in text chars.
* @param {string[]} [options.spinnerFrames=["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]] - Spinner frames characters.
* @param {string} [options.spinnerDoneChar=✱] - Char to replace spinner after it is done (when calling {@link #module_logion--module.exports.Logion+spinnerDone|spinnerDone}).
* @param {string} [options.spinnerDoneAllChar=✸] - Char to replace active spinners after they are done (when calling {@link #module_logion--module.exports.Logion+spinnerDoneAll|spinnerDoneAll}).
* @param {function} [options.spinnerFormatDone=(str) => str] - Call this function to format the spinner text after it is done, see {@link #module_logion--module.exports..spinnerFormatDone|spinnerFormatDone}.
* @returns {Instance} a logger instance.
*/
constructor({
streamIn = process.stdin,
streamOut = process.stdout,
disabled = !interactive({ stream: streamOut }),
paused = false,
renderInterval = 80,
spinnerColor = 'white',
spinnerIndent = 0,
spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
spinnerDoneChar = '✱',
spinnerDoneAllChar = '✸',
spinnerFormatDone = (str) => str,
} = {}) {
this.#streamIn = streamIn;
this.#streamOut = streamOut;
this.#write = this.#streamOut.write.bind(this.#streamOut);
this.#isDisabled = disabled;
this.#isPaused = paused;
this.#renderInterval = renderInterval;
this.#spinOpts = {
color: spinnerColor,
indent: spinnerIndent,
frames: spinnerFrames,
doneChar: spinnerDoneChar,
doneAllChar: spinnerDoneAllChar,
formatter: spinnerFormatDone,
};
}
/**
* Output a newline(s).
* Accommodates if line breaks have been used before.
* @param {number} [num=1] - Number of new lines to output.
* @param {boolean} [force=false] - Do not accommodate for previous lines.
* @param {string} [identifier=$random] - Identifier for this log message (should be unique).
* @returns {Instance} a logger instance.
*/
newline(num = 1, force = false, identifier = this.#rand()) {
if (this.#endedWith.newline >= num && !force) {
return this;
}
const string = this.#codes.nl.repeat(
num - (force ? 0 : this.#endedWith.newline)
);
this.log(string, identifier);
this.#endedWith.newline = num;
this.#endedWith.separator = 0;
return this;
}
/**
* Output spaces.
* Can be used in the middle of a line.
* @param {number} [num=2] - Number of spaces to output.
* @param {string} [identifier=$random] - Identifier for this log message (should be unique).
* @returns {Instance} a logger instance.
*/
indent(num = 2, identifier = this.#rand()) {
this.log(' '.repeat(num), identifier);
this.#endedWith.newline = 0;
this.#endedWith.separator = 0;
return this;
}
/**
* Output a separator on a new line.
* It fills all the available width.
* @param {color} [color=grey] - A separator character's {@link #module_logion--module.exports..color|color}.
* @param {string} [char=-] - Character that make up the separator.
* @param {string} [identifier=$random] - Identifier for this log message (should be unique).
* @returns {Instance} a logger instance.
*/
separate(color = 'grey', char = '―', identifier = this.#rand()) {
if (this.#endedWith.separator) {
return this;
}
const startNl = this.#endedWith.newline;
const string = `${startNl ? '' : this.#codes.nl}${char.repeat(this.width)}${
this.#codes.nl
}`;
const colored = chalk[color](string);
this.#separators.set(identifier, {
identifier,
color,
startNl,
char,
});
this.log(colored, identifier);
this.#endedWith.separator = 1;
this.#endedWith.newline = 0;
return this;
}
/**
* Output a text with stlies applied.
* Does not add a new line.
* @param {string} [string=$space] - Text to output.
* @param {(object|styleName)} styles - Style object or {@link #module_logion--module.exports..styleName|styleName} string.
* @param {color} [styles.color] - Text color (uses Chalk module {@link #module_logion--module.exports..color|color}).
* @param {color} [styles.bgColor] - Background color (uses Chalk {@link #module_logion--module.exports..color|color}).
* @param {boolean} [styles.bold=false] - Output as bold.
* @param {boolean} [styles.underline=false] - Output as underlined.
* @param {boolean} [styles.italic=false] - Output as italic.
* @param {boolean} [styles.strikethrough=false] - Output as strike-through.
* @param {string} [identifier=$random] - Identifier for this log message (should be unique).
* @returns {Instance} a logger instance.
*/
text(string = ' ', styles, identifier = this.#rand()) {
this.log(styles ? this.style(string, styles) : string, identifier);
this.#endedWith.newline = 0;
this.#endedWith.separator = 0;
return this;
}
/**
* Style a text string.
* Shortening names could be one of: "success", "error", "info", "bold", "underline", "italic", "strikethrough".
* @param {string} string - Text to style.
* @param {(object|styleName)} styles - Style object or {@link #module_logion--module.exports..styleName|styleName} string.
* @param {color} [styles.color] - Text color (uses Chalk module {@link #module_logion--module.exports..color|color}).
* @param {color} [styles.bgColor] - Background color (uses Chalk {@link #module_logion--module.exports..color|color}).
* @param {boolean} [styles.bold=false] - Output as bold.
* @param {boolean} [styles.underline=false] - Output as underlined.
* @param {boolean} [styles.italic=false] - Output as italic.
* @param {boolean} [styles.strikethrough=false] - Output as strike-through.
* @param {string} [identifier=$random] - Identifier for this log message (should be unique).
* @returns {string} a styled string (with escape codes).
*/
style(string, styles) {
const capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
let construct = chalk;
let opts = {
bold: false,
underline: false,
italic: false,
strikethrough: false,
color: '',
bgColor: '',
};
if (typeof styles === 'object') {
opts = {
...opts,
...styles,
};
} else if (typeof styles === 'string') {
switch (styles) {
case this.styleNames.SUCCESS:
opts.color = 'green';
break;
case this.styleNames.ERROR:
opts.color = 'red';
break;
case this.styleNames.INFO:
opts.color = 'magenta';
break;
case this.styleNames.BOLD:
opts.bold = true;
break;
case this.styleNames.UNDERLINE:
opts.underline = true;
break;
case this.styleNames.ITALIC:
opts.italic = true;
break;
case this.styleNames.STRIKETHROUGH:
opts.strikethrough = true;
}
}
if (opts.bold) {
construct = construct.bold;
}
if (opts.underline) {
construct = construct.underline;
}
if (opts.italic) {
construct = construct.italic;
}
if (opts.strikethrough) {
construct = construct.strikethrough;
}
if (opts.color) {
construct = construct[opts.color];
}
if (opts.bgColor) {
construct = construct[`bg${capitalize(opts.bgColor)}`];
}
return construct(string);
}
/**
* Reset all styles for a string.
* @param {string} string - String with styled text.
* @returns {string} an unstyled string.
*/
styleReset(string) {
return stripAnsi(string);
}
/**
* Output a line of a text with an indent.
* @param {string} [string=$space] - Your text.
* @param {(object|string)} [styles] - Style object or shortening name (as string), same as {@link #module_logion--module.exports.Logion+style|style}.
* @param {color} [styles.color] - Text color (uses Chalk module {@link #module_logion--module.exports..color|color}).
* @param {color} [styles.bgColor] - Background color (uses Chalk {@link #module_logion--module.exports..color|color}).
* @param {boolean} [styles.bold=false] - Output as bold.
* @param {boolean} [styles.underline=false] - Output as underlined.
* @param {boolean} [styles.italic=false] - Output as italic.
* @param {boolean} [styles.strikethrough=false] - Output as strike-through.
* @param {number} [indt=0] - Line indentation, a number of spaces to output before the text.
* @returns {Instance} a logger instance.
*/
line(string = ' ', styles, indt = 0) {
this.newline();
this.indent(indt);
this.text(string, styles);
this.newline();
return this;
}
/**
* Clear all the output.
* @returns {Instance} a logger instance.
*/
clear() {
if (this.#isDisabled || !this.#initialized) {
return this;
}
this.#stopUpdater();
this.#clearScreen();
this.#chunks.clear();
this.#spinners.clear();
this.#separators.clear();
this.#activeSpinners.clear();
this.#endedWith.newline = 0;
this.#endedWith.separator = 0;
return this;
}
/**
* Create a spinner on a new line.
* You can mark a spinner as done using its identifier with {@link #module_logion--module.exports.Logion+spinnerDone|spinnerDone}.
* @param {string} identifier - Identifier for this spinner (required, should be unique).
* @param {string} [text] - Text to output after this spinner (on the same line).
* @param {object} [config] - An object for configuring this spinner.
* @param {color} [config.color=white] - Spinner char color (uses Chalk module {@link #module_logion--module.exports..color|color}).
* @param {number} [config.indent=0] - Amount of spaces to output before this spinner.
* @returns {Instance} a logger instance.
*/
spinner(
identifier,
text = '',
{ color = this.#spinOpts.color, indent = this.#spinOpts.indent } = {}
) {
if (this.#isDisabled) {
return this;
}
if (!identifier || typeof identifier !== 'string') {
throw new Error('Spinner identifier absent!');
}
if (this.#spinners.has(identifier)) {
throw new Error(`Spinner identifier '${identifier}' already exists!`);
}
const prefix = `${
this.#endedWith.newline || this.#endedWith.separator ? '' : this.#codes.nl
}${' '.repeat(indent)}`;
const string = `${prefix}${chalk[color](this.#spinOpts.frames[0])} ${text}${
this.#codes.nl
}`;
this.#spinners.set(identifier, {
identifier,
text,
color,
prefix,
active: true,
frame: 0,
});
this.#activeSpinners.add(identifier);
this.log(string, identifier);
this.#endedWith.newline = 1;
this.#endedWith.separator = 0;
return this;
}
/**
* Mark the spinner as done.
* If text is not provided, uses {@link #module_logion--module.exports..spinnerFormatDone|spinnerFormatDone} to format the original text.
* @param {string} identifier - Identifier for the spinner (previously used to create the spinner in {@link #module_logion--module.exports.Logion+spinner|spinner}).
* @param {object} [config] - An object for configuring this spinner.
* @param {string} [config.text] - A new text for this spinner (if empty, leaves the original text).
* @param {color} [config.color] - A new spinner char {@link #module_logion--module.exports..color|color} (if empty, uses the original color).
* @param {string} [config.char=✱] - A character which replaces the spinner.
* @returns {Instance} a logger instance.
*/
spinnerDone(
identifier,
{ text, color, char = this.#spinOpts.doneChar } = {}
) {
if (this.#isDisabled) {
return this;
}
const inst = this.#spinners.get(identifier);
if (!inst || !inst.active) {
return this;
}
const resText = text || this.#spinOpts.formatter.call(this, inst.text);
const resColor = color || inst.color;
const string = `${inst.prefix}${chalk[resColor](char)} ${resText}${
this.#codes.nl
}`;
this.#spinners.set(identifier, {
...inst,
color: resColor,
text: resText,
active: false,
});
this.#chunks.set(identifier, string);
this.#activeSpinners.delete(identifier);
this.#update();
return this;
}
/**
* Mark all spinners that are not already done as done.
* Uses {@link #module_logion--module.exports..spinnerFormatDone|spinnerFormatDone} to format the original text.
* @param {object} [config] - An object for configuring this spinner.
* @param {color} [config.color] - A new spinner char {@link #module_logion--module.exports..color|color} (if empty, uses the original color).
* @param {string} [config.char=✸] - A character which replaces the spinner.
* @returns {Instance} a logger instance.
*/
spinnerDoneAll({ color, char = this.#spinOpts.doneAllChar } = {}) {
if (this.#isDisabled) {
return this;
}
for (const inst of this.#spinners) {
if (!inst[1].active) {
continue;
}
const resText = this.#spinOpts.formatter.call(this, inst[1].text);
const resColor = color || inst[1].color;
const string = `${inst[1].prefix}${chalk[resColor](char)} ${resText}${
this.#codes.nl
}`;
this.#spinners.set(inst[0], {
...inst[1],
color: resColor,
text: resText,
active: false,
});
this.#chunks.set(inst[0], string);
}
this.#activeSpinners.clear();
this.#update();
return this;
}
/**
* Make a beep sound.
* May not be played in some consoles.
* @returns {Instance} a logger instance.
*/
beep() {
if (this.#isDisabled) {
return this;
}
this.#write(this.#codes.beep);
return this;
}
/**
* Output a special spinner and waits for a user interaction.
* Removes the spinner after interaction and resolves a Promise.
* @param {string} [text=Press any key to continue] - Text for the spinner
* @param {object} [config] - An object for configuring the spinner, see {@link #module_logion--module.exports.Logion+spinner|spinner}.
* @returns {Promise.<Instance>} a promise which resolves after user interaction (resolver returns a logger instance).
*/
waitInteraction(text = 'Press any key to continue', config) {
return new Promise((success) => {
if (this.#isDisabled) {
success(this);
}
const spinId = this.#rand();
const spaceId = this.#rand();
const cb = (chunk, key) => {
if (
key &&
!['up', 'down', 'k', 'j', 'home', 'end'].includes(key.name)
) {
this.#streamIn.off('keypress', cb);
this.removeLog(spaceId);
this.removeLog(spinId);
success(this);
}
};
this.newline(1, false, spaceId);
this.spinner(spinId, text, config);
this.#streamIn.on('keypress', cb);
});
}
/**
* Log a row string.
* Other methods use this under the hood.
* @param {string} string - String to output.
* @param {string} [identifier=$random] - Identifier for this log message (should be unique).
* @returns {Instance} a logger instance.
*/
log(string, identifier = this.#rand()) {
if (this.#isDisabled || !string) {
return this;
}
if (this.#chunks.has(identifier)) {
throw new Error(`Chunk identifier '${identifier}' already exists!`);
}
this.#chunks.set(identifier, string.toString());
this.#update();
return this;
}
/**
* Remove a log message with the corresponding identifier.
* @param {string} identifier - Identifier for the log message (used to create it in other methods).
* @returns {Instance} a logger instance.
*/
removeLog(identifier) {
this.#chunks.delete(identifier);
this.#spinners.delete(identifier);
this.#separators.delete(identifier);
this.#activeSpinners.delete(identifier);
this.#update();
return this;
}
/**
* Enable a logger.
* @returns {Instance} a logger instance.
*/
enable() {
if (this.#initialized && !this.#isDisabled) {
return this;
}
this.#isDisabled = false;
this.#initialized = true;
process.on('exit', this.#onExit.bind(this));
this.#streamOut.on('resize', () => {
this.#updateSeparators();
this.#update();
});
if (!this.#isPaused) {
this.resume();
}
this.#write(this.#codes.screenAlt);
return this;
}
/**
* Disable a logger.
* When disabled, it removes all listeners, resets screen buffer, and clears all logs.
* @returns {Instance} a logger instance.
*/
disable() {
if (this.#isDisabled) {
return this;
}
this.#isDisabled = true;
if (!this.#initialized) {
return this;
}
const isPaused = this.#isPaused;
process.off('exit', this.#onExit);
this.clear();
this.#write(this.#codes.screenNorm);
this.pause();
this.#isPaused = isPaused;
return this;
}
/**
* Update the console with the last messages, resume spinners and listen for user interactions.
* Use this after {@link #module_logion--module.exports.Logion+pause|pause}.
* @returns {Instance} a logger instance.
*/
resume() {
if (this.#isDisabled) {
return this;
}
this.pause();
this.#isPaused = false;
this.#write(this.#codes.cursorHide);
this.#input = readline.createInterface({
input: this.#streamIn,
escapeCodeTimeout: 50,
});
readline.emitKeypressEvents(this.#streamIn, this.#input);
this.#streamIn.on('keypress', (chunk, key) => {
if (!key) {
return;
}
if ((key.ctrl && key.name === 'c') || key.name === 'q') {
process.exit();
}
if (Number.isNaN(this.#startPos)) {
return;
}
if (!key.shift && (key.name === 'up' || key.name === 'k')) {
this.#startPos--;
this.renderEnd = false;
this.#update();
return;
}
if (!key.shift && (key.name === 'down' || key.name === 'j')) {
this.#startPos++;
this.renderEnd = false;
this.#update();
return;
}
if ((key.shift && key.name === 'up') || key.name === 'home') {
this.#startPos = 0;
this.renderEnd = false;
this.#update();
return;
}
if ((key.shift && key.name === 'down') || key.name === 'end') {
this.renderEnd = true;
this.#update();
}
});
this.#streamIn.setRawMode(true);
return this;
}
/**
* Pause all logging, remove all listeners.
* Use this to temporary output something outside a logger instance, or to release a process from events listening (by logion).
* @returns {Instance} a logger instance.
*/
pause() {
this.#isPaused = true;
this.#write(this.#codes.cursorShow);
if (this.#input) {
this.#input.close();
this.#input = null;
}
return this;
}
/**
* Width of a console.
* @readonly
* @returns {number} width in chars.
*/
get width() {
return (
this.#streamOut?.columns || Number.parseInt(process.env.COLUMNS, 10) || 80
);
}
/**
* Height of a console.
* @readonly
* @returns {number} height in chars.
*/
get height() {
return this.#streamOut?.rows || Number.parseInt(process.env.ROWS, 10) || 40;
}
/**
* Returns true if a logger is disabled.
* @readonly
* @returns {boolean} is disabled.
*/
get disabled() {
return this.#isDisabled;
}
/**
* Returns true if a logger is paused.
* @readonly
* @returns {boolean} is paused.
*/
get paused() {
return this.#isPaused;
}
/**
* Shortening style names enum.
* See {@link #module_logion--module.exports..styleName|Available style names}
* @readonly
* @returns {object} style names enum.
*/
get styleNames() {
return {
SUCCESS: 'success',
ERROR: 'error',
INFO: 'info',
BOLD: 'bold',
UNDERLINE: 'underline',
ITALIC: 'italic',
STRIKETHROUGH: 'strikethrough',
};
}
#render(final = false) {
if (this.#isDisabled) {
return this;
}
if (!this.#initialized) {
this.enable();
}
if (!final) {
this.#clearScreen();
}
let accum = '';
for (const chunk of this.#chunks) {
accum += chunk[1];
}
if (final) {
this.#write(accum);
return this;
}
const lines = wrapAnsi(accum, this.width, {
wordWrap: false,
trim: false,
}).split(this.#codes.nl);
const lineLen = lines.length;
if (Number.isNaN(this.#startPos)) {
this.#startPos = lineLen - this.height;
}
if (this.#startPos < 0) {
this.#startPos = 0;
}
if (this.height > lineLen) {
this.#startPos = 0;
this.renderEnd = true;
} else if (this.renderEnd || this.#startPos + this.height > lineLen) {
this.#startPos = lineLen - this.height + 1;
this.renderEnd = true;
}
const content = lines
.slice(this.#startPos, this.#startPos + this.height)
.join(this.#codes.nl);
this.#write(content);
return this;
}
#update() {
if (this.#isDisabled || this.#isPaused) {
return this;
}
this.#stopUpdater();
this.#render();
if (this.#activeSpinners.size) {
this.#startUpdater();
}
return this;
}
#startUpdater(delay = this.#renderInterval) {
this.#stopUpdater();
this.#updateTimout = setTimeout(this.#updateSpinners.bind(this), delay);
return this;
}
#stopUpdater() {
clearTimeout(this.#updateTimout);
this.#updateTimout = null;
}
#clearScreen() {
this.#streamOut.cursorTo(0, 0);
this.#write(this.#codes.screenClear);
this.#streamOut.clearScreenDown();
return this;
}
#updateSeparators() {
if (!this.#separators.size) {
return this;
}
for (const inst of this.#separators) {
const string = `${
inst[1].startNl ? '' : this.#codes.nl
}${inst[1].char.repeat(this.width)}${this.#codes.nl}`;
const colored = chalk[inst[1].color](string);
this.#chunks.set(inst[0], colored);
}
return this;
}
#updateSpinners() {
if (!this.#activeSpinners.size) {
return this;
}
for (const inst of this.#spinners) {
if (!inst[1].active) {
continue;
}
const nextFrame = inst[1].frame + 1;
const newFrame =
nextFrame === this.#spinOpts.frames.length ? 0 : nextFrame;
const string = `${inst[1].prefix}${chalk[inst[1].color](
this.#spinOpts.frames[newFrame]
)} ${inst[1].text}${this.#codes.nl}`;
this.#spinners.set(inst[0], {
...inst[1],
frame: newFrame,
});
this.#chunks.set(inst[0], string);
}
this.#update();
return this;
}
#onExit() {
this.#stopUpdater();
this.spinnerDoneAll();
this.#write(this.#codes.screenNorm);
this.#render(true);
this.#write(this.#codes.nl);
this.pause();
}
#rand() {
return crypto.randomBytes(16).toString('hex');
}
}
/** Class representing a logger. */
export default Logion;