Skip to content

Commit

Permalink
Merge pull request mauricedb#1 from dylanbeattie/chunks
Browse files Browse the repository at this point in the history
Add TypeChunksFromFile for typing file content with pauses (e.g. after each line)
  • Loading branch information
dylanbeattie authored Mar 24, 2024
2 parents 8d91b32 + d9eb3b8 commit 971e5db
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 39 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

root = true

[**.{ts,json,js}]
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
5 changes: 3 additions & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"eg2.tslint"
"dbaeumer.vscode-eslint",
"ms-vscode.extension-test-runner"
]
}
}
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ This extension contributes the following settings:

- `presentation-buddy.delay`: Delay (in ms) between keys entered. Defaults to 100ms.
- `presentation-buddy.randomness`: Randomness (in ms) between keys entered. Defaults to 25ms.
- `presentation-buddy.waitInsteadOfTyping`: Default array of strings which indicate a pause when typing chunks from a file.
- `presentation-buddy.waitAfterTyping`: Default array of strings which indicate a pause when typing chunks from a file.
- `presentation-buddy.skipLinesContaining`: Default array of strings indicating lines to skip when typing chunks from a file.
- `presentation-buddy.waitAfterNewLine`: controls whether to pause after each new line when typing chunks from a file.

## Instructions

Expand Down Expand Up @@ -63,6 +67,45 @@ Example:
}
```

### TypeChunksFromFile

Type text at the current cursor position of the currently open file. Text is read from the file indicated by the path, relative to the `.presentation-buddy` folder. The input file will be split into chunks based on the supplied settings.

By default, the input will be split on newlines (`\n`). Windows-style `\r\n` line endings will be converted to `\n` before the file is processed. To disable this, set `waitAfterNewLines` to `false`.

Presentation Buddy will:

* `wait` after typing any string matching a supplied `waitAfter` argument
* `wait` **instead of** typing any string matching a supplied `waitInsteadOf` argument
* Skip any line containing any string matching a supplied `skipLinesContaining` argument

Example:

```json
{
"type":"typeChunksFromFile",
"path": "chunks-example.js",
"waitAfterTyping": [ "{", ".", " => ", " = " ],
"waitInsteadOfTyping": [ "/*WAIT*/" ],
"skipLinesContaining": [ "//skip" ]
}
```

The input file `chunks-example.js`:

```js
import { Color } from './color.js'; //skip
//skip
export class Scene {
constructor(camera, background, shapes) {
this.camera = camera;
this.background = background /*WAIT*/ ?? Color.Black;
this.shapes = shapes ?? [];
}
trace = (x, y) => this.camera.trace(this, x, y);
}
```
### OpenFile
Opens the file specified in the editor. Note that the file needs to exist.
Expand Down
40 changes: 34 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
"color": "#193549",
"theme": "dark"
},
"activationEvents": [
"onCommand:presentationBuddy.init",
"onCommand:presentationBuddy.start",
"onCommand:presentationBuddy.continue"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
Expand All @@ -50,7 +45,7 @@
}
],
"configuration": {
"title": "Presentation Buddy configuration",
"title": "Presentation Buddy",
"properties": {
"presentation-buddy.delay": {
"type": "integer",
Expand All @@ -63,6 +58,39 @@
"default": 25,
"description": "Randomness (in ms) between keys entered",
"scope": "user"
},
"presentation-buddy.waitInsteadOfTyping": {
"type": "array",
"items": {
"type": "string"
},
"default": [ "/*WAIT*/" ],
"description": "When using typeChunksFromFile, Presentation Buddy will pause when it finds any matching string. Matches are NOT copied to the output.",
"scope": "user"
},
"presentation-buddy.waitAfterNewLine": {
"type": "boolean",
"default": true,
"description": "When using typeChunksFromFile, controls whether Presentation Buddy should pause at the start of each new line.",
"scope": "user"
},
"presentation-buddy.waitAfterTyping": {
"type": "array",
"items": {
"type": "string"
},
"default": [ "(", "=" ],
"description": "When using typeChunksFromFile, Presentation Buddy will pause after typing any series of matching strings.",
"scope": "user"
},
"presentation-buddy.skipLinesContaining": {
"type": "array",
"items": {
"type": "string"
},
"default": [ "/*SKIP*/" ],
"description": "When using typeChunksFromFile, Presentation Buddy will skip any line containing any matching string.",
"scope": "user"
}
}
}
Expand Down
49 changes: 43 additions & 6 deletions src/instruction-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
CreateFile,
Wait,
TypeTextFromFile,
TypeChunksFromFile,
IHaveAFilePath
} from './instructions';
import {
mkdirIfNotExists,
Expand All @@ -26,25 +28,60 @@ import {
getDelay,
readFileAsync,
getRandomness,
splitTextIntoChunks,
getWaitAfterTyping,
getWaitInsteadOfTyping,
getSkipLinesContaining,
getWaitAfterNewLine
} from './utils';
import { setAwaiter } from './wait-for-input';

export const typeTextFromFile = async (
instruction: TypeTextFromFile
): Promise<void> => {
const readFileContents = async (
instruction: IHaveAFilePath
) : Promise<string> => {
if (!workspace.workspaceFolders) {
return;
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 readFileAsync(path);
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'));

Expand Down
15 changes: 15 additions & 0 deletions src/instructions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
type Skipable = { skip?: boolean };

export interface IHaveAFilePath {
path: string;
}

export type Command = {
type: "command";
command: string;
Expand All @@ -17,6 +21,16 @@ export type TypeTextFromFile = {
delay?: number;
};

export type TypeChunksFromFile = {
type: "typeChunksFromFile";
path: string;
delay?: number;
waitInsteadOfTyping: string[];
waitAfterTyping: string[];
waitAfterNewLine: boolean | null,
skipLinesContaining: string[];
};

export type OpenFile = { type: "openFile"; path: string };

export type CreateFile = { type: "createFile"; path: string };
Expand All @@ -31,6 +45,7 @@ export type Instruction = (
| OpenFile
| TypeText
| TypeTextFromFile
| TypeChunksFromFile
| Wait
) &
Skipable;
Expand Down
22 changes: 13 additions & 9 deletions src/presentation-buddy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { jsonc } from 'jsonc';

import { Instruction, InstructionHandler } from './instructions';
import * as instructionHandlers from './instruction-handlers';
import { mkdirIfNotExists } from './utils';
import { existsAsync, mkdirIfNotExists } from './utils';

export const init = async () => {
if (!workspace.workspaceFolders) {
Expand All @@ -18,17 +18,21 @@ export const init = async () => {

const dir = join(workspaceFolder, '.presentation-buddy');
const fileName = join(dir, 'instructions.json');

await mkdirIfNotExists(dir);

await jsonc.write(fileName, json, { space: 2 });
if (await existsAsync(fileName)) {
window.showWarningMessage(
`File ${fileName} exists: overwrite it?`, "Yes", "No"
).then(async answer => {
if (answer === "Yes") {
await jsonc.write(fileName, json, { space: 2 });
};
});
} else {
await mkdirIfNotExists(dir);
await jsonc.write(fileName, json, { space: 2 });
}
};

export const start = async () => {
// const editor = window.activeTextEditor;
// if (!editor) {
// return;
// }
if (!workspace.workspaceFolders) {
return;
}
Expand Down
Loading

0 comments on commit 971e5db

Please sign in to comment.