Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

captureConfigEdit.ts

210 lines8.0 KB

1import { parseKeybindingShortcut } from "@t3tools/shared/keybindings";
2import { readKdlNodes, type KdlNode } from "./captureConfigKdl.ts";
3import { niriCaptureBinding } from "./linuxCaptureSession.ts";
4​
5export type CaptureConfigFormat = "niri" | "hyprland" | "hyprland-lua";
6​
7export function captureConfigKeys(input = "Ctrl+Shift+2") {
8 const keys = parseKeybindingShortcut(input.replace(/super/gi, "meta"));
9 if (
10 !keys ||
11 keys.modKey ||
12 !/^(?:[a-z0-9]|f(?:[1-9]|1[0-9]|2[0-4]))$/.test(keys.key) ||
13 !(keys.ctrlKey || keys.altKey || keys.metaKey)
14 )
15 throw new Error("Choose a letter, number, or function key with Ctrl, Alt, or Super.");
16 const modifiers = [
17 keys.ctrlKey && "Ctrl",
18 keys.altKey && "Alt",
19 keys.shiftKey && "Shift",
20 keys.metaKey && "Super",
21 ].filter(Boolean);
22 return {
23 label: [...modifiers, keys.key.toUpperCase()].join("+"),
24 modifiers: modifiers.map((key) => String(key).toUpperCase()).join(" "),
25 key: keys.key.toUpperCase(),
26 mask:
27 (keys.shiftKey ? 1 : 0) |
28 (keys.ctrlKey ? 4 : 0) |
29 (keys.altKey ? 8 : 0) |
30 (keys.metaKey ? 64 : 0),
31 };
33​
34function sameKeys(left: string, right: string) {
35 try {
36 return captureConfigKeys(left).label === captureConfigKeys(right).label;
37 } catch {
38 return false;
39 }
41​
42export function captureConfigBinding(format: CaptureConfigFormat, appId: string, keys: string) {
43 if (!/^[A-Za-z0-9_.-]+$/.test(appId)) throw new Error("Invalid capture application ID.");
44 const chord = captureConfigKeys(keys);
45 if (format === "niri") return niriCaptureBinding(appId).replace("Ctrl+Shift+2", chord.label);
46 const action = `${appId}:capture-window`;
47 return format === "hyprland-lua"
48 ? `hl.bind("${chord.label.toUpperCase().replaceAll("+", " + ")}", hl.dsp.global("${action}"))`
49 : `bind = ${chord.modifiers}, ${chord.key}, global, ${action}`;
51​
52function niriBinds(source: string) {
53 const blocks = readKdlNodes(source).filter((node) => node.name === "binds");
54 if (blocks.length > 1 || blocks.some((node) => node.close === undefined))
55 throw new Error("This Niri config has an unexpected binds section. Check it in Advanced.");
56 return blocks[0];
58​
59function niriCaptureNode(node: KdlNode, appId: string) {
60 const expected = readKdlNodes(captureConfigBinding("niri", appId, "Ctrl+Shift+2"))[0]!
61 .children[0]!;
62 const action = node.children[0];
63 return (
64 node.children.length === 1 &&
65 action?.children.length === 0 &&
66 JSON.stringify(action.header.map((token) => token.value)) ===
67 JSON.stringify(expected.header.map((token) => token.value))
68 );
70​
71function hyprlandBinds(source: string, lua: boolean) {
72 const bindings: { start: number; end: number; keys: string; action: string }[] = [];
73 let start = 0;
74 for (const line of source.split(/(?<=\n)/)) {
75 const legacy =
76 !lua &&
77 /^\s*bind[a-z]*\s*=\s*([^,]*),\s*([^,]*),\s*([^,]*),\s*(.*?)\s*(?:#.*)?$/.exec(
78 line.trimEnd(),
79 );
80 const modern =
81 lua &&
82 /^\s*hl\.bind\(\s*(["'])(.*?)\1\s*,\s*hl\.dsp\.global\(\s*(["'])(.*?)\3\s*\)\s*\)\s*;?\s*(?:--.*)?$/.exec(
83 line.trimEnd(),
84 );
85 if (legacy)
86 bindings.push({
87 start,
88 end: start + line.length,
89 keys: `${legacy[1]!.trim().replace(/\s+/g, "+")}+${legacy[2]!.trim()}`,
90 action: legacy[3]!.trim() === "global" ? legacy[4]!.trim() : "",
91 });
92 if (modern)
93 bindings.push({ start, end: start + line.length, keys: modern[2]!, action: modern[4]! });
94 start += line.length;
95 }
96 return bindings;
98​
99export function niriConfigIncludes(source: string) {
100 return readKdlNodes(source)
101 .filter((node) => node.name === "include")
102 .map((node) => {
103 const path = node.header.find(
104 (token, index) => index > 0 && token.quoted && node.header[index - 1]?.value !== "=",
105 )?.value;
106 if (!path) throw new Error("Couldn't resolve a Niri include. Choose the config in Advanced.");
107 const optional = node.header.some(
108 (token, index) =>
109 token.value === "optional" &&
110 node.header[index + 1]?.value === "=" &&
111 node.header[index + 2]?.value === "true",
112 );
113 return { path, optional };
114 });
116​
117export function niriConfigConflict(source: string, appId: string, keys: string) {
118 return (
119 niriBinds(source)?.children.some(
120 (node) => sameKeys(node.name, keys) && !niriCaptureNode(node, appId),
121 ) ?? false
122 );
124​
125function removeNodes(source: string, ranges: { start: number; end: number; kept?: string }[]) {
126 let result = source;
127 for (const range of [...ranges].sort((a, b) => b.start - a.start)) {
128 const lineStart = source.lastIndexOf("\n", range.start - 1) + 1;
129 const nextLine = source.indexOf("\n", range.end);
130 const lineEnd = nextLine < 0 ? source.length : nextLine;
131 const wholeLine =
132 !range.kept &&
133 !source.slice(lineStart, range.start).trim() &&
134 !source.slice(range.end, lineEnd).trim();
135 result =
136 result.slice(0, wholeLine ? lineStart : range.start) +
137 (range.kept ?? "") +
138 result.slice(wholeLine ? Math.min(lineEnd + 1, source.length) : range.end);
139 }
140 return result;
142​
143// A top-level return must end the chunk, so appending a binding after it would break the config.
144// Block depth, not indentation, decides scope. Long brackets are rejected before this runs.
145function luaTopLevelReturn(source: string) {
146 const code = source.replace(/--[^\n]*|"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*'/g, " ");
147 let depth = 0;
148 for (const [word] of code.matchAll(/\b(?:function|if|do|repeat|end|until|return)\b/g)) {
149 if (word === "return" && depth === 0) return true;
150 if (word === "end" || word === "until") depth--;
151 else if (word !== "return") depth++;
152 }
153 return false;
155​
156export function editCaptureConfig(
157 source: string,
158 format: CaptureConfigFormat,
159 appId: string,
160 operation: "install" | "remove",
161 requestedKeys?: string,
162) {
163 if (format === "hyprland-lua" && (/\[=*\[/.test(source) || luaTopLevelReturn(source)))
164 throw new Error(
165 "This Lua config needs a manual edit. Choose your bindings file or use manual setup in Advanced.",
166 );
167 const niri = format === "niri";
168 const existing = niri
169 ? (niriBinds(source)?.children ?? [])
170 .filter((node) => niriCaptureNode(node, appId))
171 .map((node) => ({ ...node, keys: node.name }))
172 : hyprlandBinds(source, format === "hyprland-lua").filter(
173 (bind) => bind.action === `${appId}:capture-window`,
174 );
175 const keys = captureConfigKeys(requestedKeys ?? existing[0]?.keys).label;
176 if (operation === "install" && existing.length === 1 && sameKeys(existing[0]!.keys, keys))
177 return { after: source, shortcut: keys };
178 let after = removeNodes(source, existing);
179 if (operation === "remove") return { after, shortcut: keys };
180 const conflict = niri
181 ? niriConfigConflict(after, appId, keys)
182 : hyprlandBinds(after, false).some((bind) => sameKeys(bind.keys, keys));
183 if (conflict) throw new Error(`${keys} is already used in this config. Choose another shortcut.`);
184 const newline = source.includes("\r\n") ? "\r\n" : "\n";
185 const binding = captureConfigBinding(format, appId, keys);
186 if (niri) {
187 const block = niriBinds(after);
188 if (block?.close !== undefined) {
189 const lineStart = after.lastIndexOf("\n", block.close - 1) + 1;
190 const indent = after.slice(lineStart, block.close);
191 if (!indent.trim())
192 after =
193 after.slice(0, lineStart) + indent + " " + binding + newline + after.slice(lineStart);
194 else
195 after =
196 after.slice(0, block.close) +
197 newline +
198 " " +
199 binding +
200 newline +
201 after.slice(block.close);
202 } else
203 after += `${after && !after.endsWith("\n") ? newline : ""}${newline}binds {${newline} ${binding}${newline}}${newline}`;
204 readKdlNodes(after);
205 } else {
206 // Do not append executable Lua inside a block/string, or after an early return.
207 after += `${after && !after.endsWith("\n") ? newline : ""}${newline}${binding}${newline}`;
208 }
209 return { after, shortcut: keys };