UNPKG

sway-tools

Version:

Some tools and a library for controlling sway

80 lines (79 loc) 3 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 hyprland_1 = require("../../services/hyprland"); const child_process = require("child_process"); const lodash = require("lodash"); const shelljs = require("shelljs"); async function main() { var _a, e_1, _b, _c; const hyprland = new hyprland_1.Hyprland(); const clients = hyprland.getClients(); const items = clients .filter((client) => client.mapped) .map((client) => ({ address: client.address, workspace: String(client.workspace.name), appName: client.class || '???', appTitle: client.title || '', })); if (items.length === 0) { return; } const maxWorkspaceLength = items .map((item) => item.workspace.length) .reduce((prev, curr) => { if (prev >= curr) { return prev; } else { return curr; } }); const maxAppNameLength = items .map((item) => item.appName.length) .reduce((prev, curr) => { if (prev >= curr) { return prev; } else { return curr; } }); const dmenu = lodash .sortBy(items, ['appName']) .map((item) => `${item.workspace.padEnd(maxWorkspaceLength)} ${item.appName.padEnd(maxAppNameLength)} ${item.appTitle} (${item.address})`) .join('\n'); const rofi = child_process.exec('rofi -dmenu -i -p "Select a Window" -format'); rofi.stdin.write(dmenu); 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; } } const addressMatch = output.trim().match(/\(0x[0-9a-f]+\)$/); if (addressMatch !== null) { const address = addressMatch[0].slice(1, -1); shelljs.exec(`hyprctl dispatch focuswindow address:${address}`); } } void main();