mirror of
https://github.com/tomru/switchmon.git
synced 2026-03-03 14:37:27 +01:00
major refactoring - well almost a rewrite
* changed the output of --list command to be more sane * use promises and simplify API of swm.js * simplify swm.js a bit
This commit is contained in:
62
cli.js
62
cli.js
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
const argv = require('minimist')(process.argv.slice(2));
|
||||
@@ -10,51 +11,24 @@ const usage = require('./usage.js');
|
||||
const postCmd = argv.postCmd || config.postCmd;
|
||||
const profile = argv.profile || argv.p;
|
||||
|
||||
function connectionStatus(device) {
|
||||
return device.connected ? 'Connected' : 'Disconnected';
|
||||
}
|
||||
|
||||
if (argv.help || argv.h) {
|
||||
console.log(usage);
|
||||
return;
|
||||
} else if (argv.list || argv.l) {
|
||||
const devices = swm.getDevices((err, devices) => {
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
console.log('Detected devices:\n');
|
||||
Object.keys(devices)
|
||||
.sort(key => !devices[key].connected)
|
||||
.forEach(key =>
|
||||
console.log(key + ':', connectionStatus(devices[key]))
|
||||
);
|
||||
});
|
||||
} else {
|
||||
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.length ? selectedMonitors : 'all connected monitors'
|
||||
);
|
||||
|
||||
swm.getDevices((err, devices) => {
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
const xrandrOptions = swm.generateXrandrOptions(
|
||||
selectedMonitors,
|
||||
devices
|
||||
);
|
||||
swm.switchDevices(xrandrOptions);
|
||||
swm.executePostCmd(postCmd);
|
||||
});
|
||||
}
|
||||
|
||||
if (argv.list || argv.l) {
|
||||
swm.printDevices();
|
||||
return;
|
||||
}
|
||||
|
||||
let selectedMonitors = argv._;
|
||||
|
||||
if (profile) {
|
||||
if (!config.profiles[profile]) {
|
||||
throw Error(`profile ${profile} not found in config`);
|
||||
}
|
||||
selectedMonitors = config.profiles[profile];
|
||||
console.log('Using profile', profile);
|
||||
}
|
||||
|
||||
swm.activate(selectedMonitors, postCmd);
|
||||
|
||||
Reference in New Issue
Block a user