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",
"type": "keydown",
"delay": 3000,
"command": "notify-send",
"args": [
"combo",
"{{ key }} {{ type }}"
],
"sudo": false
"command": "example"
},
{
"key": "key_enter",
"type": "keydown",
"command": "notify-send",
"args": [
"{{ key }}",
"{{ type }}"
],
"sudo": false
"command": "example2"
},
{
"key": "key_esc",
"type": "keyup",
"command": "notify-send",
"args": [
"{{ key }}",
"{{ type }}"
],
"sudo": false
"command": "example"
},
{
"key": "key_space",
"type": "keyhold",
"delay": 1000,
"command": "notify-send",
"command": "curl"
}
]
}
],
"commands": {
"example": {
"cmd": "notify-send",
"args": [
"{{ key }}",
"{{ type }}"
"example",
"first example"
],
"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 ACTION_KEYUP = { id: 0, action: 'keyup' };
@ -32,9 +34,7 @@ class Keyfilter {
{
type: type,
event: config.event,
command: config.command,
args: config.args,
sudo: config.sudo,
command: getCommand(config.command),
combo: config.combo,
delay: function () {
if (config.combo === undefined) {
@ -98,16 +98,15 @@ class Keyfilter {
if (key === undefined || event === undefined) {
return;
}
let result = this.replaceVariables(
let result =
// this.replaceVariables(
{
key: key,
type: event.type.action,
command: event.command,
args: event.args,
sudo: event.sudo,
delay: event.delay
}
)
// )
if (extraName !== undefined && extra !== undefined) {
result[extraName] = extra;
}
@ -124,13 +123,6 @@ class Keyfilter {
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) {
if (event.delay === undefined || event.delay === 0 || event.captured === undefined) {
return false;
@ -194,8 +186,20 @@ class Keyfilter {
isValid() {
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;

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');
}
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 + '\'');
cli.execute(filtered.command, filtered.args, filtered.sudo)
logger.info('executing command \'' + filtered.command.name + '\' registered for captured \'' + filtered.type + '\' event for \'' + filtered.key + '\' from watcher \'' + this.device + '\'');
cli.execute(filtered.command.cmd, filtered.command.args, filtered.command.sudo)
.then(logger.info)
.catch(logger.error);
});