From e3054967b47b080eab34590868613fea4b91276e Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Tue, 1 Mar 2016 23:38:24 +0100 Subject: [PATCH] add support for predefined profiles --- cli.js | 22 +++++++++++++++++++--- config.js | 1 + swm.js | 12 +++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index ac732f1..c5741e9 100755 --- a/cli.js +++ b/cli.js @@ -5,6 +5,7 @@ const argv = require('minimist')(process.argv.slice(2)); const swm = require('./swm.js'); const config = require('./config.js'); const postCmd = argv.postCmd || config.postCmd; +const profile = argv.profile || argv.p; if (argv.help || argv.h) { console.log( @@ -41,8 +42,10 @@ the form of process.exit(2); } +const devices = swm.getDevices(); + if (argv.list || argv.l) { - swm.getDevices() + devices .then(devices => { console.log('Detected devices:\n'); Object.keys(devices) @@ -51,8 +54,21 @@ if (argv.list || argv.l) { process.exit(0); }); } else { - swm.getDevices() - .then(swm.generateXrandrOptions.bind(null, argv._)) + let selectedMonitors = argv._; + + if (profile) { + if (!config.profiles[profile]) { + console.error('profile', profile, 'not found in config'); + process.exit(1); + } + selectedMonitors = config.profiles[profile]; + console.log('Using profile', profile); + } + + console.log('Switching on', selectedMonitors); + + devices + .then(swm.generateXrandrOptions.bind(null, selectedMonitors)) .then(swm.switchDevices) .then(swm.executePostCmd.bind(null, postCmd)) .catch(err => { diff --git a/config.js b/config.js index 2d42c5b..c3eb621 100644 --- a/config.js +++ b/config.js @@ -4,6 +4,7 @@ const configPath = require('xdg').basedir.configPath('switchmon/config.json'); const defaults = { postCmd: undefined, + profiles: {}, }; let config = Object.assign({}, defaults); diff --git a/swm.js b/swm.js index bb180c9..35bfe8a 100644 --- a/swm.js +++ b/swm.js @@ -34,9 +34,19 @@ function orderDeviceKeys(selectedDevices, devices) { function setActivationFlag(selectedDevices, devices) { const result = {}; + Object.keys(devices).forEach(deviceKey => { const device = Object.assign({}, devices[deviceKey]); - device.activate = device.connected && (!selectedDevices.length || selectedDevices.indexOf(deviceKey) > -1 ); + const isSelected = !selectedDevices.length || selectedDevices.indexOf(deviceKey) > -1; + + if (isSelected) { + if (device.connected) { + device.activate = true; + } else { + console.error(deviceKey, 'not connected. Skipping...'); + } + } + result[deviceKey] = device; }); return result;