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 swm = require('./swm.js');
|
||||||
const config = require('./config.js');
|
const config = require('./config.js');
|
||||||
const postCmd = argv.postCmd || config.postCmd;
|
const postCmd = argv.postCmd || config.postCmd;
|
||||||
|
const profile = argv.profile || argv.p;
|
||||||
|
|
||||||
if (argv.help || argv.h) {
|
if (argv.help || argv.h) {
|
||||||
console.log(
|
console.log(
|
||||||
@@ -41,8 +42,10 @@ the form of
|
|||||||
process.exit(2);
|
process.exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const devices = swm.getDevices();
|
||||||
|
|
||||||
if (argv.list || argv.l) {
|
if (argv.list || argv.l) {
|
||||||
swm.getDevices()
|
devices
|
||||||
.then(devices => {
|
.then(devices => {
|
||||||
console.log('Detected devices:\n');
|
console.log('Detected devices:\n');
|
||||||
Object.keys(devices)
|
Object.keys(devices)
|
||||||
@@ -51,8 +54,21 @@ if (argv.list || argv.l) {
|
|||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
swm.getDevices()
|
let selectedMonitors = argv._;
|
||||||
.then(swm.generateXrandrOptions.bind(null, 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.switchDevices)
|
||||||
.then(swm.executePostCmd.bind(null, postCmd))
|
.then(swm.executePostCmd.bind(null, postCmd))
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ const configPath = require('xdg').basedir.configPath('switchmon/config.json');
|
|||||||
|
|
||||||
const defaults = {
|
const defaults = {
|
||||||
postCmd: undefined,
|
postCmd: undefined,
|
||||||
|
profiles: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
let config = Object.assign({}, defaults);
|
let config = Object.assign({}, defaults);
|
||||||
|
|||||||
12
swm.js
12
swm.js
@@ -34,9 +34,19 @@ function orderDeviceKeys(selectedDevices, devices) {
|
|||||||
|
|
||||||
function setActivationFlag(selectedDevices, devices) {
|
function setActivationFlag(selectedDevices, devices) {
|
||||||
const result = {};
|
const result = {};
|
||||||
|
|
||||||
Object.keys(devices).forEach(deviceKey => {
|
Object.keys(devices).forEach(deviceKey => {
|
||||||
const device = Object.assign({}, devices[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;
|
result[deviceKey] = device;
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user