repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

hypr/scripts/show_binds.lua

Raw
File outline
#!/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 modifier_bits = {
  { bit = 64, name = "SUPER" },
  { bit = 1, name = "SHIFT" },
  { bit = 4, name = "CTRL" },
  { bit = 8, name = "ALT" },
}

local function has_bit(value, bit)
  return value % (bit * 2) >= bit
end

local function mods(mask)
  local out = {}
  for _, mod in ipairs(modifier_bits) do
    if has_bit(mask, mod.bit) then out[#out + 1] = mod.name end
  end
  if #out == 0 then return "None" end
  return table.concat(out, "+")
end

local function format_key(key)
  if key:match("^mouse:") then return key:gsub("^mouse:", "MOUSE_") end
  return key:upper()
end

local binds = {}
local current

for line in common.read_command("hyprctl binds"):gmatch("[^\n]+") do
  if line:match("^bind") then
    current = { modmask = 0, key = "?", dispatcher = "", arg = "", description = "" }
    table.insert(binds, current)
  elseif current then
    local key, value = line:match("^%s*([%w_]+):%s*(.*)$")
    if key == "modmask" then
      current.modmask = tonumber(value) or 0
    elseif key == "key" then
      current.key = value
    elseif key == "dispatcher" then
      current.dispatcher = value
    elseif key == "arg" then
      current.arg = value
    elseif key == "description" then
      current.description = value
    end
  end
end

local labels = {}
local by_label = {}

for _, bind in ipairs(binds) do
  local label = string.format("%s + %s -> %s", mods(bind.modmask), format_key(bind.key), bind.description ~= "" and bind.description or bind.dispatcher)
  labels[#labels + 1] = label
  by_label[label] = bind
end

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

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

if bind.dispatcher == "exec" and bind.arg ~= "" then
  os.execute(bind.arg .. " &")
elseif bind.dispatcher ~= "" and bind.dispatcher ~= "__lua" then
  os.execute("hyprctl dispatch " .. common.shell_quote(bind.dispatcher) .. " " .. common.shell_quote(bind.arg))
end