UNPKG

sway-tools

Version:

Some tools and a library for controlling sway

76 lines (75 loc) 3.25 kB
#!/usr/bin/env node "use strict"; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require(".."); const child_process = require("child_process"); const lodash = require("lodash"); const shelljs = require("shelljs"); const logger_1 = require("../logger"); const logger = (0, logger_1.createLogger)('move-ws-to-output'); async function showMenu(menu) { var _a, e_1, _b, _c; const rofi = child_process.exec('rofi -dmenu -i -p "Select a Window" -format'); rofi.stdin.write(menu); rofi.stdin.end(); let output = ''; try { for (var _d = true, _e = __asyncValues(rofi.stdout), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const chunk = _c; output += chunk; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) await _b.call(_e); } finally { if (e_1) throw e_1.error; } } return output.trimEnd(); } async function main() { logger.debug({ file: __filename }, 'Welcome to the move ws to output tool'); const sway = new __1.Sway(); const tree = (await sway.getTree()).nodes.flatMap((node) => node.nodes); logger.debug('assemabled the tree'); const workspaces = []; for (const workspace of tree) { workspaces.push(workspace.name); } const dmenu = lodash.sortBy(workspaces).join('\n'); const selectedWorkspace = await showMenu(dmenu); if (selectedWorkspace.length === 0) { return; } logger.debug({ selectedWorkspace }, 'Selected workspace'); const outputs = await sway.getOutputs(); const outputMenu = lodash .sortBy(outputs, ['name', 'make', 'model', 'serial']) .map((output) => `${output.make} ${output.model} ${output.serial} (${output.name})`) .join('\n'); logger.debug({ outputMenu }, 'Spawning the menu'); const selectedOutput = await showMenu(outputMenu); if (selectedOutput.length === 0) { return; } const selectedOutputMatch = selectedOutput.trim().match(/\(.+\)$/); let outputName = null; if (selectedOutputMatch !== null) { outputName = selectedOutputMatch[0].slice(1, -1); } if (typeof outputName === 'string' && typeof selectedWorkspace === 'string') { logger.debug({ selectedWorkspace, outputName }, 'Moving window to workspace'); shelljs.exec(`swayws move "${selectedWorkspace}" "${outputName}"`); } } void main();