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:
Thomas Ruoff
2019-11-14 08:42:52 +01:00
parent f4ccfe4429
commit 9160bd1c82
5 changed files with 2873 additions and 121 deletions

62
cli.js
View File

@@ -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);