cleaned up the command logic

This commit is contained in:
Daniel Sommer 2022-03-10 14:05:08 +01:00
parent 3e40dc85e7
commit 2356ecd801
3 changed files with 51 additions and 42 deletions

View file

@ -18,45 +18,50 @@
"event": "EV_KEY", "event": "EV_KEY",
"type": "keydown", "type": "keydown",
"delay": 3000, "delay": 3000,
"command": "notify-send", "command": "example"
"args": [
"combo",
"{{ key }} {{ type }}"
],
"sudo": false
}, },
{ {
"key": "key_enter", "key": "key_enter",
"type": "keydown", "type": "keydown",
"command": "notify-send", "command": "example2"
"args": [
"{{ key }}",
"{{ type }}"
],
"sudo": false
}, },
{ {
"key": "key_esc", "key": "key_esc",
"type": "keyup", "type": "keyup",
"command": "notify-send", "command": "example"
"args": [
"{{ key }}",
"{{ type }}"
],
"sudo": false
}, },
{ {
"key": "key_space", "key": "key_space",
"type": "keyhold", "type": "keyhold",
"delay": 1000, "delay": 1000,
"command": "notify-send", "command": "curl"
}
]
}
],
"commands": {
"example": {
"cmd": "notify-send",
"args": [ "args": [
"{{ key }}", "example",
"{{ type }}" "first example"
], ],
"sudo": false "sudo": false
} },
"example2": {
"cmd": "notify-send",
"args": [
"example2",
"second example"
],
"sudo": false
},
"curl": {
"cmd": "curl",
"args": [
"https://velvettear.de"
] ]
},
"sudo": false
} }
]
} }

View file

@ -1,3 +1,5 @@
const config = require('../config.json');
const LINE_START = 'Event: time'; const LINE_START = 'Event: time';
const ACTION_KEYUP = { id: 0, action: 'keyup' }; const ACTION_KEYUP = { id: 0, action: 'keyup' };
@ -32,9 +34,7 @@ class Keyfilter {
{ {
type: type, type: type,
event: config.event, event: config.event,
command: config.command, command: getCommand(config.command),
args: config.args,
sudo: config.sudo,
combo: config.combo, combo: config.combo,
delay: function () { delay: function () {
if (config.combo === undefined) { if (config.combo === undefined) {
@ -98,16 +98,15 @@ class Keyfilter {
if (key === undefined || event === undefined) { if (key === undefined || event === undefined) {
return; return;
} }
let result = this.replaceVariables( let result =
// this.replaceVariables(
{ {
key: key, key: key,
type: event.type.action, type: event.type.action,
command: event.command, command: event.command,
args: event.args,
sudo: event.sudo,
delay: event.delay delay: event.delay
} }
) // )
if (extraName !== undefined && extra !== undefined) { if (extraName !== undefined && extra !== undefined) {
result[extraName] = extra; result[extraName] = extra;
} }
@ -124,13 +123,6 @@ class Keyfilter {
return; return;
} }
} }
replaceVariables(filtered) {
for (let index = 0; index < filtered.args.length; index++) {
filtered.args[index] = filtered.args[index].replace(VARIABLE_KEY, filtered.key);
filtered.args[index] = filtered.args[index].replace(VARIABLE_TYPE, filtered.type);
}
return filtered;
}
shouldBeDelayed(event) { shouldBeDelayed(event) {
if (event.delay === undefined || event.delay === 0 || event.captured === undefined) { if (event.delay === undefined || event.delay === 0 || event.captured === undefined) {
return false; return false;
@ -194,8 +186,20 @@ class Keyfilter {
isValid() { isValid() {
return this.actions != undefined && this.actions.size > 0; return this.actions != undefined && this.actions.size > 0;
} }
}
function getCommand(command) {
if (command === undefined || config.commands === undefined) {
return;
}
const result = config.commands[command];
if (result === undefined) {
return;
}
result.name = command;
return result;
} }
module.exports = Keyfilter; module.exports = Keyfilter;

View file

@ -81,8 +81,8 @@ class Watcher {
logger.debug('captured \'' + filtered.combo.type.action + '\' event for \'' + filtered.combo.key + '\' from watcher \'' + this.device + '\' is the last part of a combo'); logger.debug('captured \'' + filtered.combo.type.action + '\' event for \'' + filtered.combo.key + '\' from watcher \'' + this.device + '\' is the last part of a combo');
} }
this.keyfilter.resetCurrentCombo(); this.keyfilter.resetCurrentCombo();
logger.info('executing command \'' + filtered.command + '\' (args: \'' + filtered.args + '\') registered for captured \'' + filtered.type + '\' event for \'' + filtered.key + '\' from watcher \'' + this.device + '\''); logger.info('executing command \'' + filtered.command.name + '\' registered for captured \'' + filtered.type + '\' event for \'' + filtered.key + '\' from watcher \'' + this.device + '\'');
cli.execute(filtered.command, filtered.args, filtered.sudo) cli.execute(filtered.command.cmd, filtered.command.args, filtered.command.sudo)
.then(logger.info) .then(logger.info)
.catch(logger.error); .catch(logger.error);
}); });