@bettergi/utils 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -14
- package/dist/progress.d.ts +16 -15
- package/dist/progress.js +12 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -154,21 +154,19 @@ const total = 100;
|
|
|
154
154
|
const tracker = new ProgressTracker(total, { interval: 3000 });
|
|
155
155
|
for (let i = 0; i < total; i++) {
|
|
156
156
|
await sleep(Math.round(Math.random() * 200));
|
|
157
|
-
//
|
|
158
|
-
tracker.tick(
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
//
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
// printed();
|
|
169
|
-
// });
|
|
157
|
+
// 仅递进+1
|
|
158
|
+
tracker.tick();
|
|
159
|
+
// 递进+3,并尝试打印当前进度和消息
|
|
160
|
+
tracker.tick({ message: "等待任务完成...", increment: 3 });
|
|
161
|
+
// 不递进,尝试打印当前进度和消息
|
|
162
|
+
tracker.print("等待任务完成...");
|
|
163
|
+
// 不递进,强制打印当前进度和警告消息
|
|
164
|
+
tracker.print("等待任务完成...", true, log.warn);
|
|
165
|
+
// 自定义模板
|
|
166
|
+
const progress = tracker.getProgress();
|
|
167
|
+
log.info("[{current}/{total}]: {msg}", progress.current, progress.total, "消息");
|
|
170
168
|
}
|
|
171
|
-
tracker.complete(
|
|
169
|
+
tracker.complete(`任务完成`);
|
|
172
170
|
```
|
|
173
171
|
|
|
174
172
|
### 网络请求
|
package/dist/progress.d.ts
CHANGED
|
@@ -20,36 +20,37 @@ export type Progress = {
|
|
|
20
20
|
remaining: string;
|
|
21
21
|
};
|
|
22
22
|
};
|
|
23
|
-
/**
|
|
24
|
-
export type
|
|
23
|
+
/** 进度消息格式化器 */
|
|
24
|
+
export type ProgressFormatter = (logger: typeof log.info, message: string, progress: Progress) => void;
|
|
25
25
|
export type ProgressTrackerConfig = {
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
/**
|
|
26
|
+
/** 消息格式化器 */
|
|
27
|
+
formatter?: ProgressFormatter;
|
|
28
|
+
/** 打印间隔(毫秒),默认3000 */
|
|
29
29
|
interval?: number;
|
|
30
30
|
};
|
|
31
31
|
/** 进度递进选项 */
|
|
32
32
|
export type ProgressTickOptions = {
|
|
33
|
-
/** 递进后打印消息 */
|
|
34
|
-
message?: string;
|
|
35
33
|
/** 递进的幅度 */
|
|
36
34
|
increment?: number;
|
|
35
|
+
/** 递进后打印消息 */
|
|
36
|
+
message?: string;
|
|
37
|
+
/** 强制打印 */
|
|
38
|
+
force?: boolean;
|
|
37
39
|
};
|
|
38
40
|
/** 进度追踪器 */
|
|
39
41
|
export declare class ProgressTracker {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
private readonly
|
|
42
|
+
total: number;
|
|
43
|
+
current: number;
|
|
44
|
+
startTime: number;
|
|
45
|
+
private readonly formatter;
|
|
44
46
|
private readonly interval;
|
|
45
47
|
private lastPrintTime;
|
|
46
48
|
constructor(total: number, config?: ProgressTrackerConfig);
|
|
47
|
-
private readonly
|
|
48
|
-
tick(options?: ProgressTickOptions):
|
|
49
|
-
track(callback: (progress: Progress, shouldPrint: () => boolean, printed: () => void) => void): void;
|
|
49
|
+
private readonly defaultFormatter;
|
|
50
|
+
tick(options?: ProgressTickOptions): boolean;
|
|
50
51
|
complete(message: string): void;
|
|
51
52
|
reset(): void;
|
|
52
|
-
|
|
53
|
+
print(message: string, force?: boolean, logger?: typeof log.info): void;
|
|
53
54
|
private shouldPrint;
|
|
54
55
|
private printed;
|
|
55
56
|
getProgress(): Progress;
|
package/dist/progress.js
CHANGED
|
@@ -4,43 +4,37 @@ export class ProgressTracker {
|
|
|
4
4
|
total = 0;
|
|
5
5
|
current = 0;
|
|
6
6
|
startTime = Date.now();
|
|
7
|
-
|
|
7
|
+
formatter;
|
|
8
8
|
interval;
|
|
9
9
|
lastPrintTime = 0;
|
|
10
10
|
constructor(total, config) {
|
|
11
|
-
const {
|
|
11
|
+
const { formatter, interval = 3000 } = config || {};
|
|
12
12
|
this.total = total;
|
|
13
|
-
this.
|
|
14
|
-
this.interval =
|
|
13
|
+
this.formatter = formatter || this.defaultFormatter;
|
|
14
|
+
this.interval = interval;
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
defaultFormatter = (logger, message, progress) => {
|
|
17
|
+
logger("[🚧 {pct} ⏳ {eta}]: {msg}", progress.formatted.percentage.padStart(6), progress.current > 0 && progress.elapsed > 0 ? progress.formatted.remaining : "--:--:--", message);
|
|
18
18
|
};
|
|
19
19
|
tick(options) {
|
|
20
|
-
const { message,
|
|
20
|
+
const { increment = 1, message, force = false } = options || {};
|
|
21
21
|
this.current = Math.min(this.current + increment, this.total);
|
|
22
22
|
if (message)
|
|
23
|
-
this.print(message);
|
|
24
|
-
|
|
25
|
-
track(callback) {
|
|
26
|
-
const progress = this.getProgress();
|
|
27
|
-
const shouldPrint = this.shouldPrint.bind(this);
|
|
28
|
-
const printed = this.printed.bind(this);
|
|
29
|
-
callback(progress, shouldPrint, printed);
|
|
23
|
+
this.print(message, force);
|
|
24
|
+
return this.current === this.total;
|
|
30
25
|
}
|
|
31
26
|
complete(message) {
|
|
32
27
|
this.current = this.total;
|
|
33
|
-
|
|
34
|
-
this.print(message, true);
|
|
28
|
+
this.print(message, true);
|
|
35
29
|
}
|
|
36
30
|
reset() {
|
|
37
31
|
this.current = 0;
|
|
38
32
|
this.startTime = Date.now();
|
|
39
33
|
this.lastPrintTime = 0;
|
|
40
34
|
}
|
|
41
|
-
print(message, force = false) {
|
|
35
|
+
print(message, force = false, logger = log.info) {
|
|
42
36
|
if (force || this.shouldPrint()) {
|
|
43
|
-
this.logger
|
|
37
|
+
this.formatter(logger, message, this.getProgress());
|
|
44
38
|
this.printed();
|
|
45
39
|
}
|
|
46
40
|
}
|