From 985d3948723dc3c10216fa6c8d99849951be088d Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Mon, 18 Jan 2016 00:05:23 +0100 Subject: [PATCH] refactored to support --right-of --- .editorconfig | 14 ++++++++ .jshintrc | 22 ++++++++++++ README.md | 20 ++++++----- TODO.md | 3 -- index.js | 94 +++++++++++++++++++++++++++++++++------------------ 5 files changed, 109 insertions(+), 44 deletions(-) create mode 100644 .editorconfig create mode 100644 .jshintrc delete mode 100644 TODO.md diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..2d7bfbc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +trim_trailing_whitespace = true + +[**.{js,json,md}] +insert_final_newline = true diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..aabd4b7 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,22 @@ +{ + "node": true, + "browser": false, + "esnext": true, + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "immed": true, + "indent": 4, + "latedef": true, + "newcap": true, + "noarg": true, + "quotmark": "single", + "regexp": true, + "undef": true, + "unused": true, + "strict": true, + "trailing": true, + "smarttabs": true, + "globals" : {} +} diff --git a/README.md b/README.md index 4a65f2a..7de5857 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,17 @@ Simple helper for turning on/off connected/disconnected monitors with `xrandr`. -It uses `xrandr` to detect on which connectors there are monitors connected. It -then runs `xrandr` and sets all disconnected monitors to `off` and all connected -monitors to `on`. +## Usage -Monitors will be turned on with xrandr `--auto` option which tries to detect -the optimal video setting. +`swm [monitor-1...montior-n]` e.g. `swm LVDS1 HDMI1` -If there are multiple monitors connected, each monitor is placed to the right -of the previous monitor. This order is currently driven as xrandr lists the -connectors. +If `monitor-1` to `monitor-n` is specified `swm` will turn on these monitors +and place them from left to right in the order given. If a provided monitor is +not connected it will be skipped. + +If no monitors are specified all connected monitors will be turned on and +placed from left to right in alphabetical order of their name. + +## Requirements + +Node.js > 4.x on your PATH. diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 22f9128..0000000 --- a/TODO.md +++ /dev/null @@ -1,3 +0,0 @@ -# TODO - -- use `--leftOf` to align monitors in the order given diff --git a/index.js b/index.js index e9aed35..adec589 100755 --- a/index.js +++ b/index.js @@ -1,25 +1,9 @@ #!/usr/bin/env node -const exec = require('child_process').exec; +'use strict'; + 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 ""] - -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._; +const exec = require('child_process').exec; function getDevices() { return new Promise((resolve, reject) => { @@ -33,22 +17,47 @@ function getDevices() { }); } -function getDeviceStatus(deviceKey, device) { - if (monitorsSelected.indexOf(deviceKey) > -1 && device.connected) { - return '--auto'; - } - return '--off'; -} +function generateXrandrOptions(monitorList, devices) { + let xrandrOptions = ''; + let deviceOrder = Object.keys(devices).sort(); -function genXrandrOptions(devices) { - var xrandrOptions = ''; - - Object.keys(devices).forEach(deviceKey => { - const device = devices[deviceKey]; - const deviceStatus = getDeviceStatus(deviceKey, device); - xrandrOptions += ['', '--output', deviceKey, deviceStatus].join(' '); + // remove explicitly selected monitors inside the array and add them to the + // beginning in the order they have been specified. + monitorList.reverse().forEach((monitor) => { + const index = deviceOrder.indexOf(monitor); + if (index < 0) { + console.error('Unkown monitor', monitor, '(ignored)'); + return; + } + deviceOrder.splice(index, 1); + deviceOrder.unshift(monitor); }); + let prevDevice; + deviceOrder.forEach(deviceKey => { + const device = devices[deviceKey]; + + const deviceStatus = device.connected ? '--auto' : '--off'; + const monitorOptions = ['', '--output', deviceKey, deviceStatus]; + + if (device.connected) { + if (prevDevice) { + monitorOptions.push(['--right-of', prevDevice].join(' ')); + } + + prevDevice = deviceKey; + } + xrandrOptions += monitorOptions.join(' '); + }); + + console.log(xrandrOptions); + + + // sanity check if at least one monitor is on + if (xrandrOptions.indexOf('--auto') === -1) { + throw 'Non of the given monitors are connected, aborting...'; + } + return xrandrOptions; } @@ -80,8 +89,27 @@ function executePostCmd() { }); } +if (argv.help || argv.h) { + console.log( +`Simple helper for turning on/off connected/disconnected monitors with 'xrandr'. + +Usage: + +'swm [monitor-1...montior-n]' e.g. 'swm LVDS1 HDMI1' + +If 'monitor-1' to 'monitor-n' is specified 'swm' will turn on these monitors +and place them from left to right in the order given. If a provided monitor is +not connected it will be skipped. + +If no monitors are specified all connected monitors will be turned on and +placed from left to right in alphabetical order of their name.` + ); + + process.exit(2); +} + getDevices() - .then(genXrandrOptions) + .then(generateXrandrOptions.bind(null, argv._)) .then(switchDevices) .then(executePostCmd) .catch(err => {