@bettergi/utils 0.1.12 → 0.1.13

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 CHANGED
@@ -77,28 +77,24 @@ const t4 = findTextInDirection("师傅", "east", { contains: true, ignoreCase: t
77
77
  > 对脚本开发过程中常见工作流的抽象,例如: 等待/断言 操作/元素/区域 完成/出现/消失。
78
78
 
79
79
  ```ts
80
- // 等待直到找不到 [关闭按钮] ,重试5次,每隔1秒重试一次,期间按 Esc 键
80
+ // 等待直到找不到 [关闭按钮] ,重试5次,每隔1秒重试一次(默认参数),期间按 Esc 键
81
81
  const done = await waitForAction(
82
82
  () => findImageInDirection("assets/关闭.png", "north-east") === undefined,
83
- () => keyPress("ESCAPE"),
84
- { maxAttempts: 5, retryInterval: 1000 }
83
+ () => keyPress("ESCAPE")
85
84
  );
86
85
  if (!done) throw new Error("关闭页面超时");
87
86
 
88
- // 断言 "生日" 文字区域即将出现,重试5次,每隔1秒重试一次,期间按 Esc 键
87
+ // 断言 "生日" 文字区域即将出现,重试10次,每隔1秒重试一次,期间按 Esc 键
89
88
  await assertRegionAppearing(
90
89
  () => findTextInDirection("生日", "north-west"),
91
90
  "打开派蒙菜单超时",
92
91
  () => keyPress("ESCAPE"),
93
- { maxAttempts: 5, retryInterval: 1000 }
92
+ { maxAttempts: 10, retryInterval: 1000 }
94
93
  );
95
94
 
96
- // 断言 "购买" 区域不存在,否则抛出异常,重试5次,每隔1秒重试一次,期间如果存在 "购买" 按钮则点击
95
+ // 断言 "购买" 文字区域即将消失,重试5次,每隔1秒重试一次(默认参数),期间如果存在 "购买" 按钮则点击
97
96
  const findButton = () => findTextWithinBounds("购买", 500, 740, 900, 110);
98
- await assertRegionDisappearing(findButton, "点击购买按钮超时", () => findButton()?.click(), {
99
- maxAttempts: 5,
100
- retryInterval: 1000
101
- });
97
+ await assertRegionDisappearing(findButton, "点击购买按钮超时", () => findButton()?.click());
102
98
  ```
103
99
 
104
100
  ### 鼠标操作
@@ -127,25 +123,52 @@ await mouseScrollDownLines(1, 115);
127
123
 
128
124
  ### 状态管理和持久化
129
125
 
130
- > 对象数据持久化,通过 Proxy 实现自动存储。从而可以无感知地读取/更新数据,而无需考虑如何持久化。
126
+ > 基于深度 Proxy 实现的对象数据持久化,能够在数据被修改时自动同步至文件。使开发其能够像操作普通对象一样进行数据读写,而无需关心底层的持久化细节。
131
127
 
132
128
  ```ts
133
129
  // 创建/读取存储对象,保存到存储文件 store/my-data.json 中
134
- const state = useStore<{ lastUsedTime?: number; count: number }>("my-data");
130
+ const store = useStore<{ lastUsedTime?: number; count: number }>("my-data");
135
131
  // 默认值版本
136
132
  // const state = useStoreWithDefaults("my-data", { lastUsedTime: 0, count: 0 });
137
133
 
138
- if (state?.lastUsedTime) {
139
- log.info(`欢迎回来!上次使用时间: ${state.lastUsedTime},计数器已累计至: ${state.count}`);
134
+ if (store?.lastUsedTime) {
135
+ log.info(`欢迎回来!上次使用时间: ${store.lastUsedTime},计数器已累计至: ${store.count}`);
140
136
  }
141
137
  try {
142
138
  // 模拟脚本运行期间状态的变化
143
139
  for (let i = 0; i < Math.floor(Math.random() * 100); i++) {
144
- state.count = (state.count || 0) + 1; // 自动同步保存到文件
140
+ store.count = (store.count || 0) + 1; // 自动保存到文件
145
141
  }
146
142
  } finally {
147
- state.lastUsedTime = Date.now(); // 自动同步保存到文件
143
+ store.lastUsedTime = Date.now(); // 自动保存到文件
144
+ }
145
+ ```
146
+
147
+ ### 进度追踪
148
+
149
+ > 创建进度追踪器并设置总进度,通过递进函数增加进度,开发者可以获取当前进度、当前耗时、平均耗时、预估剩余时间等数据。
150
+
151
+ ```ts
152
+ const total = 100;
153
+ // 创建进度追踪器,使用默认日志记录器(可配置),打印间隔最小3秒
154
+ const tracker = new ProgressTracker(total, { interval: 3000 });
155
+ for (let i = 0; i < total; i++) {
156
+ await sleep(Math.round(Math.random() * 200));
157
+ // 递进并尝试打印进度和消息
158
+ tracker.tick({ message: "等待任务完成..." });
159
+
160
+ // 也可以主动追踪,使用传递的进度信息自主控制打印时机和内容
161
+ // tracker.track((progress, shouldPrint, printed) => {
162
+ // if (!shouldPrint()) return;
163
+ // log.info(
164
+ // "[进度: {pct} 预计剩余时间: {eta}]: 等待任务完成...",
165
+ // progress.formatted.percentage,
166
+ // progress.formatted.remaining
167
+ // );
168
+ // printed();
169
+ // });
148
170
  }
171
+ tracker.complete(`执行完成`);
149
172
  ```
150
173
 
151
174
  ### 网络请求
package/dist/index.d.ts CHANGED
@@ -14,6 +14,8 @@ export * from "./misc";
14
14
  export * from "./mouse";
15
15
  /** 图像识别 */
16
16
  export * from "./ocr";
17
+ /** 进度追踪 */
18
+ export * from "./progress";
17
19
  /** 数据存储 */
18
20
  export * from "./store";
19
21
  /** 日期时间 */
package/dist/index.js CHANGED
@@ -14,6 +14,8 @@ export * from "./misc";
14
14
  export * from "./mouse";
15
15
  /** 图像识别 */
16
16
  export * from "./ocr";
17
+ /** 进度追踪 */
18
+ export * from "./progress";
17
19
  /** 数据存储 */
18
20
  export * from "./store";
19
21
  /** 日期时间 */
@@ -0,0 +1,56 @@
1
+ /** 进度信息 */
2
+ export type Progress = {
3
+ /** 当前进度 */
4
+ current: number;
5
+ /** 总进度 */
6
+ total: number;
7
+ /** 完成百分比 */
8
+ percentage: number;
9
+ /** 已用时间(毫秒) */
10
+ elapsed: number;
11
+ /** 平均每单位时间(毫秒) */
12
+ average: number;
13
+ /** 预计剩余时间(毫秒) */
14
+ remaining: number;
15
+ /** 格式化后的进度信息 */
16
+ formatted: {
17
+ percentage: string;
18
+ elapsed: string;
19
+ average: string;
20
+ remaining: string;
21
+ };
22
+ };
23
+ /** 进度日志记录器 */
24
+ export type ProgressLogger = (message: string, progress: Progress) => void;
25
+ export type ProgressTrackerConfig = {
26
+ /** 日志记录器 */
27
+ logger?: ProgressLogger;
28
+ /** 节流间隔(毫秒),默认3000 */
29
+ interval?: number;
30
+ };
31
+ /** 进度递进选项 */
32
+ export type ProgressTickOptions = {
33
+ /** 递进后打印消息 */
34
+ message?: string;
35
+ /** 递进的幅度 */
36
+ increment?: number;
37
+ };
38
+ /** 进度追踪器 */
39
+ export declare class ProgressTracker {
40
+ private total;
41
+ private current;
42
+ private startTime;
43
+ private readonly logger;
44
+ private readonly interval;
45
+ private lastPrintTime;
46
+ constructor(total: number, config?: ProgressTrackerConfig);
47
+ private readonly defaultLogger;
48
+ tick(options?: ProgressTickOptions): void;
49
+ track(callback: (progress: Progress, shouldPrint: () => boolean, printed: () => void) => void): void;
50
+ complete(message: string): void;
51
+ reset(): void;
52
+ private print;
53
+ private shouldPrint;
54
+ private printed;
55
+ getProgress(): Progress;
56
+ }
@@ -0,0 +1,73 @@
1
+ import { formatDurationAsClock, formatDurationAsReadable } from "./time";
2
+ /** 进度追踪器 */
3
+ export class ProgressTracker {
4
+ total = 0;
5
+ current = 0;
6
+ startTime = Date.now();
7
+ logger;
8
+ interval;
9
+ lastPrintTime = 0;
10
+ constructor(total, config) {
11
+ const { logger, interval: throttleInterval = 3000 } = config || {};
12
+ this.total = total;
13
+ this.logger = logger || this.defaultLogger;
14
+ this.interval = throttleInterval;
15
+ }
16
+ defaultLogger = (message, progress) => {
17
+ log.info("[🚧 {pct} ⏳ {eta}]: {msg}", progress.formatted.percentage.padStart(6), progress.current > 0 && progress.elapsed > 0 ? progress.formatted.remaining : "--:--:--", message);
18
+ };
19
+ tick(options) {
20
+ const { message, increment = 1 } = options || {};
21
+ this.current = Math.min(this.current + increment, this.total);
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);
30
+ }
31
+ complete(message) {
32
+ this.current = this.total;
33
+ if (message)
34
+ this.print(message, true);
35
+ }
36
+ reset() {
37
+ this.current = 0;
38
+ this.startTime = Date.now();
39
+ this.lastPrintTime = 0;
40
+ }
41
+ print(message, force = false) {
42
+ if (force || this.shouldPrint()) {
43
+ this.logger(message, this.getProgress());
44
+ this.printed();
45
+ }
46
+ }
47
+ shouldPrint() {
48
+ return Date.now() - this.lastPrintTime >= this.interval;
49
+ }
50
+ printed() {
51
+ this.lastPrintTime = Date.now();
52
+ }
53
+ getProgress() {
54
+ const percentage = this.current / this.total;
55
+ const elapsed = Date.now() - this.startTime;
56
+ const average = this.current > 0 ? elapsed / this.current : 0;
57
+ const remaining = (this.total - this.current) * average;
58
+ return {
59
+ current: this.current,
60
+ total: this.total,
61
+ percentage,
62
+ elapsed,
63
+ average,
64
+ remaining,
65
+ formatted: {
66
+ percentage: `${(percentage * 100).toFixed(1)}%`,
67
+ elapsed: formatDurationAsReadable(elapsed),
68
+ average: formatDurationAsReadable(average),
69
+ remaining: formatDurationAsClock(remaining)
70
+ }
71
+ };
72
+ }
73
+ }
package/dist/time.d.ts CHANGED
@@ -6,3 +6,23 @@ export declare const getNextDay4AM: () => Date;
6
6
  * 获取下一个(含当日)周一凌晨4点的时间
7
7
  */
8
8
  export declare const getNextMonday4AM: () => Date;
9
+ /**
10
+ * 解析时长
11
+ * @param duration 时长(毫秒)
12
+ */
13
+ export declare const parseDuration: (duration: number) => {
14
+ h: number;
15
+ m: number;
16
+ s: number;
17
+ ms: number;
18
+ };
19
+ /**
20
+ * 将时长转换为时钟字符串
21
+ * @param duration 时长(毫秒)
22
+ */
23
+ export declare const formatDurationAsClock: (duration: number) => string;
24
+ /**
25
+ * 将时长转换为可读格式
26
+ * @param duration 时长(毫秒)
27
+ */
28
+ export declare const formatDurationAsReadable: (duration: number) => string;
package/dist/time.js CHANGED
@@ -23,3 +23,35 @@ export const getNextMonday4AM = () => {
23
23
  result.setDate(now.getDate() + daysUntilNextMonday);
24
24
  return result;
25
25
  };
26
+ /**
27
+ * 解析时长
28
+ * @param duration 时长(毫秒)
29
+ */
30
+ export const parseDuration = (duration) => {
31
+ return {
32
+ h: Math.floor(duration / 3600000),
33
+ m: Math.floor((duration % 3600000) / 60000),
34
+ s: Math.floor((duration % 60000) / 1000),
35
+ ms: Math.floor(duration % 1000)
36
+ };
37
+ };
38
+ /**
39
+ * 将时长转换为时钟字符串
40
+ * @param duration 时长(毫秒)
41
+ */
42
+ export const formatDurationAsClock = (duration) => {
43
+ return Object.values(parseDuration(duration))
44
+ .slice(0, 3)
45
+ .map(num => String(num).padStart(2, "0"))
46
+ .join(":");
47
+ };
48
+ /**
49
+ * 将时长转换为可读格式
50
+ * @param duration 时长(毫秒)
51
+ */
52
+ export const formatDurationAsReadable = (duration) => {
53
+ return Object.entries(parseDuration(duration))
54
+ .filter(([, value]) => value > 0)
55
+ .map(([unit, value]) => `${value}${unit}`)
56
+ .join(" ");
57
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@bettergi/types": "^0.1.3",
37
- "rimraf": "^6.0.1",
37
+ "rimraf": "^6.1.0",
38
38
  "typescript": "^5.9.3"
39
39
  }
40
40
  }