Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Repeat Playback' command #29

Merged
merged 3 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
All notable changes to the Keyboard Macro Bata extension will be documented in this file.

### [Unreleased]
- New
- Added a new `Keybaord Macro: Repeat Playback` command, which lets the user input a number and then repeats the macro specified times. [#29](https://github.com/tshino/vscode-kb-macro/pull/29)
- Update
- Updated keymap wrapper for Awesome Emacs Keymap (v0.37.1).
- Fix
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"onCommand:kb-macro.cancelRecording",
"onCommand:kb-macro.playback",
"onCommand:kb-macro.abortPlayback",
"onCommand:kb-macro.repeatPlayback",
"onCommand:kb-macro.wrap"
],
"main": "./src/extension.js",
Expand Down Expand Up @@ -95,6 +96,10 @@
{
"command": "kb-macro.abortPlayback",
"title": "Keyboard Macro: Abort Playback"
},
{
"command": "kb-macro.repeatPlayback",
"title": "Keyboard Macro: Repeat Playback"
}
],
"keybindings": [
Expand Down
1 change: 1 addition & 0 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function activate(context) {
registerCommand('finishRecording', keyboardMacro.finishRecording);
registerCommand('playback', keyboardMacro.playback);
registerCommand('abortPlayback', keyboardMacro.abortPlayback);
registerCommand('repeatPlayback', keyboardMacro.repeatPlayback);
registerCommand('wrap', keyboardMacro.wrap);

keyboardMacro.registerInternalCommand('internal:performType', internalCommands.performType);
Expand Down
27 changes: 25 additions & 2 deletions src/keyboard_macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const KeyboardMacro = function({ awaitController }) {
return ok;
};

const playback = makeGuardedCommand(async function(args) {
const playbackImpl = async function(args) {
if (recording) {
return;
}
Expand All @@ -163,14 +163,35 @@ const KeyboardMacro = function({ awaitController }) {
changePlaybackState(false, PlaybackStateReason.Finish);
}
}
});
};
const playback = makeGuardedCommand(playbackImpl);

const abortPlayback = async function() {
if (playing) {
shouldAbortPlayback = true;
}
};

const validatePositiveIntegerInput = function(value) {
if (value !== '' && !/^[1-9]\d*$/.test(value)) {
return 'Input a positive integer number';
}
};
const repeatPlayback = makeGuardedCommand(async function() {
if (recording) {
return;
}
const input = await vscode.window.showInputBox({
prompt: 'Input the number of times to repeat the macro',
validateInput: validatePositiveIntegerInput
});
if (input) {
await playbackImpl({
repeat: Number(input)
});
}
});

const makeCommandSpec = function(args) {
if (!args || !args.command) {
return null;
Expand Down Expand Up @@ -220,6 +241,8 @@ const KeyboardMacro = function({ awaitController }) {
push,
playback,
abortPlayback,
validatePositiveIntegerInput,
repeatPlayback,
wrap,

// testing purpose only
Expand Down
21 changes: 21 additions & 0 deletions test/suite/keyboard_macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,27 @@ describe('KeybaordMacro', () => {
assert.strictEqual(logs.length < 2 * 3 * 3, true);
});
});
describe('validatePositiveIntegerInput', () => {
const validatePositiveIntegerInput = keyboardMacro.validatePositiveIntegerInput;
it('should return undefined if input is a valid string representing positive integer', () => {
assert.strictEqual(validatePositiveIntegerInput('1'), undefined);
assert.strictEqual(validatePositiveIntegerInput('42'), undefined);
assert.strictEqual(validatePositiveIntegerInput('9999'), undefined);
});
it('should return undefined if input is empty', () => {
assert.strictEqual(validatePositiveIntegerInput(''), undefined);
});
it('should return diagnostic message if input does not reprensent an integer', () => {
assert.strictEqual(typeof validatePositiveIntegerInput('abc'), 'string');
assert.strictEqual(typeof validatePositiveIntegerInput('123m'), 'string');
assert.strictEqual(typeof validatePositiveIntegerInput('1.5'), 'string');
assert.strictEqual(typeof validatePositiveIntegerInput('1e7'), 'string');
assert.strictEqual(typeof validatePositiveIntegerInput(' '), 'string');
});
it('should return diagnostic message if input reprensents a negative integer', () => {
assert.strictEqual(typeof validatePositiveIntegerInput('-123'), 'string');
});
});
describe('wrap', () => {
const logs = [];
beforeEach(async () => {
Expand Down