From 2356ecd8018a78a7f28d1ae8a450f4a0bef60e5c Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 10 Mar 2022 14:05:08 +0100 Subject: [PATCH] cleaned up the command logic --- config.json | 55 ++++++++++++++++++++++++++--------------------- libs/keyfilter.js | 34 ++++++++++++++++------------- libs/watcher.js | 4 ++-- 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/config.json b/config.json index f0613a3..e5e0ced 100644 --- a/config.json +++ b/config.json @@ -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", - "args": [ - "{{ key }}", - "{{ type }}" - ], - "sudo": false + "command": "curl" } ] } - ] + ], + "commands": { + "example": { + "cmd": "notify-send", + "args": [ + "example", + "first example" + ], + "sudo": false + }, + "example2": { + "cmd": "notify-send", + "args": [ + "example2", + "second example" + ], + "sudo": false + }, + "curl": { + "cmd": "curl", + "args": [ + "https://velvettear.de" + ] + }, + "sudo": false + } } \ No newline at end of file diff --git a/libs/keyfilter.js b/libs/keyfilter.js index 904ed6c..be82ed1 100644 --- a/libs/keyfilter.js +++ b/libs/keyfilter.js @@ -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; \ No newline at end of file diff --git a/libs/watcher.js b/libs/watcher.js index bb9c12e..2825478 100644 --- a/libs/watcher.js +++ b/libs/watcher.js @@ -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); });