Skip to content

Commit

Permalink
Merge pull request #29 from tshino/repeat-playback-command
Browse files Browse the repository at this point in the history
Add 'Repeat Playback' command
  • Loading branch information
tshino authored Jan 3, 2022
2 parents c7d5b29 + c5851a1 commit 8b77cf9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
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

0 comments on commit 8b77cf9

Please sign in to comment.