mirror of
https://github.com/tomru/switchmon.git
synced 2026-03-02 22:17:23 +01:00
add support for predefined profiles
This commit is contained in:
22
cli.js
22
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 => {
|
||||
|
||||
@@ -4,6 +4,7 @@ const configPath = require('xdg').basedir.configPath('switchmon/config.json');
|
||||
|
||||
const defaults = {
|
||||
postCmd: undefined,
|
||||
profiles: {},
|
||||
};
|
||||
|
||||
let config = Object.assign({}, defaults);
|
||||
|
||||
12
swm.js
12
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;
|
||||
|
||||
Reference in New Issue
Block a user