hypr: add lua scripts
d9695d821c119041132183a095742fc5a82eb4c0
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 768c1714e1f3
bugabingas dorkfiles
d9695d821c119041132183a095742fc5a82eb4c0
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+447 / −0 text lines
new file mode 100644+37 / −0 text lines
local M = {}function M.trim(value) return (value or ""):gsub("^%s+", ""):gsub("%s+$", "")endfunction M.shell_quote(value) value = tostring(value) return "'" .. value:gsub("'", "'\\''") .. "'"endfunction M.read_command(command) local pipe = io.popen(command .. " 2>/dev/null") if not pipe then return "" end local output = pipe:read("*a") or "" pipe:close() return outputendfunction M.choose(lines) if #lines == 0 then return nil end local path = os.tmpname() local file = assert(io.open(path, "w")) for _, line in ipairs(lines) do file:write(line, "\n") end file:close() local selected = M.trim(M.read_command("hyprlauncher --dmenu < " .. M.shell_quote(path))) os.remove(path) if selected == "" then return nil end return selectedendreturn Mnew file mode 100644+73 / −0 text lines
#!/usr/bin/env lualocal 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.pathlocal common = require("scripts.common")local function focus(address) if not address:match("^0x") then address = "0x" .. address end local expr = "hl.dispatch(hl.dsp.focus({ window = " .. string.format("%q", "address:" .. address) .. " }))" os.execute("hyprctl -q eval " .. common.shell_quote(expr))endlocal function parse_active() return common.read_command("hyprctl activewindow"):match("^Window%s+(%S+)%s+%-%>")endlocal function parse_clients() 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 = 0, mapped = true, hidden = false } clients[#clients + 1] = 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 = tonumber(value:match("^(%-?%d+)")) or 0 elseif key == "mapped" then current.mapped = value == "1" elseif key == "hidden" then current.hidden = value == "1" end end end local filtered = {} for _, client in ipairs(clients) do if client.mapped and not client.hidden then filtered[#filtered + 1] = client end end table.sort(filtered, function(a, b) if a.workspace ~= b.workspace then return a.workspace < b.workspace end if a.class ~= b.class then return a.class < b.class end if a.title ~= b.title then return a.title < b.title end return a.address < b.address end) return filteredendlocal direction = arg[1] == "prev" and -1 or 1local clients = parse_clients()if #clients < 2 then os.exit(0) endlocal active = parse_active()local index = 1for i, client in ipairs(clients) do if client.address == active then index = i break endendlocal next_index = ((index - 1 + direction) % #clients) + 1focus(clients[next_index].address)new file mode 100644+77 / −0 text lines
#!/usr/bin/env lualocal 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.pathlocal 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) >= bitendlocal 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, "+")endlocal function format_key(key) if key:match("^mouse:") then return key:gsub("^mouse:", "MOUSE_") end return key:upper()endlocal binds = {}local currentfor 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 endendlocal 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] = bindendlocal selected = common.choose(labels)if not selected then os.exit(0) endlocal bind = by_label[selected]if not bind then os.exit(1) endif 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))endnew file mode 100644+48 / −0 text lines
#!/usr/bin/env lualocal 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.pathlocal common = require("scripts.common")local clients = {}local currentfor 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 endendlocal 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] = clientendlocal selected = common.choose(labels)if not selected then os.exit(0) endlocal client = by_label[selected]if not client then os.exit(1) endlocal address = client.addressif not address:match("^0x") then address = "0x" .. address endlocal target = "address:" .. addresslocal expr = "hl.dispatch(hl.dsp.focus({ window = " .. string.format("%q", target) .. " }))"os.execute("hyprctl -q eval " .. common.shell_quote(expr))new file mode 100644+212 / −0 text lines
#!/usr/bin/env lualocal 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.pathlocal nugu = require("generated.nugu")local function trim(value) return (value or ""):gsub("^%s+", ""):gsub("%s+$", "")endlocal function shell_quote(value) value = tostring(value) return "'" .. value:gsub("'", "'\\''") .. "'"endlocal function run(command) os.execute(command .. " >/dev/null 2>&1")endlocal function mkdir(path) run("mkdir -p " .. shell_quote(path))endlocal function read_file(path) local file = io.open(path, "r") if not file then return nil end local data = file:read("*a") file:close() return dataendlocal function write_file(path, content) local file = assert(io.open(path, "w")) file:write(content) file:close()endlocal home = assert(os.getenv("HOME"), "HOME unset")local config_home = os.getenv("XDG_CONFIG_HOME") or home .. "/.config"local cache_dir = home .. "/.cache/nugu"local mode_file = cache_dir .. "/mode"local auto_file = cache_dir .. "/auto"local function active_mode() local mode = trim(read_file(mode_file)) if mode == "light" then return "light" end return "dark"endlocal function auto_mode() local hour = tonumber(os.date("%H")) or 0 if hour >= 7 and hour < 19 then return "light" end return "dark"endlocal function auto_enabled() local value = read_file(auto_file) return value == nil or trim(value) ~= "0"endlocal function set_auto(enabled) mkdir(cache_dir) write_file(auto_file, enabled and "1" or "0")endlocal function write_desktop_color_scheme(mode) local scheme = mode == "dark" and "prefer-dark" or "prefer-light" run("gsettings set org.gnome.desktop.interface color-scheme " .. shell_quote(scheme))endlocal function write_mako(p) local dir = config_home .. "/mako" mkdir(dir) write_file(dir .. "/nugu.conf", string.format([[# Generated by hypr/scripts/theme.lua from Nugu palette.background-color=%seetext-color=%sffborder-color=%sffprogress-color=over %sff[urgency=low]background-color=%se6text-color=%sffborder-color=%sffprogress-color=over %sff[urgency=normal]border-color=%sffprogress-color=over %sff[urgency=critical]background-color=%sfftext-color=%sffborder-color=%sffprogress-color=over %sff[summary~="[Ee]rror|[Ff]ailed|[Ff]ailure|[Cc]rash"]border-color=%sffprogress-color=over %sff[summary~="[Ww]arn|[Ww]arning"]border-color=%sffprogress-color=over %sff[summary~="[Ii]nfo|[Nn]otice"]border-color=%sffprogress-color=over %sff]], p.ui.backdrop, p.ui.normal, p.ui.accent, p.ui.focus, p.content.backdrop, p.content.minor, p.ui.unfocus, p.content.minor, p.ui.accent, p.ui.focus, p.ui.backdrop, p.ui.important_global, p.error, p.error, p.error, p.error, p.warning, p.warning, p.info, p.info))endlocal function write_ironbar(p) local dir = config_home .. "/ironbar" mkdir(dir) write_file(dir .. "/nugu.css", string.format([[/* Generated by hypr/scripts/theme.lua from Nugu palette. */:root { --bg: %s; --bg-transparent: %see; --surface: %s; --fg: %s; --accent: %s; --focus: %s; --on-focus: %s; --muted: %s; --unfocus: %s; --error: %s; --warning: %s; --info: %s; --debug: %s; --border: %s;}]], p.ui.backdrop, p.ui.backdrop, p.ui.minor, p.ui.normal, p.ui.accent, p.ui.focus, p.content.backdrop, p.ui.minor, p.ui.unfocus, p.error, p.warning, p.info, p.debug, p.ui.accent))endlocal function apply_mode(mode) mkdir(cache_dir) write_file(mode_file, mode) local p = assert(nugu.get(mode), "unknown mode: " .. tostring(mode)) write_desktop_color_scheme(mode) write_mako(p) write_ironbar(p) run("hyprctl hyprpaper wallpaper " .. shell_quote(",~/.config/hypr/wallpapers/nugu-" .. mode .. ".png")) run("hyprctl reload") if os.getenv("NUGU_NO_RESTART_MAKO") ~= "1" then run("systemctl --user restart mako.service") end run("systemctl --user restart ironbar.service") if os.getenv("NUGU_NO_NOTIFY") ~= "1" then run("notify-send --expire-time 1500 nugu " .. shell_quote(mode .. " mode")) endendlocal function next_mode() return active_mode() == "dark" and "light" or "dark"endlocal function watch() while true do if auto_enabled() then local target = auto_mode() if target ~= active_mode() then apply_mode(target) end end os.execute("sleep 300") endendlocal mode = arg[1] or "toggle"if mode == "dark" or mode == "light" or mode == "toggle" then set_auto(false)elseif mode == "auto" then set_auto(true)endif mode == "dark" or mode == "light" then apply_mode(mode)elseif mode == "toggle" then apply_mode(next_mode())elseif mode == "auto" then apply_mode(auto_mode())elseif mode == "current" then apply_mode(active_mode())elseif mode == "watch" then watch()else io.stderr:write("mode must be dark|light|toggle|auto|current|watch\n") os.exit(1)end