1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | exports.ProgressCallback = exports.ProgressBar = void 0;
|
9 | class ProgressBar {
|
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 | constructor(format, options = {}) {
|
35 | this.format = format;
|
36 | this.current = 0;
|
37 | this.total = 0;
|
38 | this.tokens = null;
|
39 | this.lastDraw = "";
|
40 | this.start = 0;
|
41 | this.complete = false;
|
42 | this.stream = options.stream || process.stderr;
|
43 | this.total = options.total;
|
44 | this.width = options.width || this.total;
|
45 | this.chars = {
|
46 | complete: options.complete || "=",
|
47 | incomplete: options.incomplete || "-",
|
48 | head: options.head || options.complete || "=",
|
49 | };
|
50 | }
|
51 | |
52 |
|
53 |
|
54 | tick(delta) {
|
55 | this.currentAmount = this.current + delta;
|
56 | }
|
57 | set currentAmount(value) {
|
58 | this.current = value;
|
59 | if (this.complete) {
|
60 | return;
|
61 | }
|
62 | this.render();
|
63 | if (this.current >= this.total) {
|
64 | this.complete = true;
|
65 | this.terminate();
|
66 | }
|
67 | }
|
68 | render() {
|
69 |
|
70 | if (this.start === 0) {
|
71 | this.start = Date.now();
|
72 | }
|
73 | const ratio = Math.min(Math.max(this.current / this.total, 0), 1);
|
74 | const percent = ratio * 100;
|
75 | const elapsed = Date.now() - this.start;
|
76 | const eta = percent === 100 ? 0 : elapsed * (this.total / this.current - 1);
|
77 | const rate = this.current / (elapsed / 1000);
|
78 |
|
79 | let str = this.format
|
80 | .replace(":current", this.current.toString())
|
81 | .replace(":total", this.total.toString())
|
82 | .replace(":elapsed", isNaN(elapsed) ? "0.0" : (elapsed / 1000).toFixed(1))
|
83 | .replace(":eta", isNaN(eta) || !isFinite(eta) ? "0.0" : (eta / 1000).toFixed(1))
|
84 | .replace(":percent", percent.toFixed(0) + "%")
|
85 | .replace(":rate", Math.round(rate).toString());
|
86 |
|
87 | let availableSpace = Math.max(0, this.stream.columns - str.replace(":bar", "").length);
|
88 | if (availableSpace && process.platform === "win32") {
|
89 | availableSpace -= 1;
|
90 | }
|
91 | const width = Math.min(this.width, availableSpace);
|
92 | const completeLength = Math.round(width * ratio);
|
93 | let complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
|
94 | const incomplete = Array(Math.max(0, width - completeLength + 1)).join(this.chars.incomplete);
|
95 |
|
96 | if (completeLength > 0) {
|
97 | complete = `${complete.slice(0, -1)}${this.chars.head}`;
|
98 | }
|
99 |
|
100 | str = str.replace(":bar", complete + incomplete);
|
101 |
|
102 | if (this.tokens != null) {
|
103 | for (const key of Object.keys(this.tokens)) {
|
104 | str = str.replace(`:${key}`, this.tokens[key]);
|
105 | }
|
106 | }
|
107 | if (this.lastDraw !== str) {
|
108 | this.stream.cursorTo(0);
|
109 | this.stream.write(str);
|
110 | this.stream.clearLine(1);
|
111 | this.lastDraw = str;
|
112 | }
|
113 | }
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | update(ratio) {
|
124 | const goal = Math.floor(ratio * this.total);
|
125 | const delta = goal - this.current;
|
126 | this.tick(delta);
|
127 | }
|
128 | |
129 |
|
130 |
|
131 | interrupt(message) {
|
132 |
|
133 | const stream = this.stream;
|
134 | stream.clearLine();
|
135 |
|
136 | stream.cursorTo(0);
|
137 |
|
138 | stream.write(message);
|
139 |
|
140 | stream.write("\n");
|
141 |
|
142 | stream.write(this.lastDraw);
|
143 | }
|
144 | }
|
145 | exports.ProgressBar = ProgressBar;
|
146 | class ProgressCallback {
|
147 | constructor(progressBar) {
|
148 | this.progressBar = progressBar;
|
149 | this.start = Date.now();
|
150 | this.nextUpdate = this.start + 1000;
|
151 | }
|
152 | update(transferred, total) {
|
153 | const now = Date.now();
|
154 | if (now >= this.nextUpdate || transferred >= total) {
|
155 | this.nextUpdate = now + 1000;
|
156 | this.progressBar.total = total;
|
157 | this.progressBar.currentAmount = transferred;
|
158 | }
|
159 | }
|
160 | }
|
161 | exports.ProgressCallback = ProgressCallback;
|
162 |
|
\ | No newline at end of file |