mirror of
https://github.com/tomru/switchmon.git
synced 2026-03-02 22:17:23 +01:00
switch to callbacks
This commit is contained in:
23
cli.js
23
cli.js
@@ -18,16 +18,16 @@ if (argv.help || argv.h) {
|
|||||||
console.log(usage);
|
console.log(usage);
|
||||||
return;
|
return;
|
||||||
} else if (argv.list || argv.l) {
|
} else if (argv.list || argv.l) {
|
||||||
const devices = swm.getDevices();
|
const devices = swm.getDevices((err, devices) => {
|
||||||
devices
|
if (err) {
|
||||||
.then(devices => {
|
throw new Error(err);
|
||||||
console.log('Detected devices:\n');
|
}
|
||||||
Object.keys(devices)
|
console.log('Detected devices:\n');
|
||||||
.sort(key => !devices[key].connected)
|
Object.keys(devices)
|
||||||
.forEach(key => console.log(key + ':', connectionStatus(devices[key])));
|
.sort(key => !devices[key].connected)
|
||||||
});
|
.forEach(key => console.log(key + ':', connectionStatus(devices[key])));
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
const devices = swm.getDevices();
|
|
||||||
let selectedMonitors = argv._;
|
let selectedMonitors = argv._;
|
||||||
|
|
||||||
if (profile) {
|
if (profile) {
|
||||||
@@ -41,7 +41,10 @@ if (argv.help || argv.h) {
|
|||||||
|
|
||||||
console.log('Switching on', selectedMonitors.length ? selectedMonitors : 'all connected monitors');
|
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);
|
const xrandrOptions = swm.generateXrandrOptions(selectedMonitors, devices);
|
||||||
swm.switchDevices(xrandrOptions);
|
swm.switchDevices(xrandrOptions);
|
||||||
swm.executePostCmd(postCmd);
|
swm.executePostCmd(postCmd);
|
||||||
|
|||||||
24
swm.js
24
swm.js
@@ -3,28 +3,20 @@
|
|||||||
const xrandrParse = require('xrandr-parse');
|
const xrandrParse = require('xrandr-parse');
|
||||||
const exec = require('child_process').exec;
|
const exec = require('child_process').exec;
|
||||||
|
|
||||||
function executeCmd(cmd) {
|
function executeCmd(cmd, callback) {
|
||||||
return new Promise((resolve, reject) => {
|
exec(cmd, callback);
|
||||||
exec(cmd, (err, stdout, stderr) => {
|
|
||||||
if (err || stderr) {
|
|
||||||
reject(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve(stdout);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDevices() {
|
function getDevices(callback) {
|
||||||
return executeCmd('xrandr').then(stdout => xrandrParse(stdout));
|
executeCmd('xrandr', (err, stdout) => callback(err, xrandrParse(stdout)));
|
||||||
}
|
}
|
||||||
|
|
||||||
function switchDevices(xrandrOptions) {
|
function switchDevices(xrandrOptions, callback) {
|
||||||
return executeCmd('xrandr ' + xrandrOptions);
|
executeCmd('xrandr ' + xrandrOptions, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
function executePostCmd(postCmd) {
|
function executePostCmd(postCmd, callback) {
|
||||||
return executeCmd(postCmd);
|
executeCmd(postCmd, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
function orderDeviceKeys(selectedDevices, devices) {
|
function orderDeviceKeys(selectedDevices, devices) {
|
||||||
|
|||||||
Reference in New Issue
Block a user