/
multi.ts
196 lines (175 loc) · 5.33 KB
/
multi.ts
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
import { bgGreen, bgWhite, writeAllSync } from "./deps.ts";
const isTTY = Deno.isatty(Deno.stdout.rid);
const isWindow = Deno.build.os === "windows";
interface constructorOptions {
title?: string;
width?: number;
complete?: string;
incomplete?: string;
clear?: boolean;
interval?: number;
display?: string;
}
interface renderOptions {
completed: number;
text?: string;
total?: number;
complete?: string;
incomplete?: string;
}
export class MultiProgressBar {
width: number;
complete: string;
incomplete: string;
clear: boolean;
interval: number;
display: string;
#end = false;
#startIndex = 0;
#lastRows = 0;
#strs: string[] = [];
private lastStr = "";
private start = Date.now();
private time?: string;
private lastRender = 0;
private encoder = new TextEncoder();
/**
* Title, total, complete, incomplete, can also be set or changed in the render method
*
* @param title Progress bar title, default: ''
* @param width the displayed width of the progress, default: 50
* @param complete completion character, default: colors.bgGreen(' '), can use any string
* @param incomplete incomplete character, default: colors.bgWhite(' '), can use any string
* @param clear clear the bar on completion, default: false
* @param interval minimum time between updates in milliseconds, default: 16
* @param display What is displayed and display order, default: ':bar :text :percent :time :completed/:total'
*/
constructor(
{
title = "",
width = 50,
complete = bgGreen(" "),
incomplete = bgWhite(" "),
clear = false,
interval,
display,
}: constructorOptions = {},
) {
if (title != "") {
this.#strs.push(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";
}
/**
* "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
**/
render(bars: Array<renderOptions>): void {
if (this.#end || !isTTY) return;
const now = Date.now();
const ms = now - this.lastRender;
this.lastRender = now;
this.time = ((now - this.start) / 1000).toFixed(1) + "s";
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 (completed > total && this.#strs[index] != undefined) continue;
end = false;
const percent = ((completed / total) * 100).toFixed(2) + "%";
// :bar :text :percent :time :completed/:total
let str = this.display
.replace(":text", text)
.replace(":time", this.time)
.replace(":percent", percent)
.replace(":completed", completed + "")
.replace(":total", total + "");
// compute the available space (non-zero) for the bar
let availableSpace = Math.max(
0,
this.ttyColumns - str.replace(":bar", "").length,
);
if (availableSpace && isWindow) availableSpace -= 1;
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);
this.#strs[index++] = str;
}
if (ms < this.interval && end == false) return;
const renderStr = this.#strs.join("\n");
if (renderStr !== this.lastStr) {
this.resetScreen();
this.write(renderStr);
this.lastStr = renderStr;
this.#lastRows = this.#strs.length;
}
if (end) this.end();
}
/**
* end: end a progress bar.
* No need to call in most cases, unless you want to end before 100%
*/
end(): void {
this.#end = true;
if (this.clear) {
this.resetScreen();
} else {
this.breakLine();
}
this.showCursor();
}
/**
* interrupt the progress bar and write a message above it
*
* @param message The message to write
*/
console(message: string | number): void {
this.resetScreen();
this.write(`${message}`);
this.breakLine();
this.write(this.lastStr);
}
private write(msg: string): void {
msg = `${msg}\x1b[?25l`;
this.stdoutWrite(msg);
}
private resetScreen() {
if (this.#lastRows > 0) {
this.stdoutWrite("\x1b[" + (this.#lastRows - 1) + "A\r\x1b[?0J");
}
}
private get ttyColumns(): number {
return 100;
}
private breakLine() {
this.stdoutWrite("\r\n");
}
private stdoutWrite(msg: string) {
writeAllSync(Deno.stdout, this.encoder.encode(msg));
}
private showCursor(): void {
this.stdoutWrite("\x1b[?25h");
}
}