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

245
.eslintrc.js Normal file
View File

@@ -0,0 +1,245 @@
module.exports = {
env: {
node: true,
commonjs: true,
es6: true
},
extends: 'eslint:recommended',
parserOptions: {
ecmaVersion: 2018
},
rules: {
'accessor-pairs': 'error',
'array-bracket-newline': 'error',
'array-bracket-spacing': ['error', 'never'],
'array-callback-return': 'error',
'array-element-newline': 'off',
'arrow-body-style': 'error',
'arrow-parens': ['error', 'as-needed'],
'arrow-spacing': [
'error',
{
after: true,
before: true
}
],
'block-scoped-var': 'error',
'block-spacing': 'error',
'brace-style': ['error', '1tbs'],
'callback-return': 'error',
camelcase: 'error',
'capitalized-comments': ['error', 'never'],
'class-methods-use-this': 'error',
'comma-dangle': 'error',
'comma-spacing': [
'error',
{
after: true,
before: false
}
],
'comma-style': 'error',
complexity: 'error',
'computed-property-spacing': ['error', 'never'],
'consistent-return': 'error',
'consistent-this': 'error',
curly: 'error',
'default-case': 'error',
'default-param-last': 'error',
'dot-location': ['error', 'property'],
'dot-notation': 'error',
'eol-last': 'error',
eqeqeq: 'error',
'func-call-spacing': 'error',
'func-name-matching': 'error',
'func-names': 'error',
'func-style': ['error', 'declaration'],
'function-call-argument-newline': ['error', 'consistent'],
'function-paren-newline': 'off',
'generator-star-spacing': 'error',
'global-require': 'error',
'guard-for-in': 'error',
'handle-callback-err': 'error',
'id-blacklist': 'error',
'id-length': 'error',
'id-match': 'error',
'implicit-arrow-linebreak': ['error', 'beside'],
indent: 'error',
'indent-legacy': 'error',
'init-declarations': 'off',
'jsx-quotes': 'error',
'key-spacing': 'error',
'keyword-spacing': [
'error',
{
after: true,
before: true
}
],
'line-comment-position': 'error',
'linebreak-style': ['error', 'unix'],
'lines-around-comment': 'error',
'lines-around-directive': 'error',
'lines-between-class-members': 'error',
'max-classes-per-file': 'error',
'max-depth': 'error',
'max-len': 'error',
'max-lines': 'error',
'max-lines-per-function': 'error',
'max-nested-callbacks': 'error',
'max-params': 'error',
'max-statements': 'error',
'max-statements-per-line': 'error',
'multiline-comment-style': 'error',
'new-cap': 'error',
'new-parens': 'error',
'newline-after-var': 'off',
'newline-before-return': 'off',
'newline-per-chained-call': 'error',
'no-alert': 'error',
'no-array-constructor': 'error',
'no-await-in-loop': 'error',
'no-bitwise': 'error',
'no-buffer-constructor': 'error',
'no-caller': 'error',
'no-catch-shadow': 'error',
'no-confusing-arrow': 'error',
'no-console': 'off',
'no-continue': 'error',
'no-div-regex': 'error',
'no-duplicate-imports': 'error',
'no-else-return': 'error',
'no-empty-function': 'error',
'no-eq-null': 'error',
'no-eval': 'error',
'no-extend-native': 'error',
'no-extra-bind': 'error',
'no-extra-label': 'error',
'no-extra-parens': 'error',
'no-floating-decimal': 'error',
'no-implicit-coercion': 'error',
'no-implicit-globals': 'error',
'no-implied-eval': 'error',
'no-import-assign': 'error',
'no-inline-comments': 'error',
'no-invalid-this': 'error',
'no-iterator': 'error',
'no-label-var': 'error',
'no-labels': 'error',
'no-lone-blocks': 'error',
'no-lonely-if': 'error',
'no-loop-func': 'error',
'no-magic-numbers': 'off',
'no-mixed-operators': 'error',
'no-mixed-requires': 'error',
'no-multi-assign': 'error',
'no-multi-spaces': 'error',
'no-multi-str': 'error',
'no-multiple-empty-lines': 'error',
'no-native-reassign': 'error',
'no-negated-condition': 'off',
'no-negated-in-lhs': 'error',
'no-nested-ternary': 'error',
'no-new': 'error',
'no-new-func': 'error',
'no-new-object': 'error',
'no-new-require': 'error',
'no-new-wrappers': 'error',
'no-octal-escape': 'error',
'no-param-reassign': 'error',
'no-path-concat': 'error',
'no-plusplus': 'error',
'no-process-env': 'error',
'no-process-exit': 'error',
'no-proto': 'error',
'no-restricted-globals': 'error',
'no-restricted-imports': 'error',
'no-restricted-modules': 'error',
'no-restricted-properties': 'error',
'no-restricted-syntax': 'error',
'no-return-assign': 'error',
'no-return-await': 'error',
'no-script-url': 'error',
'no-self-compare': 'error',
'no-sequences': 'error',
'no-shadow': 'error',
'no-spaced-func': 'error',
'no-sync': 'error',
'no-tabs': 'error',
'no-template-curly-in-string': 'error',
'no-ternary': 'off',
'no-throw-literal': 'error',
'no-trailing-spaces': 'error',
'no-undef-init': 'error',
'no-undefined': 'error',
'no-underscore-dangle': 'error',
'no-unmodified-loop-condition': 'error',
'no-unneeded-ternary': 'error',
'no-unused-expressions': 'error',
'no-use-before-define': 'off',
'no-useless-call': 'error',
'no-useless-computed-key': 'error',
'no-useless-concat': 'error',
'no-useless-constructor': 'error',
'no-useless-rename': 'error',
'no-useless-return': 'error',
'no-var': 'error',
'no-void': 'error',
'no-warning-comments': 'error',
'no-whitespace-before-property': 'error',
'nonblock-statement-body-position': 'error',
'object-curly-newline': 'error',
'object-curly-spacing': ['error', 'always'],
'object-property-newline': 'error',
'object-shorthand': 'error',
'one-var': 'off',
'one-var-declaration-per-line': 'error',
'operator-assignment': ['error', 'always'],
'operator-linebreak': 'error',
'padded-blocks': 'off',
'padding-line-between-statements': 'error',
'prefer-arrow-callback': 'error',
'prefer-const': 'off',
'prefer-destructuring': 'error',
'prefer-named-capture-group': 'error',
'prefer-numeric-literals': 'error',
'prefer-object-spread': 'off',
'prefer-promise-reject-errors': 'error',
'prefer-reflect': 'error',
'prefer-regex-literals': 'error',
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'prefer-template': 'error',
'quote-props': 'error',
quotes: ['error', 'single'],
radix: 'error',
'require-await': 'error',
'require-jsdoc': 'off',
'require-unicode-regexp': 'error',
'rest-spread-spacing': 'error',
semi: 'error',
'semi-spacing': 'error',
'semi-style': ['error', 'last'],
'sort-imports': 'error',
'sort-keys': 'error',
'sort-vars': 'error',
'space-before-blocks': 'error',
'space-before-function-paren': 'off',
'space-in-parens': ['error', 'never'],
'space-infix-ops': 'error',
'space-unary-ops': 'error',
'spaced-comment': ['error', 'always'],
strict: 'error',
'switch-colon-spacing': 'error',
'symbol-description': 'error',
'template-curly-spacing': ['error', 'never'],
'template-tag-spacing': 'error',
'unicode-bom': ['error', 'never'],
'valid-jsdoc': 'error',
'vars-on-top': 'error',
'wrap-iife': 'error',
'wrap-regex': 'error',
'yield-star-spacing': 'error',
yoda: ['error', 'never']
}
};

62
cli.js
View File

@@ -1,4 +1,5 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; 'use strict';
const argv = require('minimist')(process.argv.slice(2)); const argv = require('minimist')(process.argv.slice(2));
@@ -10,51 +11,24 @@ const usage = require('./usage.js');
const postCmd = argv.postCmd || config.postCmd; const postCmd = argv.postCmd || config.postCmd;
const profile = argv.profile || argv.p; const profile = argv.profile || argv.p;
function connectionStatus(device) {
return device.connected ? 'Connected' : 'Disconnected';
}
if (argv.help || argv.h) { if (argv.help || argv.h) {
console.log(usage); console.log(usage);
return; 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);

2495
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -14,7 +14,10 @@
"type": "git", "type": "git",
"url": "git+https://github.com/tomru/switchmon.git" "url": "git+https://github.com/tomru/switchmon.git"
}, },
"keywords": ["xrandr", "monitor"], "keywords": [
"xrandr",
"monitor"
],
"author": "Thomas Ruoff", "author": "Thomas Ruoff",
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
@@ -28,6 +31,7 @@
}, },
"devDependencies": { "devDependencies": {
"coveralls": "^2.11.8", "coveralls": "^2.11.8",
"eslint": "^6.6.0",
"istanbul": "^0.4.2", "istanbul": "^0.4.2",
"mocha": "^2.4.5", "mocha": "^2.4.5",
"mocha-lcov-reporter": "^1.2.0", "mocha-lcov-reporter": "^1.2.0",

186
swm.js
View File

@@ -1,96 +1,130 @@
'use strict'; 'use strict';
const xrandrParse = require('xrandr-parse'); const xrandrParse = require('xrandr-parse');
const exec = require('child_process').exec; const util = require('util');
const exec = util.promisify(require('child_process').exec);
function executeCmd(cmd, callback) { async function getDevices() {
exec(cmd, callback); const { stdout } = await exec('xrandr');
return xrandrParse(stdout);
} }
function getDevices(callback) { function sortDeviceKeys(devices) {
executeCmd('xrandr', (err, stdout) => return (keyA, keyB) => {
callback(err, err ? null : xrandrParse(stdout)) const aConnected = devices[keyA].connected;
); const bConnected = devices[keyB].connected;
}
function switchDevices(xrandrOptions, callback) { if (!aConnected && bConnected) {
executeCmd('xrandr ' + xrandrOptions, callback); return 1;
}
function executePostCmd(postCmd, callback) {
executeCmd(postCmd, callback);
}
function orderDeviceKeys(selectedDevices, devices) {
let orderedDeviceKeys = Object.keys(devices).sort();
// fix the sort order if monitors were explicitly selected
selectedDevices.reverse().forEach(monitor => {
const index = orderedDeviceKeys.indexOf(monitor);
if (index < 0) {
console.error('Unkown monitor', monitor, '(ignored)');
return;
}
orderedDeviceKeys.splice(index, 1);
orderedDeviceKeys.unshift(monitor);
});
return orderedDeviceKeys;
}
function setActivationFlag(selectedDevices, devices) {
const result = {};
const selectByDefault = selectedDevices.length === 0;
Object.keys(devices).forEach(deviceKey => {
const device = Object.assign({}, devices[deviceKey]);
const isSelected = selectedDevices.indexOf(deviceKey) > -1;
if (isSelected || selectByDefault) {
if (device.connected) {
device.activate = true;
} else if (isSelected) {
console.error(deviceKey, 'not connected. Skipping...');
}
} }
result[deviceKey] = device; if (aConnected && !bConnected) {
}); return -1;
return result; }
return 0;
};
} }
function generateXrandrOptions(selectedDevices, rawDevices) { function getSelectedAndConnectedDevices(selectedDevices, devices) {
let xrandrOptions = ''; const selectedAndConnected = selectedDevices.filter(deviceKey => {
let prevDevice; if (!devices[deviceKey]) {
let devices = setActivationFlag(selectedDevices, rawDevices); throw new Error(`${deviceKey} is not a valid monitor`);
orderDeviceKeys(selectedDevices, devices).forEach(deviceKey => {
const device = devices[deviceKey];
const monitorOptions = ['', '--output', deviceKey];
if (!device.activate) {
monitorOptions.push('--off');
} else {
monitorOptions.push('--auto');
if (prevDevice) {
monitorOptions.push(['--right-of', prevDevice].join(' '));
}
prevDevice = deviceKey;
} }
xrandrOptions += monitorOptions.join(' ');
return devices[deviceKey].connected;
}); });
// sanity check if at least one monitor is on if (selectedDevices.length && !selectedAndConnected.length) {
if (xrandrOptions.indexOf('--auto') === -1) {
throw new Error('Non of the given monitors are connected, aborting...'); throw new Error('Non of the given monitors are connected, aborting...');
} }
return selectedAndConnected;
}
function getXrandrOptions(
deviceKeys,
devices,
positionParameter = '--right-of'
) {
return deviceKeys.reduce((acc, deviceKey, currentIndex) => {
const device = devices[deviceKey];
const monitorOptions = [`--output ${deviceKey}`];
monitorOptions.push('--auto');
if (currentIndex > 0 && device.connected) {
monitorOptions.push(
`${positionParameter} ${deviceKeys[currentIndex - 1]}`
);
}
return [acc, ...monitorOptions].join(' ');
}, '');
}
async function generateXrandrOptions(
selectedDevices = [],
positionParameter = '--right-of'
) {
const devices = await getDevices();
const selectedAndConnected = getSelectedAndConnectedDevices(
selectedDevices,
devices
);
if (selectedDevices.length && !selectedAndConnected.length) {
throw new Error('Non of the given monitors are connected, aborting...');
}
console.log(
'Switching on',
selectedAndConnected.length
? selectedAndConnected.join(', ')
: 'all connected monitors'
);
const orderedDeviceKeys = [
...selectedAndConnected,
...Object.keys(devices)
.filter(key => !selectedAndConnected.includes(key))
.sort(sortDeviceKeys(devices))
];
const xrandrOptions = getXrandrOptions(
orderedDeviceKeys,
devices,
positionParameter
);
return xrandrOptions; return xrandrOptions;
} }
module.exports.getDevices = getDevices; async function printDevices() {
module.exports.generateXrandrOptions = generateXrandrOptions; const devices = await getDevices();
module.exports.switchDevices = switchDevices;
module.exports.executePostCmd = executePostCmd; const connectedMonitors = Object.keys(devices).filter(
key => devices[key].connected
);
const disconnectedMonitors = Object.keys(devices).filter(
key => !devices[key].connected
);
console.log(`Connected Monitors: ${connectedMonitors.join(', ')}`);
console.log(`Disconnected Monitors: ${disconnectedMonitors.join(', ')}`);
}
async function activate(selectedMonitors, postCmd) {
const xrandrOptions = await generateXrandrOptions(selectedMonitors);
await exec(`xrandr ${xrandrOptions}`);
if (postCmd) {
await exec(postCmd);
}
}
module.exports.printDevices = printDevices;
module.exports.activate = activate;