-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmulti.ts
More file actions
249 lines (224 loc) · 7.18 KB
/
Copy pathmulti.ts
File metadata and controls
249 lines (224 loc) · 7.18 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
import { bgGreen, bgWhite, stripAnsiCode } from "./deps.ts";
import { writeAll } from "./deps.ts";
// import type { Writer } from "./deps.ts";
import { prettyTime, type prettyTimeOptions } from "./time.ts";
const isTerminal = Deno.stdout.isTerminal;
interface constructorOptions {
title?: string;
width?: number;
complete?: string;
incomplete?: string;
clear?: boolean;
interval?: number;
display?: string;
prettyTime?: boolean;
output?: typeof Deno.stdout | typeof Deno.stderr;
}
interface renderOptions {
completed: number;
text?: string;
total?: number;
complete?: string;
incomplete?: string;
prettyTimeOptions?: prettyTimeOptions;
}
interface bar {
str: string;
strLen?: number;
end?: boolean;
}
/**
* MultiProgressBar multiple progress bars.
*/
export class MultiProgressBar {
width: number;
complete: string;
incomplete: string;
clear: boolean;
interval: number;
display: string;
prettyTime: boolean;
#end = false;
#startIndex = 0;
#lastRows = 0;
#bars: bar[] = [];
private lastStr = "";
private start = Date.now();
private lastRenderTime = 0;
private encoder = new TextEncoder();
// private writer: WritableStreamDefaultWriter<Uint8Array>;
// private writer: Writer;
private writer: typeof Deno.stdout | typeof Deno.stderr;
/**
* Title, total, complete, incomplete, can also be set or changed in the render method
*
* - title Progress bar title, default: ''
* - width the displayed width of the progress, default: 50
* - complete completion character, default: colors.bgGreen(' '), can use any string
* - incomplete incomplete character, default: colors.bgWhite(' '), can use any string
* - clear clear the bar on completion, default: false
* - interval minimum time between updates in milliseconds, default: 16
* - display What is displayed and display order, default: ':bar :text :percent :time :completed/:total'
* - prettyTime Whether to pretty print time and eta
* - output Output stream, can be Deno.stdout or Deno.stderr, default is Deno.stdout
*/
constructor({
title = "",
width = 50,
complete = bgGreen(" "),
incomplete = bgWhite(" "),
clear = false,
interval,
display,
prettyTime = false,
output = Deno.stdout,
}: constructorOptions = {}) {
if (title != "") {
this.#bars.push({ str: title });
this.#startIndex = 1;
}
this.width = width;
this.complete = complete;
this.incomplete = incomplete;
this.clear = clear;
this.interval = interval ?? 16;
this.display = display ?? ":bar :text :percent :time :completed/:total";
this.prettyTime = prettyTime;
// this.writer = output.writable.getWriter();
this.writer = output;
}
/**
* "render" the progress bar
*
* - `bars` progress bars
* - `completed` completed value
* - `total` optional, total number of ticks to complete, default: 100
* - `text` optional, text displayed per ProgressBar, default: ''
* - `complete` - optional, completion character
* - `incomplete` - optional, incomplete character
* - `prettyTimeOptions` - prettyTime options
*/
async render(bars: Array<renderOptions>): Promise<void> {
if (this.#end || !isTerminal) return;
const now = Date.now();
const ms = now - this.lastRenderTime;
this.lastRenderTime = now;
let end = true;
let index = this.#startIndex;
for (const { completed, total = 100, text = "", ...options } of bars) {
if (completed < 0) {
throw new Error(`completed must greater than or equal to 0`);
}
if (!Number.isInteger(total)) throw new Error(`total must be 'number'`);
if (this.#bars[index] && this.#bars[index].end) {
index++;
continue;
}
end = false;
const percent = ((completed / total) * 100).toFixed(2) + "%";
const time = this.prettyTime
? prettyTime(now - this.start, options.prettyTimeOptions)
: ((now - this.start) / 1000).toFixed(1) + "s";
const msEta = completed >= total
? 0
: (total / completed - 1) * (now - this.start);
const eta = completed == 0
? "-"
: this.prettyTime
? prettyTime(msEta, options.prettyTimeOptions)
: (msEta / 1000).toFixed(1) + "s";
// :bar :text :percent :time :completed/:total
let str = this.display
.replace(":text", text)
.replace(":time", time)
.replace(":eta", eta)
.replace(":percent", percent)
.replace(":completed", completed + "")
.replace(":total", total + "");
// compute the available space (non-zero) for the bar
const availableSpace = Math.max(
0,
this.ttyColumns - stripAnsiCode(str.replace(":bar", "")).length,
);
const width = Math.min(this.width, availableSpace);
// :bar
const completeLength = Math.round((width * completed) / total);
const complete = new Array(completeLength)
.fill(options.complete ?? this.complete)
.join("");
const incomplete = new Array(width - completeLength)
.fill(options.incomplete ?? this.incomplete)
.join("");
str = str.replace(":bar", complete + incomplete);
const strLen = stripAnsiCode(str).length;
if (this.#bars[index] && str != this.#bars[index].str) {
const lastStrLen = this.#bars[index].strLen!;
if (strLen < lastStrLen) {
str += " ".repeat(lastStrLen - strLen);
}
}
this.#bars[index++] = {
str,
strLen,
end: completed >= total,
};
}
if (ms < this.interval && !end) return;
const str = this.#bars.map((v) => v.str).join("\n");
if (str !== this.lastStr) {
await this.resetScreen();
await this.write(str);
this.lastStr = str;
this.#lastRows = this.#bars.length;
}
if (end) await this.end();
}
/**
* end: end a progress bar.
* No need to call in most cases, unless you want to end before 100%
*/
async end(): Promise<void> {
if (this.#end) return;
this.#end = true;
if (this.clear) {
await this.resetScreen();
} else {
await this.breakLine();
}
await this.showCursor();
// this.writer.releaseLock();
}
/**
* interrupt the progress bar and write a message above it
*
* @param message The message to write
*/
async console(message: string | number): Promise<void> {
await this.resetScreen();
await this.write(`${message}`);
await this.breakLine();
await this.write(this.lastStr);
}
private write(msg: string): Promise<void> {
return this.stdoutWrite(`${msg}\x1b[?25l`);
}
private async resetScreen(): Promise<void> {
if (this.#lastRows > 0) {
await this.stdoutWrite("\x1b[" + (this.#lastRows - 1) + "A\r\x1b[?0J");
}
}
private get ttyColumns(): number {
if (!Deno.stdout.isTerminal()) return 100;
return Deno.consoleSize().columns;
}
private breakLine(): Promise<void> {
return this.stdoutWrite("\n");
}
private stdoutWrite(msg: string): Promise<void> {
// return this.writer.write(this.encoder.encode(msg));
return writeAll(this.writer, this.encoder.encode(msg))
}
private showCursor(): Promise<void> {
return this.stdoutWrite("\x1b[?25h");
}
}