switch to node.js

This commit is contained in:
Thomas Ruoff
2016-01-15 00:45:52 +01:00
parent 6dca960958
commit d452fefc9c
4 changed files with 117 additions and 84 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules/

89
index.js Executable file
View File

@@ -0,0 +1,89 @@
#!/usr/bin/env node
const exec = require('child_process').exec;
const argv = require('minimist')(process.argv.slice(2));
const xrandrParse = require('xrandr-parse');
if (argv.help || argv.h) {
console.log(
`swm - a helper to switch connected monitors
usage: swm [monitor1 [monitor2]...] [--postCmd "<some commmand>"]
If monitor is not passed it turns on all connected devices
and lays them out next to each other in the order detected.
monitor: The string as reported by the script. If provided only
this monitor will be turned on.`
);
return;
}
const monitorsSelected = argv._;
function getDevices() {
return new Promise((resolve, reject) => {
exec('xrandr', (err, stdout, stderr) => {
if (err || stderr) {
reject(err);
return;
}
resolve(xrandrParse(stdout));
});
});
}
function getDeviceStatus(deviceKey, device) {
if (monitorsSelected.indexOf(deviceKey) > -1 && device.connected) {
return '--auto';
}
return '--off';
}
function genXrandrOptions(devices) {
var xrandrOptions = '';
Object.keys(devices).forEach(deviceKey => {
const device = devices[deviceKey];
const deviceStatus = getDeviceStatus(deviceKey, device);
xrandrOptions += ['', '--output', deviceKey, deviceStatus].join(' ');
});
return xrandrOptions;
}
function switchDevices(xrandrOptions) {
return new Promise((resolve, reject) => {
const cmd = 'xrandr ' + xrandrOptions;
exec(cmd, (err, stdout, stderr) => {
if (err || stderr) {
reject(err);
return;
}
resolve();
});
});
}
function executePostCmd() {
if (!argv.postCmd) {
return;
}
return new Promise((resolve, reject) => {
exec(argv.postCmd, (err, stdout, stderr) => {
if (err || stderr) {
reject(err);
return;
}
resolve();
});
});
}
getDevices()
.then(genXrandrOptions)
.then(switchDevices)
.then(executePostCmd)
.catch(err => {
console.error(err);
});

27
package.json Normal file
View File

@@ -0,0 +1,27 @@
{
"name": "switchmon",
"version": "1.0.0",
"description": "Simple helper for turning on/off connected/disconnected monitors with xrandr",
"main": "indes.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tomru/switchmon.git"
},
"keywords": [
"xrandr",
"monitor"
],
"author": "Thomas Ruoff",
"license": "MIT",
"bugs": {
"url": "https://github.com/tomru/switchmon/issues"
},
"homepage": "https://github.com/tomru/switchmon#readme",
"dependencies": {
"minimist": "^1.2.0",
"xrandr-parse": "^0.1.1"
}
}

84
swm
View File

@@ -1,84 +0,0 @@
#!/usr/bin/env bash
#
# Enable all connected montiors, disable all disconnected ones.
# Multiple monitors will be added to the right of previous ones.
#
# Author Thomas Ruoff <ThomasRuoff@gmail.com>
set -e
usage() {
echo "swm - a helper to switch connected monitors";
echo;
echo "usage: swm [monitor]";
echo;
echo "If monitor is not passed it turns on all connected devices";
echo "and lays them out next to each other in the order detected.";
echo;
echo "monitor: The string as reported by the script. If provided only";
echo "this monitor will be turned on.";
}
echoerr() { echo "$@" 1>&2; }
getFirstWord() {
awk '{print $1}'
}
linesToWords() {
awk 'BEGIN{ORS=" "} {print}'
}
wordsToLines() {
awk 'BEGIN{RS=" "} {print}'
}
without() {
comm -3 <(echo "$1") <(echo "$2")
}
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]] ; then
usage
exit
fi
TARGET=$1;
CONNECTED=$( xrandr | grep " connected" | sort | getFirstWord);
DISCONNECTED=$( xrandr | grep "disconnected" | sort | getFirstWord);
POSTCMD="herbstclient reload";
echo connected devices: $(echo $CONNECTED | wordsToLines);
echo disconnected devices: $(echo $DISCONNECTED | wordsToLines);
if [[ -n "$TARGET" ]] && [[ ! "$CONNECTED" =~ "$TARGET" ]] ; then
echoerr "error: device $1 is not connected";
exit 1;
fi
FORON=${@-$CONNECTED};
FOROFF="$DISCONNECTED $(without "$CONNECTED" "$FORON")";
# turn off all disconnected monitors
XRANDR_OFF_OPTIONS="";
for mon in $FOROFF; do
XRANDR_OFF_OPTIONS+=" --output $mon --off";
done
# turn on all connected monitors
XRANDR_ON_OPTIONS="";
LAST="";
for mon in $FORON; do
XRANDR_ON_OPTIONS+=" --output $mon --auto";
if [ ! -z $LAST ]; then
XRANDR_ON_OPTIONS+=" --right-of $LAST";
fi
LAST=$mon;
done
xrandr $XRANDR_ON_OPTIONS $XRANDR_OFF_OPTIONS &&\
echo Activated monitors: ${FORON} &&\
$POSTCMD;