sway-tools
Version:
Some tools and a library for controlling sway
98 lines (97 loc) • 3.46 kB
JavaScript
;
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");
function getAppName(window) {
if (typeof window.app_id === 'string') {
const name = window.app_id.split('.').pop();
if (typeof name === 'string') {
return name;
}
}
if (typeof window.window_properties !== 'undefined') {
return window.window_properties.class;
}
return '???';
}
async function main() {
var _a, e_1, _b, _c;
const sway = new __1.Sway();
const tree = (await sway.getTree()).nodes.flatMap((node) => node.nodes);
const items = [];
for (const workspace of tree) {
const windows = workspace.nodes.flatMap((node) => {
if (node.nodes.length > 0) {
return node.nodes;
}
else {
return node;
}
});
for (const window of windows) {
items.push({
id: window.id,
workspace: workspace.name,
appTitle: window.name,
appName: getAppName(window),
});
}
}
const maxWorkspaceLength = items
.map((item) => item.workspace.length)
.reduce((prev, curr) => {
if (prev >= curr) {
return prev;
}
else {
return curr;
}
});
const maxAppTitleLength = 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(maxAppTitleLength)} ${item.appTitle} (${item.id})`)
.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 idMatch = output.trim().match(/\([0-9]+\)$/);
if (idMatch !== null) {
const id = idMatch[0].slice(1, -1);
child_process.execSync(`swaymsg "[con_id=${id}] focus"`);
}
}
void main();