Skip to content

Commit

Permalink
Merge pull request #19 from tshino/add-repeat-arg
Browse files Browse the repository at this point in the history
Add 'repeat' argument to playback command
  • Loading branch information
tshino authored Dec 19, 2021
2 parents 62fab3c + 02d7247 commit dfdafe0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to the Keyboard Macro Bata extension will be documented in t
### [Unreleased]
- New
- Added Sublime Text Keymap support. [#18](https://github.com/tshino/vscode-kb-macro/issues/18)
- Added 'repeat' argument support to the playback command. [#19](https://github.com/tshino/vscode-kb-macro/pull/19)
- Update
- Updated keymap wrapper for Vz Keymap; Removed unnecessary wrappers for Vz Keymap's built-in macro feature.
- Fix
Expand Down
15 changes: 10 additions & 5 deletions src/keyboard_macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ const KeyboardMacro = function({ awaitController }) {
return ok;
};

const playback = makeGuardedCommand(async function() {
const playback = makeGuardedCommand(async function(args) {
if (!recording) {
args = (args && typeof(args) === 'object') ? args : {};
const repeat = typeof(args.repeat) === 'number' ? args.repeat : 1;
const commands = sequence.get();
for (const spec of commands) {
const ok = await invokeCommandSync(spec, 'playback');
if (!ok) {
break;
let ok = true;
for (let k = 0; k < repeat && ok; k++) {
for (const spec of commands) {
ok = await invokeCommandSync(spec, 'playback');
if (!ok) {
break;
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/suite/keyboard_macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ describe('KeybaordMacro', () => {
'end'
]);
});
it('should repeat 5 times (repeat argument)', async () => {
keyboardMacro.startRecording();
keyboardMacro.push({ command: 'internal:log' });
keyboardMacro.finishRecording();

await keyboardMacro.playback({ repeat: 5 });
assert.deepStrictEqual(logs, [
'begin',
'end',
'begin',
'end',
'begin',
'end',
'begin',
'end',
'begin',
'end'
]);
});
it('should abort playback if command execution failed', async () => {
keyboardMacro.startRecording();
keyboardMacro.push({ command: 'internal:log' });
Expand All @@ -234,6 +253,15 @@ describe('KeybaordMacro', () => {
await keyboardMacro.playback();
assert.deepStrictEqual(logs, [ 'begin', 'end' ]);
});
it('should abort playback with repeat argument if command execution failed', async () => {
keyboardMacro.startRecording();
keyboardMacro.push({ command: 'internal:log' });
keyboardMacro.push({ command: 'INVALID' });
keyboardMacro.finishRecording();

await keyboardMacro.playback({ repeat: 5 });
assert.deepStrictEqual(logs, [ 'begin', 'end' ]);
});
it('should prevent reentry', async () => {
keyboardMacro.startRecording();
keyboardMacro.push({ command: 'internal:log' });
Expand Down

0 comments on commit dfdafe0

Please sign in to comment.