From 10df6d114d9965254233cffbabdcfcf12d659959 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Sat, 18 Nov 2017 13:17:29 +0530 Subject: [PATCH] Initial commit --- index.js | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 28 ++++++++++ 2 files changed, 172 insertions(+) create mode 100755 index.js create mode 100644 package.json diff --git a/index.js b/index.js new file mode 100755 index 0000000..de61f6e --- /dev/null +++ b/index.js @@ -0,0 +1,144 @@ +#!/usr/bin/env node +var _exec = require('child_process').execSync; + +function exec( cmd ){ + console.log( 'exec: ', cmd ); + return _exec( cmd ); +} + +function getSwayInfo( cmd ){ + return JSON.parse( exec(`swaymsg -t ${cmd}`) ); +} + +/** + * Workspace Manager + */ +function WpManager( Rows, Cols, monitorCount ){ + var w = 0, + workspaces = []; + for (r = 0; r < Rows; r++) { + for (c = 0; c < Cols; c++) { + workspaces.push( { + left: c !== 0 && w-1, + right: ( c+1 ) !== Cols && w+1, + up: r!==0 && w-Cols, + down: (r+1)!==Rows && w+Cols, + }); + w++; + } + } + this.workspaces = workspaces; + this.Rows = Rows; + this.Cols = Cols; + this.monitorCount = monitorCount; +} + +/** + * Workspace grid initialisation command generator. + * Currenlty not working because `Workspace` command will not work through `swaymsg`. + * it will work only if we put these commands on config file + */ +WpManager.prototype.getInitCmd = function(){ + var outputs = getSwayInfo('get_outputs').map(v => v.name ), + outputCout = outputs.length, + totalWorkspaces = this.Rows * this.Cols, + cmd = []; + + for( ws = 0; ws} - list of target workspaces + */ +WpManager.prototype.getWorkspaces = function( current, dir ){ + var monitorCount = this.monitorCount, out=[], i; + var targetw = this.workspaces[ Math.ceil( current/monitorCount ) - 1 ][dir]; + if( targetw !== false ){ + targetw = targetw*monitorCount; + for (i = monitorCount; i !== 0; i--) { + out.push( targetw+i ); + } + return out; + } +}; + +/** + * Get swaymsg command for switching current Workspace to direction + * + * @param swayWorkspace + * @param dir + * @param isMove + * @returns {String} - swaymsg command + */ +WpManager.prototype.getCmd = function( swayWorkspace, dir, isMove ){ + var currentWS = swayWorkspace.num, currentOutput = swayWorkspace.output; + + var ws = this.getWorkspaces( currentWS, dir ); + if(!ws){ + return ; + } + var out = ws.map( v => `workspace ${v}` ); + if( isMove ){ + var tws= ws[ currentWS%this.monitorCount]; + out.unshift( `move container to workspace ${ tws }` ); + } + out.push( `focus output ${ currentOutput }` ); + return out.join('; '); +}; + + +function main(){ + var dir = process.argv[2], + isMove=false, + ROWS=3, + COLS=3, + wm, + cmd, + outputInfo, + workspaceInfo; + + if( dir === '-m' ){ + isMove=true; + dir = process.argv[3]; + } + if( [ 'up', 'down', 'right', 'left' ].indexOf( dir ) === -1 ){ + console.log([ + 'Usage:', + '\tsway-wp-switch [ -m ] ', + '\n', + 'Options:', + '\t-m\t move current window to new workspace while switching' + ].join('\n')); + return; + } + + outputInfo = getSwayInfo('get_outputs'); + wm = new WpManager( ROWS, COLS, outputInfo.length ); + if( dir === '-i' ){ + cmd = wm.getInitCmd(); + } else { + workspaceInfo = getSwayInfo('get_workspaces').find( v=>v.focused ); + cmd = wm.getCmd( workspaceInfo, dir, isMove); + } + + if( cmd ){ + exec(`swaymsg -t command "${cmd}"`); + } +} + + +main(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..cab3966 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "sway-workspace-grid", + "version": "0.0.1", + "description": "A simple utility to organize sway workspaces as M x N grid with multi-monitor support Edit", + "main": "index.js", + "bin":{ + "sway-wp-switch": "./index.js" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/harish2704/sway-workspace-grid.git" + }, + "keywords": [ + "sway", + "i3", + "workspace", + "grid" + ], + "author": "Harish.K (https://github.com/harish2704)", + "license": "MIT", + "bugs": { + "url": "https://github.com/harish2704/sway-workspace-grid/issues" + }, + "homepage": "https://github.com/harish2704/sway-workspace-grid#readme" +}