sway-tools
Version:
Some tools and a library for controlling sway
77 lines (76 loc) • 3.56 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 hyprland_1 = require("../../services/hyprland");
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, prompt) {
var _a, e_1, _b, _c;
const rofi = child_process.exec(`rofi -dmenu -i -p "${prompt}" -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 hyprland = new hyprland_1.Hyprland();
const workspaces = hyprland.getWorkspaces().map((workspace) => workspace.name);
const dmenu = lodash.sortBy(workspaces).join('\n');
const selectedWorkspace = await showMenu(dmenu, 'Select a Workspace');
if (selectedWorkspace.length === 0) {
return;
}
logger.debug({ selectedWorkspace }, 'Selected workspace');
const outputs = hyprland.getOutputs();
const outputMenu = lodash
.sortBy(outputs, ['id', '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, 'Select a Monitor');
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') {
const activeWorkspace = hyprland.getActiveWorkspace();
logger.debug({ activeWorkspace: activeWorkspace.name }, 'Saving active workspace');
const command = `hyprctl --batch "dispatch workspace name:${selectedWorkspace} ; dispatch movecurrentworkspacetomonitor ${outputName} ; dispatch workspace name:${activeWorkspace.name}"`;
logger.debug({ selectedWorkspace, outputName, command }, 'Moving workspace to monitor');
const output = shelljs.exec(command);
logger.debug({
output,
}, 'Result of dispatch');
}
}
void main();