Created
December 1, 2022 01:22
-
-
Save epicfilemcnulty/1351170d032835da6dd0c0537b6aa2f4 to your computer and use it in GitHub Desktop.
Window switching in sway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/lua5.1 | |
local json = require("cjson") | |
local windows = {} | |
local drill_down | |
drill_down = function(workspace_name, nodes) | |
if nodes then | |
for i, w in ipairs(nodes) do | |
if w.pid then | |
if not w.focused then | |
windows[workspace_name][w.pid] = { name = w.name, id = w.id, app_id = w.app_id } | |
end | |
else | |
drill_down(workspace_name, w.nodes) | |
end | |
end | |
end | |
end | |
local get_windows = function(root) | |
for i, output in ipairs(root.nodes) do | |
for i, workspace in ipairs(output.nodes) do | |
if not windows[workspace.name] then | |
windows[workspace.name] = {} | |
end | |
drill_down(workspace.name, workspace.nodes) | |
drill_down(workspace.name, workspace.floating_nodes) | |
end | |
end | |
end | |
local f = io.popen("swaymsg -t get_tree") | |
local result = f:read("*a") | |
f:close() | |
local root = json.decode(result) | |
get_windows(root) | |
local options = {} | |
for name, workspace in pairs(windows) do | |
local n = name | |
if name == "__i3_scratch" then | |
n = "0:Scratchpad" | |
end | |
n = n:gsub("^([0-9]+:)", "") | |
for pid, window in pairs(workspace) do | |
local workspace_name = "<b>" .. n .. ":</b> " | |
local pos = #options + 1 | |
if n == "Scratchpad" then | |
workspace_name = "<span color='#056b20'><b>" .. n .. ":</b></span> " | |
pos = 1 | |
end | |
table.insert( | |
options, | |
pos, | |
{ s = workspace_name .. "<span color='#321798'>" .. window.name .. "</span>", pid = pid } | |
) | |
end | |
end | |
local rofi_options = "" | |
for _, v in ipairs(options) do | |
rofi_options = rofi_options .. v.s .. "\n" | |
end | |
f = io.popen("rofi -dmenu -i -markup-rows -p ' ' -format d > /tmp/tabber-rofi-output", "w") | |
f:write(rofi_options) | |
f:close() | |
f = io.open("/tmp/tabber-rofi-output") | |
local result = f:read("*a") | |
f:close() | |
local index = tonumber(result) | |
if index then | |
os.execute("swaymsg [pid=" .. options[index].pid .. "] focus") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment