klui-code 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +122 -2
- package/bin/klui.js +2 -0
- package/dist/catalog.js +99 -0
- package/dist/checks.js +146 -0
- package/dist/cli.js +126 -0
- package/dist/extension/agents.js +251 -0
- package/dist/extension/checks.js +110 -0
- package/dist/extension/commands.js +183 -0
- package/dist/extension/footer.js +58 -0
- package/dist/extension/header.js +161 -0
- package/dist/extension/index.js +310 -0
- package/dist/extension/permissions.js +784 -0
- package/dist/extension/state.js +31 -0
- package/dist/extension/todos.js +124 -0
- package/dist/extension/tools.js +251 -0
- package/dist/extension/voice.js +61 -0
- package/dist/extension/web.js +119 -0
- package/dist/extension/working.js +92 -0
- package/dist/fallback.js +198 -0
- package/dist/images.js +67 -0
- package/dist/mascot.js +73 -0
- package/dist/pass.js +122 -0
- package/dist/paths.js +23 -0
- package/dist/pi-patches.js +295 -0
- package/dist/pixels.js +107 -0
- package/dist/scroll.js +77 -0
- package/dist/update.js +132 -0
- package/dist/usage.js +111 -0
- package/npm-shrinkwrap.json +1992 -0
- package/package.json +60 -4
- package/themes/klui-arcade.json +81 -0
- package/themes/klui-dark.json +81 -0
- package/themes/klui-light.json +81 -0
|
@@ -0,0 +1,784 @@
|
|
|
1
|
+
// Permissions, like Claude Code's. Before a tool changes something, Klui may stop and ask: yes,
|
|
2
|
+
// yes and don't ask again this session, no, or no with a note for the model. Three modes, cycled
|
|
3
|
+
// with shift+tab:
|
|
4
|
+
// ask asks before every edit and every command that isn't plainly read-only
|
|
5
|
+
// auto works freely inside the project; asks before risky commands and changes outside it
|
|
6
|
+
// yolo never asks
|
|
7
|
+
// Reads, searches and web lookups never ask. Subagents can't show a dialog, so there a call that
|
|
8
|
+
// would ask is refused, and the subagent reports back instead.
|
|
9
|
+
import { decodeKittyPrintable, matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
|
|
10
|
+
import { existsSync, lstatSync, readlinkSync, realpathSync, statSync } from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
13
|
+
import { piModule } from "../pi-patches.js";
|
|
14
|
+
import { state, update } from "./state.js";
|
|
15
|
+
// The path edit and write actually use: Pi expands "~/", drops a leading "@" and so on.
|
|
16
|
+
const { resolveToCwd } = await piModule("./core/tools/path-utils.js");
|
|
17
|
+
const MODES = ["ask", "auto", "yolo"];
|
|
18
|
+
/** Subagents inherit the mode through this variable. */
|
|
19
|
+
export const PERMISSIONS_ENV = "KLUI_PERMISSIONS";
|
|
20
|
+
/** How a declined call's result starts, so its row can say "Declined" rather than show an error. */
|
|
21
|
+
export const DECLINED = "The user declined this";
|
|
22
|
+
const MODE_KEY = "shift+tab";
|
|
23
|
+
export function initialMode() {
|
|
24
|
+
const mode = process.env[PERMISSIONS_ENV];
|
|
25
|
+
return MODES.includes(mode) ? mode : "auto";
|
|
26
|
+
}
|
|
27
|
+
/** Plain-text check, kept free of UI so it can be tested: what to ask, or undefined to just run. */
|
|
28
|
+
export function check(mode, toolName, input, cwd, grants) {
|
|
29
|
+
if (mode === "yolo")
|
|
30
|
+
return undefined;
|
|
31
|
+
if (toolName === "edit" || toolName === "write") {
|
|
32
|
+
const path = resolveToCwd(String(input.path ?? ""), cwd);
|
|
33
|
+
// Decided on where the write really lands, so a symlink can't lead outside; shown as written.
|
|
34
|
+
const actual = physical(path);
|
|
35
|
+
const inside = within(actual, physical(cwd));
|
|
36
|
+
const dir = dirname(path);
|
|
37
|
+
if (inside && (mode === "auto" || grants.has("edits")))
|
|
38
|
+
return undefined;
|
|
39
|
+
if ([...grants].some((g) => g.startsWith("edits:") && within(actual, physical(g.slice(6)))))
|
|
40
|
+
return undefined;
|
|
41
|
+
const shown = inside ? relative(cwd, path) : tilde(path);
|
|
42
|
+
return {
|
|
43
|
+
title: toolName === "edit" ? `Edit ${shown}` : `${existsSync(path) ? "Overwrite" : "Create"} ${shown}`,
|
|
44
|
+
preview: [],
|
|
45
|
+
grant: inside ? { key: "edits", label: "all edits this session" } : { key: `edits:${dir}`, label: `edits in ${tilde(dir)}/ this session` },
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (toolName === "bash") {
|
|
49
|
+
const command = String(input.command ?? "").trim();
|
|
50
|
+
const { commands, hidden, structured } = scan(command);
|
|
51
|
+
const covered = (c) => readOnly(c) || grants.has(`bash:${commandPrefix(c.words)}`);
|
|
52
|
+
if (!hidden && commands.every(covered))
|
|
53
|
+
return undefined;
|
|
54
|
+
if (mode === "auto" && !hidden && !anyRisky(commands, cwd, structured))
|
|
55
|
+
return undefined;
|
|
56
|
+
const first = commands.find((c) => !covered(c)) ?? commands[0];
|
|
57
|
+
const word = first ? commandPrefix(first.words) : (command.split(/\s+/)[0] ?? "");
|
|
58
|
+
return {
|
|
59
|
+
title: "Run a command",
|
|
60
|
+
preview: command.split("\n"),
|
|
61
|
+
grant: { key: `bash:${word}`, label: `\`${word}\` commands this session` },
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
/** Whether text the shell expands runs a command: `$(…)` or backticks, not `$((…))` arithmetic. */
|
|
67
|
+
function substitutes(text) {
|
|
68
|
+
for (let i = 0; i < text.length; i++) {
|
|
69
|
+
if (text[i] === "\\")
|
|
70
|
+
i++;
|
|
71
|
+
else if (text[i] === "`" || (text[i] === "$" && text[i + 1] === "(" && text[i + 2] !== "("))
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A command line read the way the shell reads it: the simple commands it runs (split at `;`,
|
|
78
|
+
* `&&`, `||`, `|`, `&`, newlines and subshell parentheses), without comments, heredoc bodies or
|
|
79
|
+
* line continuations. `hidden` when it has something the classifier can't follow: a command
|
|
80
|
+
* substitution, or a quote, heredoc or `[[` that never closes. `structured` when it has
|
|
81
|
+
* subshells, pipes, background jobs, `||`, conditionals, loops or braces: commands that may run
|
|
82
|
+
* in another order, more than once, not at all, or in a shell of their own.
|
|
83
|
+
*/
|
|
84
|
+
function scan(command) {
|
|
85
|
+
const commands = [];
|
|
86
|
+
const n = command.length;
|
|
87
|
+
let hidden = false;
|
|
88
|
+
let structured = false;
|
|
89
|
+
let words = [];
|
|
90
|
+
let targets = [];
|
|
91
|
+
let word = "";
|
|
92
|
+
let started = false;
|
|
93
|
+
let quoted = false;
|
|
94
|
+
let redirect = false;
|
|
95
|
+
let heredoc = 0; // 1 after `<<`, 2 after `<<-`
|
|
96
|
+
let heredocs = [];
|
|
97
|
+
// Inside `[[ … ]]`, where `<`, `>`, `(`, `|` and `&` are part of the condition.
|
|
98
|
+
let test = false;
|
|
99
|
+
const endWord = () => {
|
|
100
|
+
if (started) {
|
|
101
|
+
if (!quoted && !redirect && !heredoc && words.length === 0 && STRUCTURE.has(word))
|
|
102
|
+
structured = true;
|
|
103
|
+
if (heredoc)
|
|
104
|
+
heredocs.push({ delimiter: word, literal: quoted, tabs: heredoc === 2 });
|
|
105
|
+
else
|
|
106
|
+
(redirect ? targets : words).push(word);
|
|
107
|
+
if (!quoted && word === "[[")
|
|
108
|
+
test = true;
|
|
109
|
+
if (!quoted && word === "]]")
|
|
110
|
+
test = false;
|
|
111
|
+
heredoc = 0;
|
|
112
|
+
redirect = false;
|
|
113
|
+
}
|
|
114
|
+
word = "";
|
|
115
|
+
started = false;
|
|
116
|
+
quoted = false;
|
|
117
|
+
};
|
|
118
|
+
const endCommand = () => {
|
|
119
|
+
endWord();
|
|
120
|
+
if (words.length || targets.length)
|
|
121
|
+
commands.push({ words, targets });
|
|
122
|
+
words = [];
|
|
123
|
+
targets = [];
|
|
124
|
+
redirect = false;
|
|
125
|
+
};
|
|
126
|
+
/** Skips the bodies of the heredocs opened on the line that just ended; returns where the next line starts. */
|
|
127
|
+
const skipHeredocs = (start) => {
|
|
128
|
+
let at = start;
|
|
129
|
+
for (const doc of heredocs) {
|
|
130
|
+
for (;;) {
|
|
131
|
+
if (at >= n) {
|
|
132
|
+
hidden = true;
|
|
133
|
+
return n;
|
|
134
|
+
}
|
|
135
|
+
const eol = command.indexOf("\n", at);
|
|
136
|
+
const line = command.slice(at, eol === -1 ? n : eol);
|
|
137
|
+
at = eol === -1 ? n : eol + 1;
|
|
138
|
+
if ((doc.tabs ? line.replace(/^\t+/, "") : line) === doc.delimiter)
|
|
139
|
+
break;
|
|
140
|
+
if (!doc.literal && substitutes(line))
|
|
141
|
+
hidden = true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
heredocs = [];
|
|
145
|
+
return at;
|
|
146
|
+
};
|
|
147
|
+
for (let i = 0; i < n && !hidden; i++) {
|
|
148
|
+
const c = command[i];
|
|
149
|
+
if (c === "'") {
|
|
150
|
+
const close = command.indexOf("'", i + 1);
|
|
151
|
+
if (close === -1) {
|
|
152
|
+
hidden = true;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
word += command.slice(i + 1, close);
|
|
156
|
+
started = quoted = true;
|
|
157
|
+
i = close;
|
|
158
|
+
}
|
|
159
|
+
else if (c === '"') {
|
|
160
|
+
started = quoted = true;
|
|
161
|
+
let j = i + 1;
|
|
162
|
+
for (; j < n && command[j] !== '"'; j++) {
|
|
163
|
+
const d = command[j];
|
|
164
|
+
// Inside double quotes a backslash only escapes $ ` " \ and newline.
|
|
165
|
+
if (d === "\\" && j + 1 < n && '$`"\\\n'.includes(command[j + 1])) {
|
|
166
|
+
if (command[j + 1] !== "\n")
|
|
167
|
+
word += command[j + 1];
|
|
168
|
+
j++;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
if (d === "`" || (d === "$" && command[j + 1] === "(" && command[j + 2] !== "("))
|
|
172
|
+
hidden = true;
|
|
173
|
+
word += d;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (j >= n)
|
|
177
|
+
hidden = true;
|
|
178
|
+
i = j;
|
|
179
|
+
}
|
|
180
|
+
else if (c === "\\") {
|
|
181
|
+
// A backslash at the end of a line just continues it.
|
|
182
|
+
if (i + 1 < n && command[i + 1] !== "\n") {
|
|
183
|
+
word += command[i + 1];
|
|
184
|
+
started = quoted = true;
|
|
185
|
+
}
|
|
186
|
+
i++;
|
|
187
|
+
}
|
|
188
|
+
else if (c === "#" && !started) {
|
|
189
|
+
while (i + 1 < n && command[i + 1] !== "\n")
|
|
190
|
+
i++;
|
|
191
|
+
}
|
|
192
|
+
else if (c === "$" && command.startsWith("((", i + 1)) {
|
|
193
|
+
// `$((1 + 2))` is arithmetic; only a command substitution inside it runs anything.
|
|
194
|
+
let depth = 0;
|
|
195
|
+
let j = i + 1;
|
|
196
|
+
for (; j < n; j++) {
|
|
197
|
+
if (command[j] === "(")
|
|
198
|
+
depth++;
|
|
199
|
+
else if (command[j] === ")" && --depth === 0)
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
if (j >= n || substitutes(command.slice(i + 3, j)))
|
|
203
|
+
hidden = true;
|
|
204
|
+
word += command.slice(i, j + 1);
|
|
205
|
+
started = true;
|
|
206
|
+
i = j;
|
|
207
|
+
}
|
|
208
|
+
else if (c === "`" || (c === "$" && command[i + 1] === "(") || ((c === "<" || c === ">") && command[i + 1] === "(" && !test)) {
|
|
209
|
+
hidden = true;
|
|
210
|
+
}
|
|
211
|
+
else if (c === "\n") {
|
|
212
|
+
endCommand();
|
|
213
|
+
if (heredocs.length)
|
|
214
|
+
i = skipHeredocs(i + 1) - 1;
|
|
215
|
+
}
|
|
216
|
+
else if (test && c === "]" && command[i + 1] === "]" && !started) {
|
|
217
|
+
// `]]` ends the condition even with a redirect right after it.
|
|
218
|
+
word = "]]";
|
|
219
|
+
started = true;
|
|
220
|
+
i++;
|
|
221
|
+
endWord();
|
|
222
|
+
}
|
|
223
|
+
else if (test && "()|&<>".includes(c)) {
|
|
224
|
+
word += c;
|
|
225
|
+
started = true;
|
|
226
|
+
}
|
|
227
|
+
else if (c === ";" || c === "(" || c === ")" || c === "|" || (c === "&" && command[i + 1] !== ">")) {
|
|
228
|
+
endCommand();
|
|
229
|
+
// Only `;`, `&&` and newlines run one command after another in this shell.
|
|
230
|
+
if (c !== ";" && !(c === "&" && command[i + 1] === "&"))
|
|
231
|
+
structured = true;
|
|
232
|
+
// `&&` and `||` are one separator.
|
|
233
|
+
if ((c === "&" || c === "|") && command[i + 1] === c)
|
|
234
|
+
i++;
|
|
235
|
+
}
|
|
236
|
+
else if (/\s/.test(c)) {
|
|
237
|
+
endWord();
|
|
238
|
+
}
|
|
239
|
+
else if (c === ">") {
|
|
240
|
+
// `2>` and `&>`: the descriptor before it isn't a word.
|
|
241
|
+
if (started && !quoted && !redirect && !heredoc && /^(\d+|&)$/.test(word)) {
|
|
242
|
+
word = "";
|
|
243
|
+
started = false;
|
|
244
|
+
}
|
|
245
|
+
else
|
|
246
|
+
endWord();
|
|
247
|
+
while (command[i + 1] === ">" || command[i + 1] === "|")
|
|
248
|
+
i++;
|
|
249
|
+
const copy = /^&(\d+|-)(?=[\s;&|)]|$)/.exec(command.slice(i + 1));
|
|
250
|
+
if (copy)
|
|
251
|
+
i += copy[0].length;
|
|
252
|
+
else {
|
|
253
|
+
if (command[i + 1] === "&")
|
|
254
|
+
i++;
|
|
255
|
+
redirect = true;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
else if (c === "<") {
|
|
259
|
+
// Input (`<file`, `<<<text`) writes nothing; `<<EOF` starts a heredoc.
|
|
260
|
+
if (started && !quoted && /^\d+$/.test(word)) {
|
|
261
|
+
word = "";
|
|
262
|
+
started = false;
|
|
263
|
+
}
|
|
264
|
+
else
|
|
265
|
+
endWord();
|
|
266
|
+
let count = 1;
|
|
267
|
+
while (command[i + 1] === "<") {
|
|
268
|
+
i++;
|
|
269
|
+
count++;
|
|
270
|
+
}
|
|
271
|
+
if (count === 2) {
|
|
272
|
+
heredoc = command[i + 1] === "-" ? 2 : 1;
|
|
273
|
+
if (heredoc === 2)
|
|
274
|
+
i++;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
word += c;
|
|
279
|
+
started = true;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (!hidden) {
|
|
283
|
+
endCommand();
|
|
284
|
+
if (heredocs.length || heredoc || test)
|
|
285
|
+
hidden = true;
|
|
286
|
+
}
|
|
287
|
+
return { commands, hidden, structured };
|
|
288
|
+
}
|
|
289
|
+
// Commands that run the rest of their line as another command, with their options that take a value.
|
|
290
|
+
const WRAPPERS = {
|
|
291
|
+
env: ["-u", "-C", "-S", "--unset", "--chdir", "--split-string"],
|
|
292
|
+
command: [],
|
|
293
|
+
builtin: [],
|
|
294
|
+
exec: ["-a"],
|
|
295
|
+
nice: ["-n", "--adjustment"],
|
|
296
|
+
nohup: [],
|
|
297
|
+
time: ["-f", "-o", "--format", "--output"],
|
|
298
|
+
timeout: ["-s", "-k", "--signal", "--kill-after"],
|
|
299
|
+
xargs: ["-I", "-L", "-n", "-P", "-d", "-E", "-s", "-a", "--arg-file", "--delimiter", "--max-args", "--max-procs", "--max-lines", "--replace", "--eof"],
|
|
300
|
+
stdbuf: ["-i", "-o", "-e"],
|
|
301
|
+
caffeinate: ["-t", "-w"],
|
|
302
|
+
};
|
|
303
|
+
// Shell grammar that makes commands run conditionally, repeatedly or out of order.
|
|
304
|
+
const STRUCTURE = new Set(["if", "then", "else", "elif", "fi", "do", "done", "while", "until", "for", "case", "esac", "select", "function", "{", "}"]);
|
|
305
|
+
// Shell grammar around a command: `if rm …`, `then rm …`, `{ rm …; }`.
|
|
306
|
+
const KEYWORDS = new Set(["if", "then", "else", "elif", "do", "while", "until", "!", "{", "}", "fi", "done", "esac"]);
|
|
307
|
+
/** Leading `FOO=1`s and shell keywords. */
|
|
308
|
+
function bare(words) {
|
|
309
|
+
const i = words.findIndex((w) => !/^\w+=/.test(w) && !KEYWORDS.has(w));
|
|
310
|
+
return i === -1 ? [] : words.slice(i);
|
|
311
|
+
}
|
|
312
|
+
/** The command a segment runs: without leading `FOO=1`s, keywords and wrappers like `env -u X` or `nice -n 5`. */
|
|
313
|
+
function core(words) {
|
|
314
|
+
let i = 0;
|
|
315
|
+
while (i < words.length) {
|
|
316
|
+
if (/^\w+=/.test(words[i]) || KEYWORDS.has(words[i])) {
|
|
317
|
+
i++;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
const wrapper = basename(words[i]);
|
|
321
|
+
const takesValue = Object.hasOwn(WRAPPERS, wrapper) ? WRAPPERS[wrapper] : undefined;
|
|
322
|
+
if (!takesValue)
|
|
323
|
+
break;
|
|
324
|
+
i++;
|
|
325
|
+
while (i < words.length && (words[i].startsWith("-") || /^\w+=/.test(words[i]) || (wrapper === "timeout" && /^\d/.test(words[i])))) {
|
|
326
|
+
if (takesValue.includes(words[i]))
|
|
327
|
+
i++;
|
|
328
|
+
i++;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return words.slice(i);
|
|
332
|
+
}
|
|
333
|
+
/** Git's arguments after its global options (`git -C dir --no-pager log` → `log`). */
|
|
334
|
+
function gitArgs(command) {
|
|
335
|
+
let i = 1;
|
|
336
|
+
while (i < command.length && command[i].startsWith("-")) {
|
|
337
|
+
if (["-C", "-c", "--git-dir", "--work-tree", "--namespace", "--exec-path", "--config-env"].includes(command[i]))
|
|
338
|
+
i++;
|
|
339
|
+
i++;
|
|
340
|
+
}
|
|
341
|
+
return command.slice(i);
|
|
342
|
+
}
|
|
343
|
+
const TWO_WORD = new Set(["git", "npm", "pnpm", "yarn", "bun", "npx", "cargo", "go", "docker", "kubectl", "pip", "pip3", "uv", "brew", "make"]);
|
|
344
|
+
/** The command a session grant is for: `npm run`, `git commit`, `python`. Leading `FOO=1` and wrappers are skipped. */
|
|
345
|
+
function commandPrefix(words) {
|
|
346
|
+
const command = core(words);
|
|
347
|
+
const word = command[0] ?? "";
|
|
348
|
+
const sub = word === "git" ? gitArgs(command)[0] : command[1];
|
|
349
|
+
return TWO_WORD.has(word) && sub && !sub.startsWith("-") ? `${word} ${sub}` : word;
|
|
350
|
+
}
|
|
351
|
+
export function prefix(part) {
|
|
352
|
+
return commandPrefix(scan(part).commands[0]?.words ?? []);
|
|
353
|
+
}
|
|
354
|
+
const READ = new Set([
|
|
355
|
+
"ls", "cat", "head", "tail", "wc", "grep", "rg", "pwd", "echo", "which", "whereis", "file", "stat", "du", "df", "tree", "sort",
|
|
356
|
+
"cut", "diff", "[", "[[", "]]", "test", "date", "whoami", "uname", "basename", "dirname", "realpath", "jq", "true", "type", "cd", "find",
|
|
357
|
+
]);
|
|
358
|
+
const GIT_READ = new Set(["status", "log", "diff", "show", "rev-parse", "ls-files", "blame", "describe"]);
|
|
359
|
+
const BRANCH_LIST = new Set(["-a", "-r", "-v", "-vv", "-l", "--all", "--remotes", "--verbose", "--list", "--show-current", "--no-color"]);
|
|
360
|
+
/** Plainly read-only: a known reading command, run directly, writing no file. */
|
|
361
|
+
function readOnly({ words, targets }) {
|
|
362
|
+
if (targets.some((t) => t !== "/dev/null"))
|
|
363
|
+
return false;
|
|
364
|
+
const command = bare(words);
|
|
365
|
+
if (command.length === 0)
|
|
366
|
+
return true; // `fi`, `done`
|
|
367
|
+
const word = basename(command[0]);
|
|
368
|
+
const args = command.slice(1);
|
|
369
|
+
if (word === "git") {
|
|
370
|
+
const [sub, ...rest] = gitArgs(command);
|
|
371
|
+
// `git branch` and `git remote` also create, move and delete; only their listing forms are reads.
|
|
372
|
+
if (sub === "branch")
|
|
373
|
+
return rest.every((a) => BRANCH_LIST.has(a));
|
|
374
|
+
if (sub === "remote")
|
|
375
|
+
return rest.length === 0 || (rest.length === 1 && rest[0] === "-v") || rest[0] === "show" || rest[0] === "get-url";
|
|
376
|
+
return GIT_READ.has(sub ?? "") && !rest.some((a) => a.startsWith("--output"));
|
|
377
|
+
}
|
|
378
|
+
if (word === "find")
|
|
379
|
+
return !args.some((a) => /^-(exec|execdir|ok|okdir|delete|fprint0?|fprintf|fls)$/.test(a));
|
|
380
|
+
if (word === "sort" || word === "tree")
|
|
381
|
+
return !args.some((a) => /^(-o|--output)/.test(a));
|
|
382
|
+
return READ.has(word);
|
|
383
|
+
}
|
|
384
|
+
const RISKY = new Set([
|
|
385
|
+
"rm", "rmdir", "sudo", "su", "doas", "dd", "shred", "chmod", "chown", "kill", "killall", "pkill", "shutdown", "reboot",
|
|
386
|
+
"launchctl", "crontab", "diskutil", "mkfs", "sh", "bash", "zsh", "dash", "fish", "eval",
|
|
387
|
+
]);
|
|
388
|
+
/**
|
|
389
|
+
* What auto mode still asks about: deleting, privileges, publishing, rewriting history, writing
|
|
390
|
+
* outside the project. Follows the one `cd` that starts a plain sequence (`cd sub && npm test >
|
|
391
|
+
* log`), so a redirect is judged where it really lands; after any other `cd` (conditional,
|
|
392
|
+
* repeated, in a subshell or pipe), where it lands can't be told.
|
|
393
|
+
*/
|
|
394
|
+
function anyRisky(commands, cwd, structured) {
|
|
395
|
+
const root = physical(cwd);
|
|
396
|
+
const moves = (c) => /^(cd|pushd|popd)$/.test(core(c.words)[0] ?? "");
|
|
397
|
+
const cds = commands.filter(moves).length;
|
|
398
|
+
let dir = structured && cds > 0 ? undefined : cwd;
|
|
399
|
+
for (const [index, { words, targets }] of commands.entries()) {
|
|
400
|
+
const command = core(words);
|
|
401
|
+
// A wrapper's options are guessed, so every word after one is tried as the command it runs.
|
|
402
|
+
const wrapped = command.length < bare(words).length;
|
|
403
|
+
const starts = wrapped ? words.map((_, i) => i) : [words.length - command.length];
|
|
404
|
+
if (starts.some((i) => riskyCommand(words.slice(i), dir)))
|
|
405
|
+
return true;
|
|
406
|
+
for (const raw of [...targets, ...destinations(command)]) {
|
|
407
|
+
if (raw === "/dev/null")
|
|
408
|
+
continue;
|
|
409
|
+
// Expansions, globs, or a directory that can't be told: can't tell where it lands.
|
|
410
|
+
if (dir === undefined || /[$*?{}[\]]|^~[^/]/.test(raw))
|
|
411
|
+
return true;
|
|
412
|
+
if (!within(physical(at(dir, raw)), root))
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
if (moves({ words, targets }))
|
|
416
|
+
dir = index === 0 && cds === 1 ? afterCd(command, dir) : undefined;
|
|
417
|
+
}
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
// File-writing commands: the options of each that take a value, and where its output goes. The
|
|
421
|
+
// values matter for where the names are: `touch -r ref file` writes file, not ref.
|
|
422
|
+
const WRITERS = {
|
|
423
|
+
cp: { takes: ["-S", "--suffix", "-t", "--target-directory"], writes: (n, v) => intoOrLast(n, v) },
|
|
424
|
+
mv: { takes: ["-S", "--suffix", "-t", "--target-directory"], writes: (n, v) => intoOrLast(n, v) },
|
|
425
|
+
ln: { takes: ["-S", "--suffix", "-t", "--target-directory"], writes: (n, v) => intoOrLast(n, v) },
|
|
426
|
+
install: {
|
|
427
|
+
takes: ["-m", "--mode", "-o", "--owner", "-g", "--group", "-S", "--suffix", "-t", "--target-directory"],
|
|
428
|
+
// `install -d a b` makes every directory it names.
|
|
429
|
+
writes: (n, v, args) => (args.some((a) => a === "--directory" || /^-[a-zA-Z]*d/.test(a)) ? n : intoOrLast(n, v)),
|
|
430
|
+
},
|
|
431
|
+
rsync: {
|
|
432
|
+
takes: ["-e", "--rsh", "--exclude", "--include", "--filter", "-f", "--exclude-from", "--include-from", "--files-from", "--log-file", "--temp-dir", "-T", "--backup-dir", "--partial-dir", "--chmod", "--chown", "--rsync-path"],
|
|
433
|
+
// Backup, temp and partial folders that aren't absolute are taken from the receiving folder:
|
|
434
|
+
// the destination when it's plainly a folder, otherwise maybe its parent (a single file), so
|
|
435
|
+
// both. Joined as text, so `..` is followed through symlinks rather than collapsed.
|
|
436
|
+
writes: (n, v) => {
|
|
437
|
+
const into = n.at(-1);
|
|
438
|
+
const bases = into === undefined ? ["."] : /(^|\/)\.{0,2}$/.test(into) ? [into] : [into, dirname(into)];
|
|
439
|
+
const extra = v("--backup-dir", "--temp-dir", "-T", "--partial-dir").flatMap((d) => (isAbsolute(d) ? [d] : bases.map((b) => `${b}/${d}`)));
|
|
440
|
+
return [...n.slice(-1), ...v("--log-file"), ...extra];
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
tee: { takes: [], writes: (n) => n },
|
|
444
|
+
touch: { takes: ["-r", "--reference", "-d", "--date", "-t"], writes: (n) => n },
|
|
445
|
+
mkdir: { takes: ["-m", "--mode"], writes: (n) => n },
|
|
446
|
+
truncate: { takes: ["-s", "--size", "-r", "--reference"], writes: (n) => n },
|
|
447
|
+
curl: {
|
|
448
|
+
takes: ["-o", "--output", "-d", "--data", "--data-raw", "--data-binary", "-H", "--header", "-X", "--request", "-u", "--user", "-A", "--user-agent", "-e", "--referer", "-F", "--form", "-T", "--upload-file", "-w", "--write-out", "-m", "--max-time", "-x", "--proxy", "-b", "--cookie", "-c", "--cookie-jar", "--output-dir"],
|
|
449
|
+
writes: (_n, v) => [...v("-o", "--output", "-c", "--cookie-jar"), ...v("--output-dir")],
|
|
450
|
+
},
|
|
451
|
+
wget: { takes: ["-O", "--output-document", "-P", "--directory-prefix", "-o", "--output-file", "-a", "--append-output"], writes: (_n, v) => v("-O", "--output-document", "-P", "--directory-prefix", "-o", "--output-file", "-a", "--append-output") },
|
|
452
|
+
unzip: { takes: ["-d"], writes: (_n, v) => v("-d") },
|
|
453
|
+
};
|
|
454
|
+
/** cp, mv and friends: the directory after -t, or else the last name. */
|
|
455
|
+
function intoOrLast(named, values) {
|
|
456
|
+
const into = values("-t", "--target-directory");
|
|
457
|
+
return into.length ? into : named.slice(-1);
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* A command's options and names, the way getopt reads them: `--flag=value`, `--flag value`,
|
|
461
|
+
* `-fvalue`, `-f value`, and a value-taking letter at the end of a cluster (`-sSLo file`).
|
|
462
|
+
*/
|
|
463
|
+
function options(args, takes) {
|
|
464
|
+
const named = [];
|
|
465
|
+
const found = [];
|
|
466
|
+
for (let i = 0; i < args.length; i++) {
|
|
467
|
+
const arg = args[i];
|
|
468
|
+
if (arg === "--") {
|
|
469
|
+
named.push(...args.slice(i + 1));
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
if (arg.startsWith("--")) {
|
|
473
|
+
const eq = arg.indexOf("=");
|
|
474
|
+
const flag = eq === -1 ? arg : arg.slice(0, eq);
|
|
475
|
+
if (eq !== -1)
|
|
476
|
+
found.push([flag, arg.slice(eq + 1)]);
|
|
477
|
+
else if (takes.includes(flag))
|
|
478
|
+
found.push([flag, args[++i] ?? ""]);
|
|
479
|
+
}
|
|
480
|
+
else if (arg.startsWith("-") && arg.length > 1) {
|
|
481
|
+
for (let j = 1; j < arg.length; j++) {
|
|
482
|
+
const flag = `-${arg[j]}`;
|
|
483
|
+
if (!takes.includes(flag))
|
|
484
|
+
continue;
|
|
485
|
+
found.push([flag, arg.slice(j + 1) || (args[++i] ?? "")]);
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
else if (arg) {
|
|
490
|
+
named.push(arg);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return { named, values: (...flags) => found.filter(([f]) => flags.includes(f)).map(([, v]) => v) };
|
|
494
|
+
}
|
|
495
|
+
/** The files a command writes to, for the common ones that take them as arguments. */
|
|
496
|
+
function destinations(command) {
|
|
497
|
+
const word = basename(command[0] ?? "");
|
|
498
|
+
const args = command.slice(1);
|
|
499
|
+
if (word === "sed") {
|
|
500
|
+
// In place only with -i (`-i`, `-i.bak`, `-Ei`, `--in-place`; macOS takes the suffix apart, as in
|
|
501
|
+
// `-i ''` or `-i .bak`). The script is the first name unless -e or -f gave it.
|
|
502
|
+
const inPlace = args.findIndex((a) => /^--in-place/.test(a) || /^-[a-zA-Z]*i/.test(a));
|
|
503
|
+
if (inPlace === -1)
|
|
504
|
+
return [];
|
|
505
|
+
const suffix = /^-[a-zA-Z]*i$/.test(args[inPlace]) && /^(\.[\w.-]*)?$/.test(args[inPlace + 1] ?? "-");
|
|
506
|
+
const rest = args.filter((_, i) => !(suffix && i === inPlace + 1));
|
|
507
|
+
const { named, values } = options(rest, ["-e", "--expression", "-f", "--file", "-l", "--line-length"]);
|
|
508
|
+
return values("-e", "--expression", "-f", "--file").length ? named : named.slice(1);
|
|
509
|
+
}
|
|
510
|
+
const writer = Object.hasOwn(WRITERS, word) ? WRITERS[word] : undefined;
|
|
511
|
+
if (!writer)
|
|
512
|
+
return [];
|
|
513
|
+
const { named, values } = options(args, writer.takes);
|
|
514
|
+
return writer.writes(named, values, args);
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* The working directory after a command: changed by `cd` into a folder that exists; unknown
|
|
518
|
+
* after `cd` into one that doesn't (it fails and the shell stays put), `cd -`, `cd` alone or `popd`.
|
|
519
|
+
*/
|
|
520
|
+
function afterCd(command, dir) {
|
|
521
|
+
const word = command[0];
|
|
522
|
+
if (word === "popd")
|
|
523
|
+
return undefined;
|
|
524
|
+
if (word !== "cd" && word !== "pushd")
|
|
525
|
+
return dir;
|
|
526
|
+
const target = command.slice(1).find((a) => !a.startsWith("-"));
|
|
527
|
+
if (dir === undefined || !target || /[$*?{}[\]]|^~[^/]/.test(target))
|
|
528
|
+
return undefined;
|
|
529
|
+
// Through a symlink, the shell's idea of the folder (for a later `cd ..`) and the real one differ.
|
|
530
|
+
const next = physical(at(dir, target));
|
|
531
|
+
const lexical = resolve(physical(dir) ?? dir, target.replace(/^~(?=\/|$)/, homedir()));
|
|
532
|
+
return next !== undefined && next === lexical && isDirectory(next) ? next : undefined;
|
|
533
|
+
}
|
|
534
|
+
function isDirectory(path) {
|
|
535
|
+
try {
|
|
536
|
+
return statSync(path).isDirectory();
|
|
537
|
+
}
|
|
538
|
+
catch {
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
/** The folder git works in: `-C` moves it; `--work-tree` or `--git-dir` make it unknown. */
|
|
543
|
+
function gitDir(command, dir) {
|
|
544
|
+
for (let i = 1; i < command.length && command[i].startsWith("-"); i++) {
|
|
545
|
+
if (command[i] === "-C") {
|
|
546
|
+
const next = command[++i];
|
|
547
|
+
dir = dir === undefined || next === undefined || /[$*?{}[\]`]|^~[^/]/.test(next) ? undefined : physical(at(dir, next));
|
|
548
|
+
}
|
|
549
|
+
else if (/^--(work-tree|git-dir)/.test(command[i]))
|
|
550
|
+
dir = undefined;
|
|
551
|
+
else if (["-c", "--namespace", "--exec-path", "--config-env"].includes(command[i]))
|
|
552
|
+
i++;
|
|
553
|
+
}
|
|
554
|
+
return dir;
|
|
555
|
+
}
|
|
556
|
+
/** A path as the shell would take it from `dir`, with `~` expanded. */
|
|
557
|
+
function at(dir, path) {
|
|
558
|
+
const expanded = path.replace(/^~(?=\/|$)/, homedir());
|
|
559
|
+
return isAbsolute(expanded) ? expanded : `${dir}/${expanded}`;
|
|
560
|
+
}
|
|
561
|
+
function riskyCommand(command, dir) {
|
|
562
|
+
const word = basename(command[0] ?? "");
|
|
563
|
+
if (RISKY.has(word) || word.startsWith("mkfs"))
|
|
564
|
+
return true;
|
|
565
|
+
const args = command.slice(1);
|
|
566
|
+
if (word === "git") {
|
|
567
|
+
const [sub, ...rest] = gitArgs(command);
|
|
568
|
+
if (sub === "switch")
|
|
569
|
+
return rest.some((a) => /^(-f|--force|--discard-changes)$/.test(a));
|
|
570
|
+
if (sub === "checkout") {
|
|
571
|
+
if (rest.some((a) => /^(--|-f|--force|-p|--patch|--ours|--theirs|-m|--merge)$/.test(a)))
|
|
572
|
+
return true;
|
|
573
|
+
const named = rest.filter((a) => !a.startsWith("-"));
|
|
574
|
+
if (rest.some((a) => /^(-b|-B|--orphan)$/.test(a)))
|
|
575
|
+
return named.length > 2;
|
|
576
|
+
// `git checkout main` switches branches; `git checkout src/app.ts` throws away its changes.
|
|
577
|
+
const where = gitDir(command, dir);
|
|
578
|
+
return named.length > 1 || named.some((p) => p === "." || /[*?[$`]/.test(p) || where === undefined || existsSync(at(where, p)));
|
|
579
|
+
}
|
|
580
|
+
if (sub === "branch")
|
|
581
|
+
return rest.some((a) => /^(-[a-zA-Z]*[DfM][a-zA-Z]*|--delete|--force|--move)$/.test(a));
|
|
582
|
+
return /^(push|clean|restore|rebase|update-ref|filter-branch|filter-repo)\b|^reset\b.*--hard|^stash\s+(drop|clear)\b|^reflog\s+expire\b/.test([sub, ...rest].join(" "));
|
|
583
|
+
}
|
|
584
|
+
if (word === "find")
|
|
585
|
+
return args.some((a) => /^-(delete|exec|execdir|ok|okdir)$/.test(a));
|
|
586
|
+
// Their global options can come first (`npm --registry x publish`), so any word counts.
|
|
587
|
+
if (/^(npm|pnpm|yarn|bun|cargo)$/.test(word))
|
|
588
|
+
return args.includes("publish");
|
|
589
|
+
if (word === "docker")
|
|
590
|
+
return args.some((a) => a === "rm" || a === "rmi" || a === "prune");
|
|
591
|
+
return false;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Where a path really leads, following symlinks the way the OS does, including one whose target
|
|
595
|
+
* doesn't exist yet. Undefined when it can't be told (a symlink loop).
|
|
596
|
+
*/
|
|
597
|
+
function physical(path, depth = 0) {
|
|
598
|
+
if (process.platform === "win32")
|
|
599
|
+
return physicalWindows(path);
|
|
600
|
+
if (depth > 40)
|
|
601
|
+
return undefined;
|
|
602
|
+
const parts = path.split("/").filter(Boolean);
|
|
603
|
+
let current = "/";
|
|
604
|
+
for (let i = 0; i < parts.length; i++) {
|
|
605
|
+
const part = parts[i];
|
|
606
|
+
if (part === ".")
|
|
607
|
+
continue;
|
|
608
|
+
if (part === "..") {
|
|
609
|
+
current = dirname(current);
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
const next = join(current, part);
|
|
613
|
+
let link;
|
|
614
|
+
try {
|
|
615
|
+
if (!lstatSync(next).isSymbolicLink()) {
|
|
616
|
+
current = next;
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
link = readlinkSync(next);
|
|
620
|
+
}
|
|
621
|
+
catch {
|
|
622
|
+
// Doesn't exist: nothing further down can be a link.
|
|
623
|
+
return join(next, ...parts.slice(i + 1));
|
|
624
|
+
}
|
|
625
|
+
const target = physical(isAbsolute(link) ? link : `${current}/${link}`, depth + 1);
|
|
626
|
+
if (target === undefined)
|
|
627
|
+
return undefined;
|
|
628
|
+
current = target;
|
|
629
|
+
}
|
|
630
|
+
return current;
|
|
631
|
+
}
|
|
632
|
+
/** Windows: the nearest existing ancestor's real path (links and junctions resolved); undefined behind a dangling link. */
|
|
633
|
+
function physicalWindows(path) {
|
|
634
|
+
const full = resolve(path);
|
|
635
|
+
let existing = full;
|
|
636
|
+
while (!existsSync(existing)) {
|
|
637
|
+
try {
|
|
638
|
+
if (lstatSync(existing).isSymbolicLink())
|
|
639
|
+
return undefined;
|
|
640
|
+
}
|
|
641
|
+
catch { }
|
|
642
|
+
const up = dirname(existing);
|
|
643
|
+
if (up === existing)
|
|
644
|
+
return full;
|
|
645
|
+
existing = up;
|
|
646
|
+
}
|
|
647
|
+
try {
|
|
648
|
+
return join(realpathSync.native(existing), relative(existing, full));
|
|
649
|
+
}
|
|
650
|
+
catch {
|
|
651
|
+
return undefined;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
function within(path, dir) {
|
|
655
|
+
if (path === undefined || dir === undefined)
|
|
656
|
+
return false;
|
|
657
|
+
const rel = relative(dir, path);
|
|
658
|
+
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
|
|
659
|
+
}
|
|
660
|
+
function tilde(path) {
|
|
661
|
+
const home = homedir();
|
|
662
|
+
return path === home || path.startsWith(`${home}/`) ? `~${path.slice(home.length)}` : path;
|
|
663
|
+
}
|
|
664
|
+
export function modeLabel(theme, mode = state.mode, withKey = true) {
|
|
665
|
+
const hint = withKey ? theme.fg("dim", ` (${MODE_KEY})`) : "";
|
|
666
|
+
if (mode === "yolo")
|
|
667
|
+
return `${theme.fg("error", "⏵⏵⏵ yolo mode on")}${hint}`;
|
|
668
|
+
if (mode === "auto")
|
|
669
|
+
return `${theme.fg("accent", "⏵⏵ auto mode on")}${hint}`;
|
|
670
|
+
return `${theme.fg("muted", "⏵ ask mode on")}${hint}`;
|
|
671
|
+
}
|
|
672
|
+
// ── Wiring ──────────────────────────────────────────────────────────────────────────────────
|
|
673
|
+
const PREVIEW_LINES = 10;
|
|
674
|
+
const OPTIONS = ["Yes", "", "No", "No, and tell Klui what to do instead"];
|
|
675
|
+
export function registerPermissions(pi, subagent) {
|
|
676
|
+
const grants = new Set();
|
|
677
|
+
// One dialog at a time: parallel tool calls queue up, and each re-checks once its turn comes,
|
|
678
|
+
// since a "don't ask again" answer may already cover it.
|
|
679
|
+
let queue = Promise.resolve();
|
|
680
|
+
if (!subagent)
|
|
681
|
+
pi.registerShortcut(MODE_KEY, { description: "Cycle permission mode (ask, auto, yolo)", handler: nextMode });
|
|
682
|
+
pi.on("session_shutdown", async () => grants.clear());
|
|
683
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
684
|
+
const ask = () => check(state.mode, event.toolName, event.input, ctx.cwd, grants);
|
|
685
|
+
if (!ask())
|
|
686
|
+
return undefined;
|
|
687
|
+
if (!ctx.hasUI) {
|
|
688
|
+
return {
|
|
689
|
+
block: true,
|
|
690
|
+
reason: `Klui is in ${state.mode} mode, so this needs the user's OK, and nobody can be asked from here. Don't work around it: say what you wanted to do in your report.`,
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
const turn = queue.then(async () => {
|
|
694
|
+
const request = ask();
|
|
695
|
+
if (!request)
|
|
696
|
+
return undefined;
|
|
697
|
+
if (event.toolName === "edit")
|
|
698
|
+
request.preview = editPreview(event.input);
|
|
699
|
+
if (event.toolName === "write")
|
|
700
|
+
request.preview = String(event.input.content ?? "").split("\n").map((l) => `+${l}`);
|
|
701
|
+
const answer = await prompt(ctx, request, () => ask() !== undefined);
|
|
702
|
+
if (answer === 0)
|
|
703
|
+
return undefined;
|
|
704
|
+
if (answer === 1) {
|
|
705
|
+
grants.add(request.grant.key);
|
|
706
|
+
return undefined;
|
|
707
|
+
}
|
|
708
|
+
const note = answer === 3 ? (await ctx.ui.input("What should Klui do instead?", "tell Klui…"))?.trim() : undefined;
|
|
709
|
+
// A plain "no" ends the turn, so the attendee can say what they want instead.
|
|
710
|
+
return note
|
|
711
|
+
? { block: true, reason: `${DECLINED} and said: ${note}` }
|
|
712
|
+
: { block: true, terminate: true, reason: `${DECLINED}. Don't retry it; wait for their next message.` };
|
|
713
|
+
});
|
|
714
|
+
queue = turn.catch(() => undefined);
|
|
715
|
+
return turn;
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
function editPreview(input) {
|
|
719
|
+
const edits = Array.isArray(input.edits) ? input.edits : [];
|
|
720
|
+
return edits.flatMap((e, i) => [
|
|
721
|
+
...(i > 0 ? ["…"] : []),
|
|
722
|
+
...String(e.oldText ?? "").split("\n").map((l) => `-${l}`),
|
|
723
|
+
...String(e.newText ?? "").split("\n").map((l) => `+${l}`),
|
|
724
|
+
]);
|
|
725
|
+
}
|
|
726
|
+
function nextMode() {
|
|
727
|
+
update({ mode: MODES[(MODES.indexOf(state.mode) + 1) % MODES.length] });
|
|
728
|
+
}
|
|
729
|
+
/** Show the dialog; resolves to the chosen option's index (esc is "No"). */
|
|
730
|
+
async function prompt(ctx, request, stillAsks) {
|
|
731
|
+
const options = OPTIONS.map((o, i) => (i === 1 ? `Yes, and don't ask again for ${request.grant.label}` : o));
|
|
732
|
+
if (ctx.mode !== "tui") {
|
|
733
|
+
const choice = await ctx.ui.select(`${request.title}\n${request.preview.slice(0, PREVIEW_LINES).join("\n")}`, options);
|
|
734
|
+
return choice === undefined ? 2 : options.indexOf(choice);
|
|
735
|
+
}
|
|
736
|
+
return ctx.ui.custom((tui, theme, _keys, done) => {
|
|
737
|
+
let selected = 0;
|
|
738
|
+
const component = {
|
|
739
|
+
render(width) {
|
|
740
|
+
const fit = (line) => truncateToWidth(line, width);
|
|
741
|
+
const shown = request.preview.slice(0, PREVIEW_LINES).map((line) => {
|
|
742
|
+
const color = line.startsWith("+") ? "toolDiffAdded" : line.startsWith("-") ? "toolDiffRemoved" : "text";
|
|
743
|
+
return fit(` ${theme.fg(color, line)}`);
|
|
744
|
+
});
|
|
745
|
+
const more = request.preview.length - PREVIEW_LINES;
|
|
746
|
+
return [
|
|
747
|
+
theme.fg("accent", "─".repeat(width)),
|
|
748
|
+
fit(` ${theme.bold(theme.fg("accent", request.title))}`),
|
|
749
|
+
"",
|
|
750
|
+
...shown,
|
|
751
|
+
...(more > 0 ? [theme.fg("dim", ` … +${more} lines`)] : []),
|
|
752
|
+
"",
|
|
753
|
+
fit(` ${theme.fg("text", "Do you want to proceed?")}`),
|
|
754
|
+
...options.map((option, i) => fit(i === selected ? ` ${theme.fg("accent", `❯ ${i + 1}. ${option}`)}` : ` ${theme.fg("text", `${i + 1}. ${option}`)}`)),
|
|
755
|
+
"",
|
|
756
|
+
fit(theme.fg("dim", ` ↑↓ choose · enter confirm · 1-4 or y/n · esc no · ${modeLabel(theme)}`)),
|
|
757
|
+
];
|
|
758
|
+
},
|
|
759
|
+
handleInput(data) {
|
|
760
|
+
const char = decodeKittyPrintable(data) ?? data;
|
|
761
|
+
const key = { y: 0, n: 2 }[char] ?? (/^[1-4]$/.test(char) ? Number(char) - 1 : undefined);
|
|
762
|
+
if (key !== undefined)
|
|
763
|
+
return done(key);
|
|
764
|
+
if (matchesKey(data, "escape"))
|
|
765
|
+
return done(2);
|
|
766
|
+
if (matchesKey(data, "enter"))
|
|
767
|
+
return done(selected);
|
|
768
|
+
if (matchesKey(data, "up") || char === "k")
|
|
769
|
+
selected = (selected + options.length - 1) % options.length;
|
|
770
|
+
else if (matchesKey(data, "down") || char === "j")
|
|
771
|
+
selected = (selected + 1) % options.length;
|
|
772
|
+
else if (matchesKey(data, MODE_KEY)) {
|
|
773
|
+
// shift+tab still switches modes here; a mode that wouldn't ask answers this one too.
|
|
774
|
+
nextMode();
|
|
775
|
+
if (!stillAsks())
|
|
776
|
+
return done(0);
|
|
777
|
+
}
|
|
778
|
+
tui.requestRender();
|
|
779
|
+
},
|
|
780
|
+
invalidate() { },
|
|
781
|
+
};
|
|
782
|
+
return component;
|
|
783
|
+
});
|
|
784
|
+
}
|