repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

hypr/scripts/show_clients.lua

Raw
#!/usr/bin/env lua

local source = debug.getinfo(1, "S").source:gsub("^@", "")
local script_dir = source:match("(.+)/[^/]+$") or "."
local hypr_dir = script_dir:match("(.+)/[^/]+$") or "."
package.path = hypr_dir .. "/?.lua;" .. hypr_dir .. "/?/init.lua;" .. package.path

local common = require("scripts.common")

local clients = {}
local current

for line in common.read_command("hyprctl clients"):gmatch("[^\n]+") do
  local address, title = line:match("^Window%s+(%S+)%s+%-%>%s+(.+):$")
  if address then
    current = { address = address, title = title, class = "?", workspace = "?" }
    table.insert(clients, current)
  elseif current then
    local key, value = line:match("^%s*([%w_]+):%s*(.*)$")
    if key == "class" then
      current.class = value
    elseif key == "workspace" then
      current.workspace = value:match("%((.-)%)") or value:match("^(%S+)") or "?"
    end
  end
end

local labels = {}
local by_label = {}

for _, client in ipairs(clients) do
  local label = string.format("%s: %s (%s)", client.class, client.title, client.workspace)
  labels[#labels + 1] = label
  by_label[label] = client
end

local selected = common.choose(labels)
if not selected then os.exit(0) end

local client = by_label[selected]
if not client then os.exit(1) end

local address = client.address
if not address:match("^0x") then address = "0x" .. address end

local target = "address:" .. address
local expr = "hl.dispatch(hl.dsp.focus({ window = " .. string.format("%q", target) .. " }))"
os.execute("hyprctl -q eval " .. common.shell_quote(expr))