128 lines
5.0 KiB
Lua
128 lines
5.0 KiB
Lua
-- =========================
|
|
-- Overview (pure Lua, wofi-driven)
|
|
-- =========================
|
|
-- A niri-style workspace overview built entirely from Hyprland's Lua API -- no
|
|
-- external plugin (.so). Because Hyprland's Lua API exposes no "render a
|
|
-- workspace to a texture" primitive, we can't draw live scaled thumbnails from
|
|
-- Lua alone. So this is the honest pure-Lua version: a workspace *switcher
|
|
-- overview* -- enumerate every workspace (id, name, window count, active/empty
|
|
-- state) with hl.get_workspaces(), present them in a wofi dmenu, and jump to
|
|
-- the one you pick.
|
|
--
|
|
-- Flow: the Lua side is the source of truth for the workspace list; it writes a
|
|
-- tiny picker script to /tmp and launches it with `sh <file>`. The script shows
|
|
-- wofi, extracts the leading workspace id from the chosen line, and dispatches
|
|
-- it with hyprctl.
|
|
--
|
|
-- Why the dispatch lives in the script (not in a Lua timer): a Lua keybind
|
|
-- callback runs on the compositor's main thread under a ~100ms watchdog, so it
|
|
-- can't block reading wofi's stdout, and an async poll-timer handoff (reading a
|
|
-- result file on a repeating hl.timer) proved fragile -- a dispatch error left
|
|
-- the poll timer leaked. Keeping the switch in the spawned shell is the simple,
|
|
-- robust path. The script is launched as `sh <file>` (not executed) so we never
|
|
-- need os.execute/chmod, which would also block the callback.
|
|
--
|
|
-- Bound to Mod+Tab (niri parity) and Mod+G. The old plugin-based
|
|
-- conf/scrolloverview.lua is left untouched and still commented out in
|
|
-- hyprland.lua.
|
|
|
|
local mod = "SUPER"
|
|
|
|
-- Where the generated picker script lives. /tmp is fine for a per-session
|
|
-- helper; it's rewritten on every toggle so it always reflects live state.
|
|
local script_path = "/tmp/hypr-overview.sh"
|
|
|
|
---Build the display lines fed to wofi.
|
|
---Each line starts with the numeric workspace id, which is what the picker
|
|
---dispatches on. Keeping the id first means we never have to round-trip a
|
|
---hidden selector column through wofi.
|
|
---@return string[] lines one human-readable label per workspace
|
|
local function workspace_lines()
|
|
local lines = {}
|
|
|
|
-- Stable ordering: numeric id ascending.
|
|
local ws = hl.get_workspaces() or {}
|
|
table.sort(ws, function(a, b)
|
|
return (a.id or 0) < (b.id or 0)
|
|
end)
|
|
|
|
for _, w in ipairs(ws) do
|
|
-- Skip special (scratchpad) workspaces; they aren't part of the tape.
|
|
if not w.special and w.id and w.id > 0 then
|
|
local name = (w.name and w.name ~= "") and w.name or ("workspace " .. w.id)
|
|
local count = w.windows or 0
|
|
|
|
local flags = {}
|
|
if w.active then
|
|
table.insert(flags, "active")
|
|
end
|
|
if count == 0 then
|
|
table.insert(flags, "empty")
|
|
end
|
|
if w.has_urgent then
|
|
table.insert(flags, "urgent")
|
|
end
|
|
local suffix = #flags > 0 and (" [" .. table.concat(flags, ", ") .. "]") or ""
|
|
|
|
-- Leading "<id>." is parsed back out by the picker; keep it first.
|
|
table.insert(
|
|
lines,
|
|
string.format("%d. %-24s %2d window(s)%s", w.id, name, count, suffix)
|
|
)
|
|
end
|
|
end
|
|
|
|
return lines
|
|
end
|
|
|
|
---Write the picker helper script. It feeds the display lines to wofi, extracts
|
|
---the leading workspace id from the chosen line, and dispatches it via hyprctl.
|
|
---@param lines string[]
|
|
---@return boolean ok
|
|
local function write_script(lines)
|
|
local f = io.open(script_path, "w")
|
|
if not f then
|
|
hl.print("overview: could not write", script_path)
|
|
return false
|
|
end
|
|
|
|
f:write("#!/bin/sh\n")
|
|
f:write("# Generated by conf/overview.lua -- do not edit by hand.\n")
|
|
-- No `set -e`: wofi exits non-zero on Escape/cancel; we just do nothing.
|
|
f:write("pick=$(cat <<'__DATA__' | wofi --dmenu --prompt 'Overview' --width 480 --lines 15)\n")
|
|
for _, line in ipairs(lines) do
|
|
-- Guard against a stray newline/heredoc terminator inside a name.
|
|
local safe = line:gsub("\n", " ")
|
|
f:write(safe .. "\n")
|
|
end
|
|
f:write("__DATA__\n")
|
|
f:write(")\n")
|
|
f:write('[ -z "$pick" ] && exit 0\n')
|
|
-- The chosen line begins with "<id>."; take the leading digits as the id.
|
|
f:write('id=$(printf %s "$pick" | cut -d. -f1 | tr -dc 0-9)\n')
|
|
f:write('[ -z "$id" ] && exit 0\n')
|
|
f:write('exec hyprctl dispatch workspace "$id"\n')
|
|
f:close()
|
|
|
|
return true
|
|
end
|
|
|
|
---Toggle the overview: regenerate the live workspace list and pop wofi.
|
|
---Writing a small file and spawning a process are both fast enough to run
|
|
---directly in the keybind callback (no blocking subprocess, no chmod).
|
|
local function show_overview()
|
|
local lines = workspace_lines()
|
|
if #lines == 0 then
|
|
return
|
|
end
|
|
|
|
if write_script(lines) then
|
|
hl.exec_cmd("sh " .. script_path)
|
|
end
|
|
end
|
|
|
|
-- Mod+Tab is niri's toggle-overview; Mod+G kept for muscle-memory parity with
|
|
-- the old scrolloverview bind.
|
|
hl.bind(mod .. " + Tab", show_overview)
|
|
hl.bind(mod .. " + g", show_overview)
|