repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

hypr: add lua scripts

d9695d821c119041132183a095742fc5a82eb4c0

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

+447 / −0 text lines

Diff layout

hypr/scripts/common.lua

Added
new file mode 100644

+37 / −0 text lines

1local M = {}
2
3function M.trim(value)
4 return (value or ""):gsub("^%s+", ""):gsub("%s+$", "")
5end
6
7function M.shell_quote(value)
8 value = tostring(value)
9 return "'" .. value:gsub("'", "'\\''") .. "'"
10end
12function M.read_command(command)
13 local pipe = io.popen(command .. " 2>/dev/null")
14 if not pipe then return "" end
15 local output = pipe:read("*a") or ""
16 pipe:close()
17 return output
18end
20function M.choose(lines)
21 if #lines == 0 then return nil end
23 local path = os.tmpname()
24 local file = assert(io.open(path, "w"))
25 for _, line in ipairs(lines) do
26 file:write(line, "\n")
27 end
28 file:close()
30 local selected = M.trim(M.read_command("hyprlauncher --dmenu < " .. M.shell_quote(path)))
31 os.remove(path)
33 if selected == "" then return nil end
34 return selected
35end
37return M

hypr/scripts/cycle_clients.lua

Added
new file mode 100644

+73 / −0 text lines

1#!/usr/bin/env lua
2
3local source = debug.getinfo(1, "S").source:gsub("^@", "")
4local script_dir = source:match("(.+)/[^/]+$") or "."
5local hypr_dir = script_dir:match("(.+)/[^/]+$") or "."
6package.path = hypr_dir .. "/?.lua;" .. hypr_dir .. "/?/init.lua;" .. package.path
7
8local common = require("scripts.common")
9
10local function focus(address)
11 if not address:match("^0x") then address = "0x" .. address end
12 local expr = "hl.dispatch(hl.dsp.focus({ window = " .. string.format("%q", "address:" .. address) .. " }))"
13 os.execute("hyprctl -q eval " .. common.shell_quote(expr))
14end
16local function parse_active()
17 return common.read_command("hyprctl activewindow"):match("^Window%s+(%S+)%s+%-%>")
18end
20local function parse_clients()
21 local clients = {}
22 local current
24 for line in common.read_command("hyprctl clients"):gmatch("[^\n]+") do
25 local address, title = line:match("^Window%s+(%S+)%s+%-%>%s+(.+):$")
26 if address then
27 current = { address = address, title = title, class = "", workspace = 0, mapped = true, hidden = false }
28 clients[#clients + 1] = current
29 elseif current then
30 local key, value = line:match("^%s*([%w_]+):%s*(.*)$")
31 if key == "class" then
32 current.class = value
33 elseif key == "workspace" then
34 current.workspace = tonumber(value:match("^(%-?%d+)")) or 0
35 elseif key == "mapped" then
36 current.mapped = value == "1"
37 elseif key == "hidden" then
38 current.hidden = value == "1"
39 end
40 end
41 end
43 local filtered = {}
44 for _, client in ipairs(clients) do
45 if client.mapped and not client.hidden then filtered[#filtered + 1] = client end
46 end
48 table.sort(filtered, function(a, b)
49 if a.workspace ~= b.workspace then return a.workspace < b.workspace end
50 if a.class ~= b.class then return a.class < b.class end
51 if a.title ~= b.title then return a.title < b.title end
52 return a.address < b.address
53 end)
55 return filtered
56end
58local direction = arg[1] == "prev" and -1 or 1
59local clients = parse_clients()
60if #clients < 2 then os.exit(0) end
62local active = parse_active()
63local index = 1
65for i, client in ipairs(clients) do
66 if client.address == active then
67 index = i
68 break
69 end
70end
72local next_index = ((index - 1 + direction) % #clients) + 1
73focus(clients[next_index].address)

hypr/scripts/show_binds.lua

Added
new file mode 100644

+77 / −0 text lines

1#!/usr/bin/env lua
2
3local source = debug.getinfo(1, "S").source:gsub("^@", "")
4local script_dir = source:match("(.+)/[^/]+$") or "."
5local hypr_dir = script_dir:match("(.+)/[^/]+$") or "."
6package.path = hypr_dir .. "/?.lua;" .. hypr_dir .. "/?/init.lua;" .. package.path
7
8local common = require("scripts.common")
9
10local modifier_bits = {
11 { bit = 64, name = "SUPER" },
12 { bit = 1, name = "SHIFT" },
13 { bit = 4, name = "CTRL" },
14 { bit = 8, name = "ALT" },
15}
17local function has_bit(value, bit)
18 return value % (bit * 2) >= bit
19end
21local function mods(mask)
22 local out = {}
23 for _, mod in ipairs(modifier_bits) do
24 if has_bit(mask, mod.bit) then out[#out + 1] = mod.name end
25 end
26 if #out == 0 then return "None" end
27 return table.concat(out, "+")
28end
30local function format_key(key)
31 if key:match("^mouse:") then return key:gsub("^mouse:", "MOUSE_") end
32 return key:upper()
33end
35local binds = {}
36local current
38for line in common.read_command("hyprctl binds"):gmatch("[^\n]+") do
39 if line:match("^bind") then
40 current = { modmask = 0, key = "?", dispatcher = "", arg = "", description = "" }
41 table.insert(binds, current)
42 elseif current then
43 local key, value = line:match("^%s*([%w_]+):%s*(.*)$")
44 if key == "modmask" then
45 current.modmask = tonumber(value) or 0
46 elseif key == "key" then
47 current.key = value
48 elseif key == "dispatcher" then
49 current.dispatcher = value
50 elseif key == "arg" then
51 current.arg = value
52 elseif key == "description" then
53 current.description = value
54 end
55 end
56end
58local labels = {}
59local by_label = {}
61for _, bind in ipairs(binds) do
62 local label = string.format("%s + %s -> %s", mods(bind.modmask), format_key(bind.key), bind.description ~= "" and bind.description or bind.dispatcher)
63 labels[#labels + 1] = label
64 by_label[label] = bind
65end
67local selected = common.choose(labels)
68if not selected then os.exit(0) end
70local bind = by_label[selected]
71if not bind then os.exit(1) end
73if bind.dispatcher == "exec" and bind.arg ~= "" then
74 os.execute(bind.arg .. " &")
75elseif bind.dispatcher ~= "" and bind.dispatcher ~= "__lua" then
76 os.execute("hyprctl dispatch " .. common.shell_quote(bind.dispatcher) .. " " .. common.shell_quote(bind.arg))
77end

hypr/scripts/show_clients.lua

Added
new file mode 100644

+48 / −0 text lines

1#!/usr/bin/env lua
2
3local source = debug.getinfo(1, "S").source:gsub("^@", "")
4local script_dir = source:match("(.+)/[^/]+$") or "."
5local hypr_dir = script_dir:match("(.+)/[^/]+$") or "."
6package.path = hypr_dir .. "/?.lua;" .. hypr_dir .. "/?/init.lua;" .. package.path
7
8local common = require("scripts.common")
9
10local clients = {}
11local current
13for line in common.read_command("hyprctl clients"):gmatch("[^\n]+") do
14 local address, title = line:match("^Window%s+(%S+)%s+%-%>%s+(.+):$")
15 if address then
16 current = { address = address, title = title, class = "?", workspace = "?" }
17 table.insert(clients, current)
18 elseif current then
19 local key, value = line:match("^%s*([%w_]+):%s*(.*)$")
20 if key == "class" then
21 current.class = value
22 elseif key == "workspace" then
23 current.workspace = value:match("%((.-)%)") or value:match("^(%S+)") or "?"
24 end
25 end
26end
28local labels = {}
29local by_label = {}
31for _, client in ipairs(clients) do
32 local label = string.format("%s: %s (%s)", client.class, client.title, client.workspace)
33 labels[#labels + 1] = label
34 by_label[label] = client
35end
37local selected = common.choose(labels)
38if not selected then os.exit(0) end
40local client = by_label[selected]
41if not client then os.exit(1) end
43local address = client.address
44if not address:match("^0x") then address = "0x" .. address end
46local target = "address:" .. address
47local expr = "hl.dispatch(hl.dsp.focus({ window = " .. string.format("%q", target) .. " }))"
48os.execute("hyprctl -q eval " .. common.shell_quote(expr))

hypr/scripts/theme.lua

Added
new file mode 100644

+212 / −0 text lines

1#!/usr/bin/env lua
2
3local source = debug.getinfo(1, "S").source:gsub("^@", "")
4local script_dir = source:match("(.+)/[^/]+$") or "."
5local hypr_dir = script_dir:match("(.+)/[^/]+$") or "."
6package.path = hypr_dir .. "/?.lua;" .. hypr_dir .. "/?/init.lua;" .. package.path
7
8local nugu = require("generated.nugu")
9
10local function trim(value)
11 return (value or ""):gsub("^%s+", ""):gsub("%s+$", "")
12end
14local function shell_quote(value)
15 value = tostring(value)
16 return "'" .. value:gsub("'", "'\\''") .. "'"
17end
19local function run(command)
20 os.execute(command .. " >/dev/null 2>&1")
21end
23local function mkdir(path)
24 run("mkdir -p " .. shell_quote(path))
25end
27local function read_file(path)
28 local file = io.open(path, "r")
29 if not file then return nil end
30 local data = file:read("*a")
31 file:close()
32 return data
33end
35local function write_file(path, content)
36 local file = assert(io.open(path, "w"))
37 file:write(content)
38 file:close()
39end
41local home = assert(os.getenv("HOME"), "HOME unset")
42local config_home = os.getenv("XDG_CONFIG_HOME") or home .. "/.config"
43local cache_dir = home .. "/.cache/nugu"
44local mode_file = cache_dir .. "/mode"
45local auto_file = cache_dir .. "/auto"
47local function active_mode()
48 local mode = trim(read_file(mode_file))
49 if mode == "light" then return "light" end
50 return "dark"
51end
53local function auto_mode()
54 local hour = tonumber(os.date("%H")) or 0
55 if hour >= 7 and hour < 19 then return "light" end
56 return "dark"
57end
59local function auto_enabled()
60 local value = read_file(auto_file)
61 return value == nil or trim(value) ~= "0"
62end
64local function set_auto(enabled)
65 mkdir(cache_dir)
66 write_file(auto_file, enabled and "1" or "0")
67end
69local function write_desktop_color_scheme(mode)
70 local scheme = mode == "dark" and "prefer-dark" or "prefer-light"
71 run("gsettings set org.gnome.desktop.interface color-scheme " .. shell_quote(scheme))
72end
74local function write_mako(p)
75 local dir = config_home .. "/mako"
76 mkdir(dir)
77 write_file(dir .. "/nugu.conf", string.format([[# Generated by hypr/scripts/theme.lua from Nugu palette.
78background-color=%see
79text-color=%sff
80border-color=%sff
81progress-color=over %sff
83[urgency=low]
84background-color=%se6
85text-color=%sff
86border-color=%sff
87progress-color=over %sff
89[urgency=normal]
90border-color=%sff
91progress-color=over %sff
93[urgency=critical]
94background-color=%sff
95text-color=%sff
96border-color=%sff
97progress-color=over %sff
99[summary~="[Ee]rror|[Ff]ailed|[Ff]ailure|[Cc]rash"]
100border-color=%sff
101progress-color=over %sff
103[summary~="[Ww]arn|[Ww]arning"]
104border-color=%sff
105progress-color=over %sff
107[summary~="[Ii]nfo|[Nn]otice"]
108border-color=%sff
109progress-color=over %sff
110]],
111 p.ui.backdrop, p.ui.normal, p.ui.accent, p.ui.focus,
112 p.content.backdrop, p.content.minor, p.ui.unfocus, p.content.minor,
113 p.ui.accent, p.ui.focus,
114 p.ui.backdrop, p.ui.important_global, p.error, p.error,
115 p.error, p.error,
116 p.warning, p.warning,
117 p.info, p.info))
118end
120local function write_ironbar(p)
121 local dir = config_home .. "/ironbar"
122 mkdir(dir)
123 write_file(dir .. "/nugu.css", string.format([[/* Generated by hypr/scripts/theme.lua from Nugu palette. */
124:root {
125 --bg: %s;
126 --bg-transparent: %see;
127 --surface: %s;
128 --fg: %s;
129 --accent: %s;
130 --focus: %s;
131 --on-focus: %s;
132 --muted: %s;
133 --unfocus: %s;
134 --error: %s;
135 --warning: %s;
136 --info: %s;
137 --debug: %s;
138 --border: %s;
139}
140]],
141 p.ui.backdrop,
142 p.ui.backdrop,
143 p.ui.minor,
144 p.ui.normal,
145 p.ui.accent,
146 p.ui.focus,
147 p.content.backdrop,
148 p.ui.minor,
149 p.ui.unfocus,
150 p.error,
151 p.warning,
152 p.info,
153 p.debug,
154 p.ui.accent))
155end
157local function apply_mode(mode)
158 mkdir(cache_dir)
159 write_file(mode_file, mode)
161 local p = assert(nugu.get(mode), "unknown mode: " .. tostring(mode))
162 write_desktop_color_scheme(mode)
163 write_mako(p)
164 write_ironbar(p)
166 run("hyprctl hyprpaper wallpaper " .. shell_quote(",~/.config/hypr/wallpapers/nugu-" .. mode .. ".png"))
167 run("hyprctl reload")
168 if os.getenv("NUGU_NO_RESTART_MAKO") ~= "1" then
169 run("systemctl --user restart mako.service")
170 end
171 run("systemctl --user restart ironbar.service")
172 if os.getenv("NUGU_NO_NOTIFY") ~= "1" then
173 run("notify-send --expire-time 1500 nugu " .. shell_quote(mode .. " mode"))
174 end
175end
177local function next_mode()
178 return active_mode() == "dark" and "light" or "dark"
179end
181local function watch()
182 while true do
183 if auto_enabled() then
184 local target = auto_mode()
185 if target ~= active_mode() then apply_mode(target) end
186 end
187 os.execute("sleep 300")
188 end
189end
191local mode = arg[1] or "toggle"
193if mode == "dark" or mode == "light" or mode == "toggle" then
194 set_auto(false)
195elseif mode == "auto" then
196 set_auto(true)
197end
199if mode == "dark" or mode == "light" then
200 apply_mode(mode)
201elseif mode == "toggle" then
202 apply_mode(next_mode())
203elseif mode == "auto" then
204 apply_mode(auto_mode())
205elseif mode == "current" then
206 apply_mode(active_mode())
207elseif mode == "watch" then
208 watch()
209else
210 io.stderr:write("mode must be dark|light|toggle|auto|current|watch\n")
211 os.exit(1)
212end