switch to callbacks

This commit is contained in:
Thomas Ruoff
2016-03-11 14:52:32 +01:00
parent ef0b8de4e1
commit a64a14419b
2 changed files with 21 additions and 26 deletions

13
cli.js
View File

@@ -18,16 +18,16 @@ if (argv.help || argv.h) {
console.log(usage);
return;
} else if (argv.list || argv.l) {
const devices = swm.getDevices();
devices
.then(devices => {
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 {
const devices = swm.getDevices();
let selectedMonitors = argv._;
if (profile) {
@@ -41,7 +41,10 @@ if (argv.help || argv.h) {
console.log('Switching on', selectedMonitors.length ? selectedMonitors : 'all connected monitors');
devices.then(devices => {
swm.getDevices((err, devices) => {
if (err) {
throw new Error(err);
}
const xrandrOptions = swm.generateXrandrOptions(selectedMonitors, devices);
swm.switchDevices(xrandrOptions);
swm.executePostCmd(postCmd);

24
swm.js
View File

@@ -3,28 +3,20 @@
const xrandrParse = require('xrandr-parse');
const exec = require('child_process').exec;
function executeCmd(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (err, stdout, stderr) => {
if (err || stderr) {
reject(err);
return;
}
resolve(stdout);
});
});
function executeCmd(cmd, callback) {
exec(cmd, callback);
}
function getDevices() {
return executeCmd('xrandr').then(stdout => xrandrParse(stdout));
function getDevices(callback) {
executeCmd('xrandr', (err, stdout) => callback(err, xrandrParse(stdout)));
}
function switchDevices(xrandrOptions) {
return executeCmd('xrandr ' + xrandrOptions);
function switchDevices(xrandrOptions, callback) {
executeCmd('xrandr ' + xrandrOptions, callback);
}
function executePostCmd(postCmd) {
return executeCmd(postCmd);
function executePostCmd(postCmd, callback) {
executeCmd(postCmd, callback);
}
function orderDeviceKeys(selectedDevices, devices) {