|
| 1 | +-- ╔══════════════════════════════════════════════════════════════╗ |
| 2 | +-- ║ STOA LINUX — Keybinds panel ║ |
| 3 | +-- ║ The cheatsheet, laid out. ║ |
| 4 | +-- ╚══════════════════════════════════════════════════════════════╝ |
| 5 | +-- |
| 6 | +-- Renders the output of `stoa-keybinds-bar text` — the same text Super+/ |
| 7 | +-- shows in the yad dialog — as a scrollable list: one block per "▸" |
| 8 | +-- section, each bind as key (left) + description (right). Nothing is |
| 9 | +-- hard-coded here, so the panel follows the script, including its niri |
| 10 | +-- substitutions. |
| 11 | +-- |
| 12 | +-- Written in the Lua 5.4-compatible subset of Luau (no type annotations, |
| 13 | +-- no if-expressions) so the CI lua step can actually parse it. |
| 14 | + |
| 15 | +local KEYBINDS_BIN = "$HOME/.local/bin/stoa-keybinds-bar" |
| 16 | +local TOGGLE_BIN = "$HOME/.local/bin/stoa-keybinds-toggle" |
| 17 | + |
| 18 | +-- ── UI tree node builders (same helpers as the stoa-health panel) ── |
| 19 | +local function node(kind, props, children) |
| 20 | + return { type = kind, props = props or {}, children = children or {} } |
| 21 | +end |
| 22 | +local function col(props, children) return node("column", props, children) end |
| 23 | +local function rowN(props, children) return node("row", props, children) end |
| 24 | +local function lbl(props) return node("label", props) end |
| 25 | +local function btn(props) return node("button", props) end |
| 26 | +local function sep(props) return node("separator", props) end |
| 27 | +local function scr(props, children) return node("scroll", props, children) end |
| 28 | + |
| 29 | +-- ── State ── |
| 30 | + |
| 31 | +local state = { |
| 32 | + loaded = false, |
| 33 | + failed = false, |
| 34 | + -- { { title = "Window", binds = { { key = "...", desc = "..." }, ... } }, ... } |
| 35 | + sections = {}, |
| 36 | + binds = 0, |
| 37 | +} |
| 38 | + |
| 39 | +-- ── Data ── |
| 40 | + |
| 41 | +-- " Super+Return Terminal (kitty)" → key, desc. Key and |
| 42 | +-- description are separated by two or more spaces; the key never contains |
| 43 | +-- a space. The box-drawing banner lines and blank lines match neither |
| 44 | +-- pattern and are skipped. |
| 45 | +local function parseText(text) |
| 46 | + local sections, current, binds = {}, nil, 0 |
| 47 | + for line in string.gmatch(text, "[^\n]+") do |
| 48 | + local title = string.match(line, "^▸%s*(.+)$") |
| 49 | + if title then |
| 50 | + current = { title = title, binds = {} } |
| 51 | + sections[#sections + 1] = current |
| 52 | + elseif current then |
| 53 | + local key, desc = string.match(line, "^%s+(%S+)%s%s+(%S.*)$") |
| 54 | + if key then |
| 55 | + current.binds[#current.binds + 1] = { key = key, desc = desc } |
| 56 | + binds = binds + 1 |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + state.sections = sections |
| 61 | + state.binds = binds |
| 62 | +end |
| 63 | + |
| 64 | +local function load() |
| 65 | + noctalia.runAsync(KEYBINDS_BIN .. " text", function(result) |
| 66 | + state.loaded = true |
| 67 | + if result.exitCode == 0 and result.stdout ~= "" then |
| 68 | + state.failed = false |
| 69 | + parseText(result.stdout) |
| 70 | + else |
| 71 | + state.failed = true |
| 72 | + end |
| 73 | + render() |
| 74 | + end) |
| 75 | +end |
| 76 | + |
| 77 | +-- ── Rendering ── |
| 78 | + |
| 79 | +local function bindRow(b) |
| 80 | + return rowN({ align = "center", gap = 10 }, { |
| 81 | + lbl({ text = b.key, fontSize = 12, fontWeight = "bold", color = "primary", width = 150 }), |
| 82 | + lbl({ text = b.desc, fontSize = 12, flexGrow = 1 }), |
| 83 | + }) |
| 84 | +end |
| 85 | + |
| 86 | +local function sectionBlock(section) |
| 87 | + local children = { |
| 88 | + lbl({ text = section.title, fontWeight = "bold", fontSize = 13 }), |
| 89 | + } |
| 90 | + for _, b in ipairs(section.binds) do |
| 91 | + children[#children + 1] = bindRow(b) |
| 92 | + end |
| 93 | + return col({ gap = 4 }, children) |
| 94 | +end |
| 95 | + |
| 96 | +local function body() |
| 97 | + if not state.loaded then |
| 98 | + return col({ flexGrow = 1, align = "center", justify = "center" }, { |
| 99 | + lbl({ text = "…", color = "on_surface_variant" }), |
| 100 | + }) |
| 101 | + end |
| 102 | + if state.failed or #state.sections == 0 then |
| 103 | + return col({ flexGrow = 1, align = "center", justify = "center", gap = 6 }, { |
| 104 | + lbl({ text = "Could not read the cheatsheet", color = "error" }), |
| 105 | + lbl({ text = "stoa-keybinds-bar text failed", color = "on_surface_variant", fontSize = 12 }), |
| 106 | + }) |
| 107 | + end |
| 108 | + |
| 109 | + local children = {} |
| 110 | + for i, section in ipairs(state.sections) do |
| 111 | + if i > 1 then children[#children + 1] = sep({ opacity = 0.3 }) end |
| 112 | + children[#children + 1] = sectionBlock(section) |
| 113 | + end |
| 114 | + return scr({ flexGrow = 1, gap = 10 }, children) |
| 115 | +end |
| 116 | + |
| 117 | +function render() |
| 118 | + local subtitle = "" |
| 119 | + if state.loaded and not state.failed then |
| 120 | + subtitle = string.format("%d binds", state.binds) |
| 121 | + end |
| 122 | + |
| 123 | + panel.render(col({ flexGrow = 1, gap = 10, padding = 12 }, { |
| 124 | + rowN({ align = "center" }, { |
| 125 | + lbl({ text = "Stoa Keybinds", fontWeight = "bold", flexGrow = 1 }), |
| 126 | + lbl({ text = subtitle, color = "on_surface_variant", fontSize = 12 }), |
| 127 | + }), |
| 128 | + sep({}), |
| 129 | + body(), |
| 130 | + sep({}), |
| 131 | + rowN({ gap = 6 }, { |
| 132 | + btn({ text = "Open as window", flexGrow = 1, controlSize = "sm", variant = "outline", |
| 133 | + onClick = function() noctalia.runAsync(TOGGLE_BIN) end }), |
| 134 | + }), |
| 135 | + })) |
| 136 | +end |
| 137 | + |
| 138 | +-- ── Lifecycle ── |
| 139 | + |
| 140 | +function onOpen(context) |
| 141 | + -- Re-read on every open: cheap, and it picks up a changed script or a |
| 142 | + -- compositor switch without restarting the shell. |
| 143 | + render() |
| 144 | + load() |
| 145 | +end |
0 commit comments