@bettergi/utils 0.1.13 → 0.1.15

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
@@ -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({ 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
- // });
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/ocr.d.ts CHANGED
@@ -5,17 +5,18 @@ export type ROInstance = InstanceType<typeof RecognitionObject>;
5
5
  export type ROConfig = Partial<{
6
6
  [K in keyof ROInstance as ROInstance[K] extends Function ? never : K]: ROInstance[K];
7
7
  }>;
8
+ export type ImageMat = ReturnType<typeof file.readImageMatSync>;
8
9
  export type MatchDirection = "north" /** 上半边 */ | "north-east" /** 右上四分之一 */ | "east" /** 右半边 */ | "south-east" /** 右下四分之一 */ | "south" /** 下半边 */ | "south-west" /** 左下四分之一 */ | "west" /** 左半边 */ | "north-west"; /** 左上四分之一 */
9
10
  /**
10
11
  * 在整个画面内搜索图片
11
- * @param path 图片路径
12
+ * @param image 图片路径 或 图片矩阵
12
13
  * @param config 识别对象配置
13
14
  * @returns 如果找到匹配的图片区域,则返回该区域
14
15
  */
15
- export declare const findImage: (path: string, config?: ROConfig) => Region | undefined;
16
+ export declare const findImage: (image: string | ImageMat, config?: ROConfig) => Region | undefined;
16
17
  /**
17
18
  * 在指定区域内搜索图片
18
- * @param path 图片路径
19
+ * @param image 图片路径 或 图片矩阵
19
20
  * @param x 水平方向偏移量(像素)
20
21
  * @param y 垂直方向偏移量(像素)
21
22
  * @param w 宽度
@@ -23,10 +24,10 @@ export declare const findImage: (path: string, config?: ROConfig) => Region | un
23
24
  * @param config 识别对象配置
24
25
  * @returns 如果找到匹配的图片区域,则返回该区域
25
26
  */
26
- export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number, config?: ROConfig) => Region | undefined;
27
+ export declare const findImageWithinBounds: (image: string | ImageMat, x: number, y: number, w: number, h: number, config?: ROConfig) => Region | undefined;
27
28
  /**
28
29
  * 在指定坐标范围内搜索图片
29
- * @param path 图片路径
30
+ * @param image 图片路径 或 图片矩阵
30
31
  * @param left 左边界偏移量(像素)
31
32
  * @param top 上边界偏移量(像素)
32
33
  * @param right 右边界偏移量(像素)
@@ -34,15 +35,15 @@ export declare const findImageWithinBounds: (path: string, x: number, y: number,
34
35
  * @param config 识别对象配置
35
36
  * @returns 如果找到匹配的图片区域,则返回该区域
36
37
  */
37
- export declare const findImageBetweenCoordinates: (path: string, left: number, top: number, right: number, bottom: number, config?: ROConfig) => Region | undefined;
38
+ export declare const findImageBetweenCoordinates: (image: string | ImageMat, left: number, top: number, right: number, bottom: number, config?: ROConfig) => Region | undefined;
38
39
  /**
39
40
  * 在指定方向上搜索图片
40
- * @param path 图片路径
41
+ * @param image 图片路径 或 图片矩阵
41
42
  * @param direction 搜索方向
42
43
  * @param config 识别对象配置
43
44
  * @returns 如果找到匹配的图片区域,则返回该区域
44
45
  */
45
- export declare const findImageInDirection: (path: string, direction: MatchDirection, config?: ROConfig) => Region | undefined;
46
+ export declare const findImageInDirection: (image: string | ImageMat, direction: MatchDirection, config?: ROConfig) => Region | undefined;
46
47
  /** 文本搜索选项 */
47
48
  export type TextMatchOptions = {
48
49
  /** 是否忽略大小写(默认: 是) */
package/dist/ocr.js CHANGED
@@ -23,14 +23,15 @@ const directionToBounds = (direction) => {
23
23
  };
24
24
  /**
25
25
  * 在整个画面内搜索图片
26
- * @param path 图片路径
26
+ * @param image 图片路径 或 图片矩阵
27
27
  * @param config 识别对象配置
28
28
  * @returns 如果找到匹配的图片区域,则返回该区域
29
29
  */
30
- export const findImage = (path, config = {}) => {
30
+ export const findImage = (image, config = {}) => {
31
31
  const ir = captureGameRegion();
32
32
  try {
33
- const ro = RecognitionObject.templateMatch(file.readImageMatSync(path));
33
+ const mat = typeof image === "string" ? file.readImageMatSync(image) : image;
34
+ const ro = RecognitionObject.templateMatch(mat);
34
35
  Object.assign(ro, config);
35
36
  return findFirst(ir, ro, region => region.isExist());
36
37
  }
@@ -43,7 +44,7 @@ export const findImage = (path, config = {}) => {
43
44
  };
44
45
  /**
45
46
  * 在指定区域内搜索图片
46
- * @param path 图片路径
47
+ * @param image 图片路径 或 图片矩阵
47
48
  * @param x 水平方向偏移量(像素)
48
49
  * @param y 垂直方向偏移量(像素)
49
50
  * @param w 宽度
@@ -51,10 +52,11 @@ export const findImage = (path, config = {}) => {
51
52
  * @param config 识别对象配置
52
53
  * @returns 如果找到匹配的图片区域,则返回该区域
53
54
  */
54
- export const findImageWithinBounds = (path, x, y, w, h, config = {}) => {
55
+ export const findImageWithinBounds = (image, x, y, w, h, config = {}) => {
55
56
  const ir = captureGameRegion();
56
57
  try {
57
- const ro = RecognitionObject.templateMatch(file.readImageMatSync(path), x, y, w, h);
58
+ const mat = typeof image === "string" ? file.readImageMatSync(image) : image;
59
+ const ro = RecognitionObject.templateMatch(mat, x, y, w, h);
58
60
  Object.assign(ro, config);
59
61
  return findFirst(ir, ro, region => region.isExist());
60
62
  }
@@ -67,7 +69,7 @@ export const findImageWithinBounds = (path, x, y, w, h, config = {}) => {
67
69
  };
68
70
  /**
69
71
  * 在指定坐标范围内搜索图片
70
- * @param path 图片路径
72
+ * @param image 图片路径 或 图片矩阵
71
73
  * @param left 左边界偏移量(像素)
72
74
  * @param top 上边界偏移量(像素)
73
75
  * @param right 右边界偏移量(像素)
@@ -75,19 +77,19 @@ export const findImageWithinBounds = (path, x, y, w, h, config = {}) => {
75
77
  * @param config 识别对象配置
76
78
  * @returns 如果找到匹配的图片区域,则返回该区域
77
79
  */
78
- export const findImageBetweenCoordinates = (path, left, top, right, bottom, config = {}) => {
79
- return findImageWithinBounds(path, left, top, right - left, bottom - top, config);
80
+ export const findImageBetweenCoordinates = (image, left, top, right, bottom, config = {}) => {
81
+ return findImageWithinBounds(image, left, top, right - left, bottom - top, config);
80
82
  };
81
83
  /**
82
84
  * 在指定方向上搜索图片
83
- * @param path 图片路径
85
+ * @param image 图片路径 或 图片矩阵
84
86
  * @param direction 搜索方向
85
87
  * @param config 识别对象配置
86
88
  * @returns 如果找到匹配的图片区域,则返回该区域
87
89
  */
88
- export const findImageInDirection = (path, direction, config = {}) => {
90
+ export const findImageInDirection = (image, direction, config = {}) => {
89
91
  const { x, y, w, h } = directionToBounds(direction);
90
- return findImageWithinBounds(path, x, y, w, h, config);
92
+ return findImageWithinBounds(image, x, y, w, h, config);
91
93
  };
92
94
  /**
93
95
  * 在整个画面内搜索文本
@@ -20,36 +20,37 @@ export type Progress = {
20
20
  remaining: string;
21
21
  };
22
22
  };
23
- /** 进度日志记录器 */
24
- export type ProgressLogger = (message: string, progress: Progress) => void;
23
+ /** 进度消息格式化器 */
24
+ export type ProgressFormatter = (logger: typeof log.info, message: string, progress: Progress) => void;
25
25
  export type ProgressTrackerConfig = {
26
- /** 日志记录器 */
27
- logger?: ProgressLogger;
28
- /** 节流间隔(毫秒),默认3000 */
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
- private total;
41
- private current;
42
- private startTime;
43
- private readonly logger;
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 defaultLogger;
48
- tick(options?: ProgressTickOptions): void;
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
- private print;
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
- logger;
7
+ formatter;
8
8
  interval;
9
9
  lastPrintTime = 0;
10
10
  constructor(total, config) {
11
- const { logger, interval: throttleInterval = 3000 } = config || {};
11
+ const { formatter, interval = 3000 } = config || {};
12
12
  this.total = total;
13
- this.logger = logger || this.defaultLogger;
14
- this.interval = throttleInterval;
13
+ this.formatter = formatter || this.defaultFormatter;
14
+ this.interval = interval;
15
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);
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, increment = 1 } = options || {};
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
- if (message)
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(message, this.getProgress());
37
+ this.formatter(logger, message, this.getProgress());
44
38
  this.printed();
45
39
  }
46
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",