Skip to content

Commit 9644114

Browse files
committed
(#371) Refined logging for mouse, keyboard and clipboard
1 parent 2dee537 commit 9644114

File tree

4 files changed

+38
-43
lines changed

4 files changed

+38
-43
lines changed

Diff for: lib/clipboard.class.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ export class ClipboardClass {
1414
* {@link setContent} copies a given text to the system clipboard
1515
* @param text The text to copy
1616
*/
17-
public setContent(text: string): Promise<void> {
18-
this.providerRegistry.getLogProvider().debug(`Saving to clipboard`);
19-
return this.providerRegistry.getClipboard().copy(text);
17+
public async setContent(text: string): Promise<void> {
18+
await this.providerRegistry.getClipboard().copy(text);
19+
this.providerRegistry.getLogProvider().debug(`Saved to clipboard`);
2020
}
2121

2222
/**
2323
* {@link getContent} returns the current content of the system clipboard (limited to text)
2424
*/
25-
public getContent(): Promise<string> {
26-
this.providerRegistry.getLogProvider().debug(`Fetching clipboard content`);
27-
return this.providerRegistry.getClipboard().paste();
25+
public async getContent(): Promise<string> {
26+
const content = await this.providerRegistry.getClipboard().paste();
27+
this.providerRegistry.getLogProvider().debug(`Fetched clipboard content`);
28+
return content;
2829
}
2930
}

Diff for: lib/keyboard.class.spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Key } from "./key.enum";
22
import { KeyboardClass } from "./keyboard.class";
33
import { ProviderRegistry } from "./provider/provider-registry.class";
44
import { mockPartial } from "sneer";
5-
import { KeyboardProviderInterface } from "./provider";
5+
import { KeyboardProviderInterface, LogProviderInterface } from "./provider";
6+
import { NoopLogProvider } from "./provider/log/noop-log-provider.class";
67

78
jest.setTimeout(10000);
89

@@ -11,6 +12,9 @@ beforeEach(() => {
1112
});
1213

1314
const providerRegistryMock = mockPartial<ProviderRegistry>({
15+
getLogProvider(): LogProviderInterface {
16+
return new NoopLogProvider();
17+
},
1418
getKeyboard(): KeyboardProviderInterface {
1519
return mockPartial<KeyboardProviderInterface>({
1620
setKeyboardDelay: jest.fn(),

Diff for: lib/keyboard.class.ts

+18
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,22 @@ export class KeyboardClass {
5656
for (const char of input.join(" ")) {
5757
await sleep(this.config.autoDelayMs);
5858
await this.providerRegistry.getKeyboard().type(char);
59+
this.providerRegistry.getLogProvider().debug(`Tapped ${char}`);
5960
}
61+
this.providerRegistry.getLogProvider().info(`Typed string ${input}`);
6062
} else {
6163
await this.providerRegistry.getKeyboard().click(...(input as Key[]));
64+
const key = input[input.length - 1];
65+
const modifiers = input.slice(0, -1);
66+
const keyName = Key[key];
67+
const modifierNames = modifiers.map((modifier) => Key[modifier]);
68+
this.providerRegistry
69+
.getLogProvider()
70+
.info(`Tapped key ${keyName} with modifiers ${modifierNames}`);
6271
}
6372
resolve(this);
6473
} catch (e) {
74+
this.providerRegistry.getLogProvider().error(e as Error);
6575
reject(e);
6676
}
6777
});
@@ -83,8 +93,11 @@ export class KeyboardClass {
8393
try {
8494
await sleep(this.config.autoDelayMs);
8595
await this.providerRegistry.getKeyboard().pressKey(...keys);
96+
const keyNames = keys.map((key) => Key[key]);
97+
this.providerRegistry.getLogProvider().info(`Pressed keys ${keyNames}`);
8698
resolve(this);
8799
} catch (e) {
100+
this.providerRegistry.getLogProvider().error(e as Error);
88101
reject(e);
89102
}
90103
});
@@ -106,8 +119,13 @@ export class KeyboardClass {
106119
try {
107120
await sleep(this.config.autoDelayMs);
108121
await this.providerRegistry.getKeyboard().releaseKey(...keys);
122+
const keyNames = keys.map((key) => Key[key]);
123+
this.providerRegistry
124+
.getLogProvider()
125+
.info(`Released keys ${keyNames}`);
109126
resolve(this);
110127
} catch (e) {
128+
this.providerRegistry.getLogProvider().error(e as Error);
111129
reject(e);
112130
}
113131
});

Diff for: lib/mouse.class.ts

+8-36
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,10 @@ export class MouseClass {
248248
try {
249249
await sleep(this.config.autoDelayMs);
250250
await this.providerRegistry.getMouse().pressButton(btn);
251+
const btnName = Button[btn];
251252
this.providerRegistry
252253
.getLogProvider()
253-
.info(
254-
`Pressed mouse button ${
255-
btn === Button.LEFT
256-
? "left"
257-
: btn === Button.MIDDLE
258-
? "middle"
259-
: "right"
260-
}`
261-
);
254+
.info(`Pressed ${btnName} mouse button`);
262255
resolve(this);
263256
} catch (e) {
264257
this.providerRegistry.getLogProvider().error(e as Error);
@@ -276,17 +269,10 @@ export class MouseClass {
276269
try {
277270
await sleep(this.config.autoDelayMs);
278271
await this.providerRegistry.getMouse().releaseButton(btn);
272+
const btnName = Button[btn];
279273
this.providerRegistry
280274
.getLogProvider()
281-
.info(
282-
`Released mouse button ${
283-
btn === Button.LEFT
284-
? "left"
285-
: btn === Button.MIDDLE
286-
? "middle"
287-
: "right"
288-
}`
289-
);
275+
.info(`Pressed ${btnName} mouse button`);
290276
resolve(this);
291277
} catch (e) {
292278
this.providerRegistry.getLogProvider().error(e as Error);
@@ -304,17 +290,10 @@ export class MouseClass {
304290
try {
305291
await sleep(this.config.autoDelayMs);
306292
await this.providerRegistry.getMouse().click(btn);
293+
const btnName = Button[btn];
307294
this.providerRegistry
308295
.getLogProvider()
309-
.info(
310-
`Clicked ${
311-
btn === Button.LEFT
312-
? "left"
313-
: btn === Button.MIDDLE
314-
? "middle"
315-
: "right"
316-
} button`
317-
);
296+
.info(`Pressed ${btnName} mouse button`);
318297
resolve(this);
319298
} catch (e) {
320299
this.providerRegistry.getLogProvider().error(e as Error);
@@ -332,17 +311,10 @@ export class MouseClass {
332311
try {
333312
await sleep(this.config.autoDelayMs);
334313
await this.providerRegistry.getMouse().doubleClick(btn);
314+
const btnName = Button[btn];
335315
this.providerRegistry
336316
.getLogProvider()
337-
.info(
338-
`Double-clicked ${
339-
btn === Button.LEFT
340-
? "left"
341-
: btn === Button.MIDDLE
342-
? "middle"
343-
: "right"
344-
} button`
345-
);
317+
.info(`Pressed ${btnName} mouse button`);
346318
resolve(this);
347319
} catch (e) {
348320
this.providerRegistry.getLogProvider().error(e as Error);

0 commit comments

Comments
 (0)