1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const DEFAULT_TERMINAL_WIDTH = process.stdout.columns || 70;
|
4 | function formatTime(msec) {
|
5 | const dd = Math.floor(msec / 1000 / 60 / 60 / 24);
|
6 | msec -= dd * 1000 * 60 * 60 * 24;
|
7 | const hh = Math.floor(msec / 1000 / 60 / 60);
|
8 | msec -= hh * 1000 * 60 * 60;
|
9 | const mm = Math.floor(msec / 1000 / 60);
|
10 | msec -= mm * 1000 * 60;
|
11 | const ss = Math.floor(msec / 1000);
|
12 | msec -= ss * 1000;
|
13 | const str = [`0${hh}`.slice(-2), `0${mm}`.slice(-2), `0${ss}`.slice(-2)].join(':');
|
14 | return dd ? `${dd} días` : str;
|
15 | }
|
16 | function getStateInfo(size, value, ellapsedTime, remainingTime) {
|
17 | const ellapsed = formatTime(ellapsedTime);
|
18 | const percent = Math.floor((value * 100) / size);
|
19 | const eta = formatTime(value >= size ? 0 : remainingTime);
|
20 | return {
|
21 | prefix: ellapsed + ' ' + percent + '% [',
|
22 | suffix: '] ETA ' + eta,
|
23 | percent,
|
24 | };
|
25 | }
|
26 | function getBar(barWidth, percent) {
|
27 | const ticks = [];
|
28 | for (let i = 0, len = barWidth; i < len; i++) {
|
29 | if ((i * 100) / len <= percent) {
|
30 | ticks.push('#');
|
31 | }
|
32 | else {
|
33 | ticks.push('·');
|
34 | }
|
35 | }
|
36 | return ticks.join('');
|
37 | }
|
38 | class ProgressBar {
|
39 | constructor(size = 100, { tickSize = 1, silent = false, terminalWidth = DEFAULT_TERMINAL_WIDTH } = {}) {
|
40 | this.size = size;
|
41 | this.tickSize = tickSize;
|
42 | this.value = 0;
|
43 | this.startTime = Date.now();
|
44 | this.lastRemainingTimes = [];
|
45 | this.silent = silent;
|
46 | this.terminalWidth = terminalWidth;
|
47 | this.callback = () => { };
|
48 | }
|
49 | getEllapsed() {
|
50 | return Date.now() - this.startTime;
|
51 | }
|
52 | getRemaining() {
|
53 | const secondsPerTick = this.getEllapsed() / this.value;
|
54 | const remaining = Math.floor((this.size - this.value) * secondsPerTick);
|
55 | this.lastRemainingTimes.push(remaining);
|
56 | if (this.lastRemainingTimes.length > 5)
|
57 | this.lastRemainingTimes.shift();
|
58 | const sum = this.lastRemainingTimes.reduce((accum, num) => accum + num, 0);
|
59 | return Math.floor(sum / this.lastRemainingTimes.length);
|
60 | }
|
61 | setValue(value) {
|
62 | this.value = Math.min(value, this.size);
|
63 | const str = this.print();
|
64 | if (this.value === this.size) {
|
65 | this.write('\n');
|
66 | if (this.callback)
|
67 | this.callback();
|
68 | this.callback = () => { };
|
69 | }
|
70 | return str;
|
71 | }
|
72 | tick() {
|
73 | return this.setValue(this.value + this.tickSize);
|
74 | }
|
75 | onFinish(callback) {
|
76 | this.callback = callback;
|
77 | }
|
78 | write(text) {
|
79 | if (this.silent)
|
80 | return;
|
81 | process.stdout.write(text);
|
82 | }
|
83 | print() {
|
84 | const { prefix, suffix, percent } = getStateInfo(this.size, this.value, this.getEllapsed(), this.getRemaining());
|
85 | const barWidth = this.terminalWidth - suffix.length - prefix.length;
|
86 | const bar = getBar(barWidth, percent);
|
87 | this.write('\r');
|
88 | const str = prefix + bar + suffix;
|
89 | this.write(str);
|
90 | return str;
|
91 | }
|
92 | }
|
93 | exports.default = ProgressBar;
|