forked from mauricedb/presentation-buddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction-handlers.ts
227 lines (192 loc) · 6 KB
/
instruction-handlers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {
commands,
Position,
Selection,
TextEditorRevealType,
Uri,
window,
workspace,
} from 'vscode';
import { join, dirname } from 'path';
import {
Command,
TypeText,
OpenFile,
GoTo,
Select,
CreateFile,
Wait,
TypeTextFromFile,
TypeChunksFromFile,
IHaveAFilePath
} from './instructions';
import {
mkdirIfNotExists,
writeFileAsync,
timeout,
getDelay,
readFileAsync,
getRandomness,
splitTextIntoChunks,
getWaitAfterTyping,
getWaitInsteadOfTyping,
getSkipLinesContaining,
getWaitAfterNewLine
} from './utils';
import { setAwaiter } from './wait-for-input';
const readFileContents = async (
instruction: IHaveAFilePath
) : Promise<string> => {
if (!workspace.workspaceFolders) {
return "";
}
const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
const path = join(workspaceFolder, '.presentation-buddy', instruction.path);
return await readFileAsync(path);
};
export const typeTextFromFile = async (
instruction: TypeTextFromFile
): Promise<void> => {
const text = await readFileContents(instruction);
if (text === '') { return; }
const data = Array.from(text.split('\r\n').join('\n'));
await typeTextIntoActiveTextEditor(data, instruction.delay);
};
export const typeChunksFromFile = async (
instruction: TypeChunksFromFile
): Promise<void> => {
const text = await readFileContents(instruction);
if (text === '') { return; }
const waitInsteadOf = instruction.waitInsteadOfTyping || getWaitInsteadOfTyping();
const waitAfter = instruction.waitAfterTyping || getWaitAfterTyping();
const skipLinesContaining = instruction.skipLinesContaining || getSkipLinesContaining();
const waitAfterNewLine = instruction.waitAfterNewLine ?? getWaitAfterNewLine() ?? true;
if (waitAfterNewLine) { waitAfter.push('\n'); }
var chunks = splitTextIntoChunks(text, waitInsteadOf, waitAfter, skipLinesContaining);
for (const chunk of chunks) {
await typeTextIntoActiveTextEditor(Array.from(chunk), instruction.delay);
if (chunk.endsWith('\n')) {
await command({ type: "command", command: "cursorHome", args: [], repeat: 1 });
}
await wait({ delay: "manual", type: 'wait' });
}
};
export const typeText = async (instruction: TypeText): Promise<void> => {
const data = Array.from(instruction.text.join('\n'));
await typeTextIntoActiveTextEditor(data, instruction.delay);
};
const typeTextIntoActiveTextEditor = async (
data: string[],
delay?: number
): Promise<void> => {
const delayBetweenChars = delay || getDelay();
const randomness = Math.min(delayBetweenChars, getRandomness());
const editor = window.activeTextEditor;
if (!editor) {
return;
}
if (!editor.selection.isEmpty) {
await editor.edit((editorBuilder) =>
editorBuilder.delete(editor.selection)
);
}
let char = data.shift();
let pos = editor.selection.start;
while (char) {
await editor.edit((editBuilder) => {
editor.selection = new Selection(pos, pos);
editBuilder.insert(editor.selection.active, char!);
if (char === '\n') {
pos = new Position(pos.line + 1, pos.character);
// scroll to current line if needed
editor.revealRange(editor.selection, TextEditorRevealType.Default);
} else {
pos = new Position(pos.line, pos.character + 1);
}
});
char = data.shift();
// give me a random number between -randomness and +randomness
const randomDelay =
delayBetweenChars +
(Math.floor(Math.random() * randomness * 2) - randomness);
delay === 0 ? void 0 : await timeout(randomDelay);
}
};
export const command = async (instruction: Command): Promise<void> => {
const { args = [], repeat = 1 } = instruction;
for (let index = 0; index < repeat; index++) {
await commands.executeCommand(instruction.command, ...args);
await timeout(getDelay());
}
};
export const openFile = async (instruction: OpenFile): Promise<void> => {
if (!workspace.workspaceFolders) {
return;
}
const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
const uri = Uri.file(join(workspaceFolder, instruction.path));
await commands.executeCommand('vscode.open', uri);
await timeout(getDelay());
};
export const createFile = async (instruction: CreateFile): Promise<void> => {
if (!workspace.workspaceFolders) {
return;
}
const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
const path = join(workspaceFolder, instruction.path);
await mkdirIfNotExists(dirname(path));
await writeFileAsync(path);
const uri = Uri.file(join(workspaceFolder, instruction.path));
await commands.executeCommand('vscode.open', uri);
await timeout(getDelay());
};
export const goto = async (instruction: GoTo): Promise<void> => {
const { line = 1, column = 1 } = instruction;
const editor = window.activeTextEditor;
if (!editor) {
return;
}
const position = new Position(line - 1, column - 1);
editor.selection = new Selection(position, position);
editor.revealRange(
editor.selection,
TextEditorRevealType.InCenterIfOutsideViewport
);
await timeout(getDelay());
};
export const select = async (instruction: Select): Promise<void> => {
const { line = 1, column = 1 } = instruction;
const editor = window.activeTextEditor;
if (!editor) {
return;
}
const end = new Position(line - 1, column - 1);
editor.selection = new Selection(editor.selection.start, end);
editor.revealRange(
editor.selection,
TextEditorRevealType.InCenterIfOutsideViewport
);
await timeout(getDelay());
};
export const wait = (instruction: Wait): Promise<void> => {
return new Promise(async (resolve, reject) => {
if (instruction.save) {
await command({
type: 'command',
command: 'workbench.action.files.saveAll',
args: [],
repeat: 1,
});
}
if (typeof instruction.delay === 'number') {
await timeout(instruction.delay);
resolve();
} else if (instruction.delay === 'manual') {
setAwaiter(() => {
resolve();
});
} else {
reject();
}
});
};