@d-zero/dealer 1.1.0 → 1.3.0

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.
@@ -1,3 +1,7 @@
1
+ /**
2
+ *
3
+ * @param text
4
+ */
1
5
  export declare function countDownFunctionParser(text: string): {
2
6
  id: string;
3
7
  time: number;
@@ -1,3 +1,7 @@
1
+ /**
2
+ *
3
+ * @param text
4
+ */
1
5
  export function countDownFunctionParser(text) {
2
6
  const matched = /%countdown\((?<time>\d+)\s*,\s*(?<id>[^\s),]+)(?:\s*,\s*(?<unit>m?s))?\)%/i.exec(text);
3
7
  if (!matched) {
package/dist/deal.d.ts CHANGED
@@ -1,8 +1,14 @@
1
1
  import type { DealerOptions } from './dealer.js';
2
2
  import type { LanesOptions } from './lanes.js';
3
- type Options = DealerOptions & LanesOptions & {
4
- header?: (progress: number, done: number, total: number, limit: number) => string;
5
- debug?: boolean;
3
+ export type DealOptions = DealerOptions & LanesOptions & {
4
+ readonly header?: DealHeader;
5
+ readonly debug?: boolean;
6
6
  };
7
- export declare function deal<T extends WeakKey>(items: readonly T[], setup: (process: T, update: (log: string) => void, index: number) => Promise<() => void | Promise<void>> | (() => void | Promise<void>), options?: Options): Promise<void>;
8
- export {};
7
+ export type DealHeader = (progress: number, done: number, total: number, limit: number) => string;
8
+ /**
9
+ *
10
+ * @param items
11
+ * @param setup
12
+ * @param options
13
+ */
14
+ export declare function deal<T extends WeakKey>(items: readonly T[], setup: (process: T, update: (log: string) => void, index: number) => Promise<() => void | Promise<void>> | (() => void | Promise<void>), options?: DealOptions): Promise<void>;
package/dist/deal.js CHANGED
@@ -1,6 +1,12 @@
1
1
  import { Dealer } from './dealer.js';
2
2
  import { Lanes } from './lanes.js';
3
3
  const DEBUG_ID = Number.MIN_SAFE_INTEGER;
4
+ /**
5
+ *
6
+ * @param items
7
+ * @param setup
8
+ * @param options
9
+ */
4
10
  export async function deal(items, setup, options) {
5
11
  const dealer = new Dealer(items, options);
6
12
  const lanes = new Lanes(options);
package/dist/dealer.js CHANGED
@@ -1,13 +1,13 @@
1
1
  export class Dealer {
2
+ #debug = () => { };
2
3
  #done = new WeakSet();
3
4
  #doneCount = 0;
5
+ #finish = () => { };
4
6
  #items;
5
7
  #limit;
8
+ #progress = () => { };
6
9
  #starts = new WeakMap();
7
10
  #workers = new Set();
8
- #debug = () => { };
9
- #finish = () => { };
10
- #progress = () => { };
11
11
  constructor(items, options) {
12
12
  this.#items = items;
13
13
  this.#limit = options?.limit ?? 10;
package/dist/display.d.ts CHANGED
@@ -2,6 +2,7 @@ import type { Animations, FPS } from './types.js';
2
2
  interface Options {
3
3
  animations?: Animations;
4
4
  fps?: FPS;
5
+ verbose?: boolean;
5
6
  }
6
7
  export declare class Display {
7
8
  #private;
package/dist/display.js CHANGED
@@ -19,7 +19,7 @@ export class Display {
19
19
  #stack = null;
20
20
  #startTime = Date.now();
21
21
  #timer = null;
22
- #verbose = false;
22
+ #verbose;
23
23
  constructor(options) {
24
24
  this.#animations = {
25
25
  ...animationPresets,
@@ -27,6 +27,7 @@ export class Display {
27
27
  };
28
28
  const fps = options?.fps ?? 30;
29
29
  this.#frameInterval = 1000 / fps;
30
+ this.#verbose = options?.verbose ?? false;
30
31
  process.stdout.on('resize', () => this.#resize());
31
32
  }
32
33
  close() {
@@ -46,7 +47,9 @@ export class Display {
46
47
  }
47
48
  write(...logs) {
48
49
  if (this.#verbose) {
49
- process.stdout.write(logs.map((log) => log.trim()).join('\n'));
50
+ for (const log of logs) {
51
+ process.stdout.write(this.#text(log, false) + '\n');
52
+ }
50
53
  return;
51
54
  }
52
55
  this.#stack = [...this.#debugMessages, ...logs];
@@ -85,6 +88,9 @@ export class Display {
85
88
  return text.replace(placeholder, `${displayTime}`);
86
89
  }
87
90
  #enterFrame() {
91
+ if (this.#verbose) {
92
+ return;
93
+ }
88
94
  this.#timer = setTimeout(() => this.#enterFrame(), this.#frameInterval);
89
95
  this.#write();
90
96
  }
@@ -94,17 +100,25 @@ export class Display {
94
100
  }
95
101
  this.#write();
96
102
  }
103
+ #text(text, trim = true) {
104
+ text = riffle(text, Date.now() - this.#startTime, this.#animations, this.#verbose);
105
+ text = this.#countDown(text);
106
+ text = text.replaceAll(/\r?\n/g, ' ');
107
+ if (trim) {
108
+ text = text.slice(0, process.stdout.columns);
109
+ }
110
+ return `${RESET}${text}${RESET}`;
111
+ }
97
112
  #write() {
98
113
  if (!this.#stack) {
99
114
  return;
100
115
  }
101
116
  this.#clear();
117
+ const outputBuffer = [];
102
118
  for (const stack of this.#stack) {
103
- let text = riffle(stack, Date.now() - this.#startTime, this.#animations);
104
- text = this.#countDown(text);
105
- const displayText = text.slice(0, process.stdout.columns);
106
- process.stdout.write(`${RESET}${displayText}${RESET}\n`);
119
+ outputBuffer.push(this.#text(stack));
107
120
  }
121
+ process.stdout.write(outputBuffer.join('\n') + '\n');
108
122
  this.#lastWroteLineNum = this.#stack.length;
109
123
  }
110
124
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export type { DealOptions, DealHeader } from './deal.js';
1
2
  export { deal } from './deal.js';
2
3
  export { Dealer } from './dealer.js';
3
4
  export { Lanes } from './lanes.js';
package/dist/lanes.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import type { Animations, FPS } from './types.js';
2
- type Log = [id: number, message: string];
2
+ type Log = readonly [id: number, message: string];
3
3
  type SortFunc = (a: Log, b: Log) => number;
4
4
  export type LanesOptions = {
5
- animations?: Animations;
6
- fps?: FPS;
7
- indent?: string;
8
- sort?: SortFunc;
5
+ readonly animations?: Animations;
6
+ readonly fps?: FPS;
7
+ readonly indent?: string;
8
+ readonly sort?: SortFunc;
9
+ readonly verbose?: boolean;
9
10
  };
10
11
  export declare class Lanes {
11
12
  #private;
package/dist/lanes.js CHANGED
@@ -5,35 +5,51 @@ export class Lanes {
5
5
  #indent = '';
6
6
  #logs = new Map();
7
7
  #sort = ([a], [b]) => a - b;
8
+ #verbose;
8
9
  constructor(options) {
9
10
  this.#display = new Display({
10
11
  animations: options?.animations,
11
12
  fps: options?.fps,
13
+ verbose: options?.verbose,
12
14
  });
13
15
  this.#indent = options?.indent ?? this.#indent;
14
16
  this.#sort = options?.sort ?? this.#sort;
17
+ this.#verbose = options?.verbose ?? false;
15
18
  }
16
19
  close() {
17
20
  this.#display.close();
18
21
  }
19
22
  delete(id) {
23
+ if (this.#verbose) {
24
+ return;
25
+ }
20
26
  this.#logs.delete(id);
21
27
  this.write();
22
28
  }
23
29
  header(text) {
24
30
  this.#header = text;
31
+ if (this.#verbose) {
32
+ return;
33
+ }
25
34
  this.write();
26
35
  }
27
36
  update(id, log) {
37
+ if (this.#verbose) {
38
+ this.#display.write(this.#header + ' ' + log);
39
+ return;
40
+ }
28
41
  this.#logs.set(id, log);
29
42
  this.write();
30
43
  }
31
44
  write() {
45
+ if (this.#verbose) {
46
+ return;
47
+ }
32
48
  const logs = [...this.#logs.entries()];
33
49
  logs.sort(this.#sort);
34
50
  const messages = logs.map(([, message]) => `${this.#indent}${message}`);
35
51
  if (this.#header) {
36
- messages.unshift(this.#header);
52
+ messages.unshift(...this.#header.split('\n'));
37
53
  }
38
54
  this.#display.write(...messages);
39
55
  }
package/dist/riffle.d.ts CHANGED
@@ -1,2 +1,9 @@
1
1
  import type { Animations } from './types.js';
2
- export declare function riffle(text: string, elapsed: number, animations: Animations): string;
2
+ /**
3
+ *
4
+ * @param text
5
+ * @param elapsed
6
+ * @param animations
7
+ * @param hidden
8
+ */
9
+ export declare function riffle(text: string, elapsed: number, animations: Animations, hidden?: boolean): string;
package/dist/riffle.js CHANGED
@@ -1,5 +1,16 @@
1
- export function riffle(text, elapsed, animations) {
1
+ /**
2
+ *
3
+ * @param text
4
+ * @param elapsed
5
+ * @param animations
6
+ * @param hidden
7
+ */
8
+ export function riffle(text, elapsed, animations, hidden = false) {
2
9
  for (const key of Object.keys(animations)) {
10
+ if (hidden) {
11
+ text = text.replaceAll(`%${key}%`, '');
12
+ continue;
13
+ }
3
14
  const animation = animations[key];
4
15
  if (!animation) {
5
16
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/dealer",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "A tool that provides an API and CLI for parallel processing of collections and sequential logging to standard output",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -20,10 +20,11 @@
20
20
  ],
21
21
  "scripts": {
22
22
  "build": "tsc",
23
+ "watch": "tsc --watch",
23
24
  "clean": "tsc --build --clean"
24
25
  },
25
26
  "dependencies": {
26
27
  "ansi-colors": "4.1.3"
27
28
  },
28
- "gitHead": "e8f65086bf7c316dda6667f1173da8585a5ef19c"
29
+ "gitHead": "e4fd17857e31022d121527b00fd7f009dbdb2142"
29
30
  }
package/dist/animate.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function animate(chars: string[], time: number): string | undefined;
package/dist/animate.js DELETED
@@ -1,3 +0,0 @@
1
- export function animate(chars, time) {
2
- return chars[time % chars.length];
3
- }
package/dist/logger.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { Animations, FPS } from './types.js';
2
- interface Options {
3
- animations?: Animations;
4
- fps?: FPS;
5
- }
6
- export declare class Logger {
7
- #private;
8
- constructor(options?: Options);
9
- clear(): void;
10
- close(): void;
11
- verboseMode(): void;
12
- write(...logs: string[]): void;
13
- }
14
- export {};
package/dist/logger.js DELETED
@@ -1,111 +0,0 @@
1
- import readline from 'node:readline';
2
- import c from 'ansi-colors';
3
- import { countDownFunctionParser } from './count-down-function-parser.js';
4
- import { riffle } from './riffle.js';
5
- import { writeVerbosely } from './write-verbosely.js';
6
- const animationPresets = {
7
- earth: ['🌏', '🌍', '🌎'],
8
- dots: ['. ', '.. ', '...'],
9
- block: ['▘', '▀', '▜', '▉', '▟', '▃', '▖', ' '],
10
- propeller: ['\\', '|', '/', '-'],
11
- };
12
- export class Logger {
13
- #animations;
14
- #coundDownMap = new Map();
15
- #frameInterval;
16
- #lastWroteLineNum = 0;
17
- #stack = null;
18
- #time = 0;
19
- #timer = null;
20
- #verbose = false;
21
- constructor(options) {
22
- this.#animations = {
23
- ...animationPresets,
24
- ...options?.animations,
25
- };
26
- const fps = options?.fps ?? 30;
27
- this.#frameInterval = 1000 / fps;
28
- process.stdout.on('resize', () => this.#resize());
29
- }
30
- clear() {
31
- if (this.#verbose) {
32
- return;
33
- }
34
- const cursorY = this.#lastWroteLineNum * -1;
35
- readline.moveCursor(process.stdout, 0, cursorY);
36
- readline.cursorTo(process.stdout, 0);
37
- }
38
- close() {
39
- if (this.#verbose) {
40
- return;
41
- }
42
- // Writing last stack
43
- if (this.#timer) {
44
- clearTimeout(this.#timer);
45
- this.#timer = null;
46
- }
47
- this.#write();
48
- this.#time = 0;
49
- this.#lastWroteLineNum = 0;
50
- this.#stack = null;
51
- }
52
- verboseMode() {
53
- this.#verbose = true;
54
- }
55
- write(...logs) {
56
- if (this.#verbose) {
57
- writeVerbosely(...logs);
58
- return;
59
- }
60
- this.#stack = logs;
61
- if (this.#timer) {
62
- return;
63
- }
64
- this.#run();
65
- }
66
- #countDown(text) {
67
- const parsed = countDownFunctionParser(text);
68
- if (!parsed) {
69
- return text;
70
- }
71
- const { id, time, placeholder, unit } = parsed;
72
- const currentTime = this.#coundDownMap.get(id);
73
- let displayTimeMS;
74
- if (currentTime == null) {
75
- this.#coundDownMap.set(id, Date.now());
76
- displayTimeMS = time;
77
- }
78
- else {
79
- const elapsedTime = Date.now() - currentTime;
80
- displayTimeMS = Math.max(time - elapsedTime, 0);
81
- }
82
- const displayTime = unit === 's' ? Math.round(displayTimeMS / 1000) : displayTimeMS;
83
- return text.replace(placeholder, `${displayTime}`);
84
- }
85
- #resize() {
86
- if (this.#verbose) {
87
- return;
88
- }
89
- this.#write();
90
- }
91
- #run() {
92
- this.#time += 1;
93
- this.#timer = setTimeout(() => this.#run(), this.#frameInterval);
94
- this.#write();
95
- }
96
- #write() {
97
- if (!this.#stack) {
98
- return;
99
- }
100
- this.clear();
101
- const reset = c.reset('');
102
- for (const line of this.#stack) {
103
- readline.clearLine(process.stdout, 0);
104
- let text = riffle(line, this.#time, this.#animations ?? {});
105
- text = this.#countDown(text);
106
- const displayText = text.slice(0, process.stdout.columns);
107
- process.stdout.write(`${reset}${displayText}${reset}\n`);
108
- }
109
- this.#lastWroteLineNum = this.#stack.length;
110
- }
111
- }
@@ -1 +0,0 @@
1
- export declare function writeVerbosely(...lines: string[]): void;
@@ -1,3 +0,0 @@
1
- export function writeVerbosely(...lines) {
2
- process.stdout.write(lines.map((line) => line.trim()).join('\n'));
3
- }