@oh-my-pi/pi-tui 1.337.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/README.md +654 -0
- package/package.json +45 -0
- package/src/autocomplete.ts +575 -0
- package/src/components/box.ts +134 -0
- package/src/components/cancellable-loader.ts +39 -0
- package/src/components/editor.ts +1342 -0
- package/src/components/image.ts +87 -0
- package/src/components/input.ts +344 -0
- package/src/components/loader.ts +55 -0
- package/src/components/markdown.ts +646 -0
- package/src/components/select-list.ts +184 -0
- package/src/components/settings-list.ts +188 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +140 -0
- package/src/components/text.ts +106 -0
- package/src/components/truncated-text.ts +65 -0
- package/src/index.ts +91 -0
- package/src/keys.ts +560 -0
- package/src/terminal-image.ts +340 -0
- package/src/terminal.ts +163 -0
- package/src/tui.ts +353 -0
- package/src/utils.ts +712 -0
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
import { readdirSync, statSync } from "fs";
|
|
2
|
+
import { homedir } from "os";
|
|
3
|
+
import { basename, dirname, join } from "path";
|
|
4
|
+
|
|
5
|
+
// Use fd to walk directory tree (fast, respects .gitignore)
|
|
6
|
+
function walkDirectoryWithFd(
|
|
7
|
+
baseDir: string,
|
|
8
|
+
fdPath: string,
|
|
9
|
+
query: string,
|
|
10
|
+
maxResults: number,
|
|
11
|
+
): Array<{ path: string; isDirectory: boolean }> {
|
|
12
|
+
const args = ["--base-directory", baseDir, "--max-results", String(maxResults), "--type", "f", "--type", "d"];
|
|
13
|
+
|
|
14
|
+
// Add query as pattern if provided
|
|
15
|
+
if (query) {
|
|
16
|
+
args.push(query);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const result = Bun.spawnSync([fdPath, ...args], {
|
|
20
|
+
stdout: "pipe",
|
|
21
|
+
stderr: "pipe",
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (!result.success || !result.stdout) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const stdout = new TextDecoder().decode(result.stdout);
|
|
29
|
+
const lines = stdout.trim().split("\n").filter(Boolean);
|
|
30
|
+
const results: Array<{ path: string; isDirectory: boolean }> = [];
|
|
31
|
+
|
|
32
|
+
for (const line of lines) {
|
|
33
|
+
// fd outputs directories with trailing /
|
|
34
|
+
const isDirectory = line.endsWith("/");
|
|
35
|
+
results.push({
|
|
36
|
+
path: line,
|
|
37
|
+
isDirectory,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return results;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface AutocompleteItem {
|
|
45
|
+
value: string;
|
|
46
|
+
label: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SlashCommand {
|
|
51
|
+
name: string;
|
|
52
|
+
description?: string;
|
|
53
|
+
// Function to get argument completions for this command
|
|
54
|
+
// Returns null if no argument completion is available
|
|
55
|
+
getArgumentCompletions?(argumentPrefix: string): AutocompleteItem[] | null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface AutocompleteProvider {
|
|
59
|
+
// Get autocomplete suggestions for current text/cursor position
|
|
60
|
+
// Returns null if no suggestions available
|
|
61
|
+
getSuggestions(
|
|
62
|
+
lines: string[],
|
|
63
|
+
cursorLine: number,
|
|
64
|
+
cursorCol: number,
|
|
65
|
+
): {
|
|
66
|
+
items: AutocompleteItem[];
|
|
67
|
+
prefix: string; // What we're matching against (e.g., "/" or "src/")
|
|
68
|
+
} | null;
|
|
69
|
+
|
|
70
|
+
// Apply the selected item
|
|
71
|
+
// Returns the new text and cursor position
|
|
72
|
+
applyCompletion(
|
|
73
|
+
lines: string[],
|
|
74
|
+
cursorLine: number,
|
|
75
|
+
cursorCol: number,
|
|
76
|
+
item: AutocompleteItem,
|
|
77
|
+
prefix: string,
|
|
78
|
+
): {
|
|
79
|
+
lines: string[];
|
|
80
|
+
cursorLine: number;
|
|
81
|
+
cursorCol: number;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Combined provider that handles both slash commands and file paths
|
|
86
|
+
export class CombinedAutocompleteProvider implements AutocompleteProvider {
|
|
87
|
+
private commands: (SlashCommand | AutocompleteItem)[];
|
|
88
|
+
private basePath: string;
|
|
89
|
+
private fdPath: string | null;
|
|
90
|
+
|
|
91
|
+
constructor(
|
|
92
|
+
commands: (SlashCommand | AutocompleteItem)[] = [],
|
|
93
|
+
basePath: string = process.cwd(),
|
|
94
|
+
fdPath: string | null = null,
|
|
95
|
+
) {
|
|
96
|
+
this.commands = commands;
|
|
97
|
+
this.basePath = basePath;
|
|
98
|
+
this.fdPath = fdPath;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getSuggestions(
|
|
102
|
+
lines: string[],
|
|
103
|
+
cursorLine: number,
|
|
104
|
+
cursorCol: number,
|
|
105
|
+
): { items: AutocompleteItem[]; prefix: string } | null {
|
|
106
|
+
const currentLine = lines[cursorLine] || "";
|
|
107
|
+
const textBeforeCursor = currentLine.slice(0, cursorCol);
|
|
108
|
+
|
|
109
|
+
// Check for @ file reference (fuzzy search) - must be after a space or at start
|
|
110
|
+
const atMatch = textBeforeCursor.match(/(?:^|[\s])(@[^\s]*)$/);
|
|
111
|
+
if (atMatch) {
|
|
112
|
+
const prefix = atMatch[1] ?? "@"; // The @... part
|
|
113
|
+
const query = prefix.slice(1); // Remove the @
|
|
114
|
+
const suggestions = this.getFuzzyFileSuggestions(query);
|
|
115
|
+
if (suggestions.length === 0) return null;
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
items: suggestions,
|
|
119
|
+
prefix: prefix,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Check for slash commands
|
|
124
|
+
if (textBeforeCursor.startsWith("/")) {
|
|
125
|
+
const spaceIndex = textBeforeCursor.indexOf(" ");
|
|
126
|
+
|
|
127
|
+
if (spaceIndex === -1) {
|
|
128
|
+
// No space yet - complete command names
|
|
129
|
+
const prefix = textBeforeCursor.slice(1); // Remove the "/"
|
|
130
|
+
const filtered = this.commands
|
|
131
|
+
.filter((cmd) => {
|
|
132
|
+
const name = "name" in cmd ? cmd.name : cmd.value; // Check if SlashCommand or AutocompleteItem
|
|
133
|
+
return name?.toLowerCase().startsWith(prefix.toLowerCase());
|
|
134
|
+
})
|
|
135
|
+
.map((cmd) => ({
|
|
136
|
+
value: "name" in cmd ? cmd.name : cmd.value,
|
|
137
|
+
label: "name" in cmd ? cmd.name : cmd.label,
|
|
138
|
+
...(cmd.description && { description: cmd.description }),
|
|
139
|
+
}));
|
|
140
|
+
|
|
141
|
+
if (filtered.length === 0) return null;
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
items: filtered,
|
|
145
|
+
prefix: textBeforeCursor,
|
|
146
|
+
};
|
|
147
|
+
} else {
|
|
148
|
+
// Space found - complete command arguments
|
|
149
|
+
const commandName = textBeforeCursor.slice(1, spaceIndex); // Command without "/"
|
|
150
|
+
const argumentText = textBeforeCursor.slice(spaceIndex + 1); // Text after space
|
|
151
|
+
|
|
152
|
+
const command = this.commands.find((cmd) => {
|
|
153
|
+
const name = "name" in cmd ? cmd.name : cmd.value;
|
|
154
|
+
return name === commandName;
|
|
155
|
+
});
|
|
156
|
+
if (!command || !("getArgumentCompletions" in command) || !command.getArgumentCompletions) {
|
|
157
|
+
return null; // No argument completion for this command
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const argumentSuggestions = command.getArgumentCompletions(argumentText);
|
|
161
|
+
if (!argumentSuggestions || argumentSuggestions.length === 0) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
items: argumentSuggestions,
|
|
167
|
+
prefix: argumentText,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Check for file paths - triggered by Tab or if we detect a path pattern
|
|
173
|
+
const pathMatch = this.extractPathPrefix(textBeforeCursor, false);
|
|
174
|
+
|
|
175
|
+
if (pathMatch !== null) {
|
|
176
|
+
const suggestions = this.getFileSuggestions(pathMatch);
|
|
177
|
+
if (suggestions.length === 0) return null;
|
|
178
|
+
|
|
179
|
+
// Check if we have an exact match that is a directory
|
|
180
|
+
// In that case, we might want to return suggestions for the directory content instead
|
|
181
|
+
// But only if the prefix ends with /
|
|
182
|
+
if (suggestions.length === 1 && suggestions[0]?.value === pathMatch && !pathMatch.endsWith("/")) {
|
|
183
|
+
// Exact match found (e.g. user typed "src" and "src/" is the only match)
|
|
184
|
+
// We still return it so user can select it and add /
|
|
185
|
+
return {
|
|
186
|
+
items: suggestions,
|
|
187
|
+
prefix: pathMatch,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
items: suggestions,
|
|
193
|
+
prefix: pathMatch,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
applyCompletion(
|
|
201
|
+
lines: string[],
|
|
202
|
+
cursorLine: number,
|
|
203
|
+
cursorCol: number,
|
|
204
|
+
item: AutocompleteItem,
|
|
205
|
+
prefix: string,
|
|
206
|
+
): { lines: string[]; cursorLine: number; cursorCol: number } {
|
|
207
|
+
const currentLine = lines[cursorLine] || "";
|
|
208
|
+
const beforePrefix = currentLine.slice(0, cursorCol - prefix.length);
|
|
209
|
+
const afterCursor = currentLine.slice(cursorCol);
|
|
210
|
+
|
|
211
|
+
// Check if we're completing a slash command (prefix starts with "/" but NOT a file path)
|
|
212
|
+
// Slash commands are at the start of the line and don't contain path separators after the first /
|
|
213
|
+
const isSlashCommand = prefix.startsWith("/") && beforePrefix.trim() === "" && !prefix.slice(1).includes("/");
|
|
214
|
+
if (isSlashCommand) {
|
|
215
|
+
// This is a command name completion
|
|
216
|
+
const newLine = `${beforePrefix}/${item.value} ${afterCursor}`;
|
|
217
|
+
const newLines = [...lines];
|
|
218
|
+
newLines[cursorLine] = newLine;
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
lines: newLines,
|
|
222
|
+
cursorLine,
|
|
223
|
+
cursorCol: beforePrefix.length + item.value.length + 2, // +2 for "/" and space
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Check if we're completing a file attachment (prefix starts with "@")
|
|
228
|
+
if (prefix.startsWith("@")) {
|
|
229
|
+
// This is a file attachment completion
|
|
230
|
+
const newLine = `${beforePrefix + item.value} ${afterCursor}`;
|
|
231
|
+
const newLines = [...lines];
|
|
232
|
+
newLines[cursorLine] = newLine;
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
lines: newLines,
|
|
236
|
+
cursorLine,
|
|
237
|
+
cursorCol: beforePrefix.length + item.value.length + 1, // +1 for space
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Check if we're in a slash command context (beforePrefix contains "/command ")
|
|
242
|
+
const textBeforeCursor = currentLine.slice(0, cursorCol);
|
|
243
|
+
if (textBeforeCursor.includes("/") && textBeforeCursor.includes(" ")) {
|
|
244
|
+
// This is likely a command argument completion
|
|
245
|
+
const newLine = beforePrefix + item.value + afterCursor;
|
|
246
|
+
const newLines = [...lines];
|
|
247
|
+
newLines[cursorLine] = newLine;
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
lines: newLines,
|
|
251
|
+
cursorLine,
|
|
252
|
+
cursorCol: beforePrefix.length + item.value.length,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// For file paths, complete the path
|
|
257
|
+
const newLine = beforePrefix + item.value + afterCursor;
|
|
258
|
+
const newLines = [...lines];
|
|
259
|
+
newLines[cursorLine] = newLine;
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
lines: newLines,
|
|
263
|
+
cursorLine,
|
|
264
|
+
cursorCol: beforePrefix.length + item.value.length,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Extract a path-like prefix from the text before cursor
|
|
269
|
+
private extractPathPrefix(text: string, forceExtract: boolean = false): string | null {
|
|
270
|
+
// Check for @ file attachment syntax first
|
|
271
|
+
const atMatch = text.match(/@([^\s]*)$/);
|
|
272
|
+
if (atMatch) {
|
|
273
|
+
return atMatch[0]; // Return the full @path pattern
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Simple approach: find the last whitespace/delimiter and extract the word after it
|
|
277
|
+
// This avoids catastrophic backtracking from nested quantifiers
|
|
278
|
+
const lastDelimiterIndex = Math.max(
|
|
279
|
+
text.lastIndexOf(" "),
|
|
280
|
+
text.lastIndexOf("\t"),
|
|
281
|
+
text.lastIndexOf('"'),
|
|
282
|
+
text.lastIndexOf("'"),
|
|
283
|
+
text.lastIndexOf("="),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
const pathPrefix = lastDelimiterIndex === -1 ? text : text.slice(lastDelimiterIndex + 1);
|
|
287
|
+
|
|
288
|
+
// For forced extraction (Tab key), always return something
|
|
289
|
+
if (forceExtract) {
|
|
290
|
+
return pathPrefix;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// For natural triggers, return if it looks like a path, ends with /, starts with ~/, .
|
|
294
|
+
// Only return empty string if the text looks like it's starting a path context
|
|
295
|
+
if (pathPrefix.includes("/") || pathPrefix.startsWith(".") || pathPrefix.startsWith("~/")) {
|
|
296
|
+
return pathPrefix;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Return empty string only if we're at the beginning of the line or after a space
|
|
300
|
+
// (not after quotes or other delimiters that don't suggest file paths)
|
|
301
|
+
if (pathPrefix === "" && (text === "" || text.endsWith(" "))) {
|
|
302
|
+
return pathPrefix;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Expand home directory (~/) to actual home path
|
|
309
|
+
private expandHomePath(path: string): string {
|
|
310
|
+
if (path.startsWith("~/")) {
|
|
311
|
+
const expandedPath = join(homedir(), path.slice(2));
|
|
312
|
+
// Preserve trailing slash if original path had one
|
|
313
|
+
return path.endsWith("/") && !expandedPath.endsWith("/") ? `${expandedPath}/` : expandedPath;
|
|
314
|
+
} else if (path === "~") {
|
|
315
|
+
return homedir();
|
|
316
|
+
}
|
|
317
|
+
return path;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Get file/directory suggestions for a given path prefix
|
|
321
|
+
private getFileSuggestions(prefix: string): AutocompleteItem[] {
|
|
322
|
+
try {
|
|
323
|
+
let searchDir: string;
|
|
324
|
+
let searchPrefix: string;
|
|
325
|
+
let expandedPrefix = prefix;
|
|
326
|
+
let isAtPrefix = false;
|
|
327
|
+
|
|
328
|
+
// Handle @ file attachment prefix
|
|
329
|
+
if (prefix.startsWith("@")) {
|
|
330
|
+
isAtPrefix = true;
|
|
331
|
+
expandedPrefix = prefix.slice(1); // Remove the @
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Handle home directory expansion
|
|
335
|
+
if (expandedPrefix.startsWith("~")) {
|
|
336
|
+
expandedPrefix = this.expandHomePath(expandedPrefix);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (
|
|
340
|
+
expandedPrefix === "" ||
|
|
341
|
+
expandedPrefix === "./" ||
|
|
342
|
+
expandedPrefix === "../" ||
|
|
343
|
+
expandedPrefix === "~" ||
|
|
344
|
+
expandedPrefix === "~/" ||
|
|
345
|
+
expandedPrefix === "/" ||
|
|
346
|
+
prefix === "@"
|
|
347
|
+
) {
|
|
348
|
+
// Complete from specified position
|
|
349
|
+
if (prefix.startsWith("~") || expandedPrefix === "/") {
|
|
350
|
+
searchDir = expandedPrefix;
|
|
351
|
+
} else {
|
|
352
|
+
searchDir = join(this.basePath, expandedPrefix);
|
|
353
|
+
}
|
|
354
|
+
searchPrefix = "";
|
|
355
|
+
} else if (expandedPrefix.endsWith("/")) {
|
|
356
|
+
// If prefix ends with /, show contents of that directory
|
|
357
|
+
if (prefix.startsWith("~") || expandedPrefix.startsWith("/")) {
|
|
358
|
+
searchDir = expandedPrefix;
|
|
359
|
+
} else {
|
|
360
|
+
searchDir = join(this.basePath, expandedPrefix);
|
|
361
|
+
}
|
|
362
|
+
searchPrefix = "";
|
|
363
|
+
} else {
|
|
364
|
+
// Split into directory and file prefix
|
|
365
|
+
const dir = dirname(expandedPrefix);
|
|
366
|
+
const file = basename(expandedPrefix);
|
|
367
|
+
if (prefix.startsWith("~") || expandedPrefix.startsWith("/")) {
|
|
368
|
+
searchDir = dir;
|
|
369
|
+
} else {
|
|
370
|
+
searchDir = join(this.basePath, dir);
|
|
371
|
+
}
|
|
372
|
+
searchPrefix = file;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const entries = readdirSync(searchDir, { withFileTypes: true });
|
|
376
|
+
const suggestions: AutocompleteItem[] = [];
|
|
377
|
+
|
|
378
|
+
for (const entry of entries) {
|
|
379
|
+
if (!entry.name.toLowerCase().startsWith(searchPrefix.toLowerCase())) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Check if entry is a directory (or a symlink pointing to a directory)
|
|
384
|
+
let isDirectory = entry.isDirectory();
|
|
385
|
+
if (!isDirectory && entry.isSymbolicLink()) {
|
|
386
|
+
try {
|
|
387
|
+
const fullPath = join(searchDir, entry.name);
|
|
388
|
+
isDirectory = statSync(fullPath).isDirectory();
|
|
389
|
+
} catch {
|
|
390
|
+
// Broken symlink or permission error - treat as file
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
let relativePath: string;
|
|
395
|
+
const name = entry.name;
|
|
396
|
+
|
|
397
|
+
// Handle @ prefix path construction
|
|
398
|
+
if (isAtPrefix) {
|
|
399
|
+
const pathWithoutAt = expandedPrefix;
|
|
400
|
+
if (pathWithoutAt.endsWith("/")) {
|
|
401
|
+
relativePath = `@${pathWithoutAt}${name}`;
|
|
402
|
+
} else if (pathWithoutAt.includes("/")) {
|
|
403
|
+
if (pathWithoutAt.startsWith("~/")) {
|
|
404
|
+
const homeRelativeDir = pathWithoutAt.slice(2); // Remove ~/
|
|
405
|
+
const dir = dirname(homeRelativeDir);
|
|
406
|
+
relativePath = `@~/${dir === "." ? name : join(dir, name)}`;
|
|
407
|
+
} else {
|
|
408
|
+
relativePath = `@${join(dirname(pathWithoutAt), name)}`;
|
|
409
|
+
}
|
|
410
|
+
} else {
|
|
411
|
+
if (pathWithoutAt.startsWith("~")) {
|
|
412
|
+
relativePath = `@~/${name}`;
|
|
413
|
+
} else {
|
|
414
|
+
relativePath = `@${name}`;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
} else if (prefix.endsWith("/")) {
|
|
418
|
+
// If prefix ends with /, append entry to the prefix
|
|
419
|
+
relativePath = prefix + name;
|
|
420
|
+
} else if (prefix.includes("/")) {
|
|
421
|
+
// Preserve ~/ format for home directory paths
|
|
422
|
+
if (prefix.startsWith("~/")) {
|
|
423
|
+
const homeRelativeDir = prefix.slice(2); // Remove ~/
|
|
424
|
+
const dir = dirname(homeRelativeDir);
|
|
425
|
+
relativePath = `~/${dir === "." ? name : join(dir, name)}`;
|
|
426
|
+
} else if (prefix.startsWith("/")) {
|
|
427
|
+
// Absolute path - construct properly
|
|
428
|
+
const dir = dirname(prefix);
|
|
429
|
+
if (dir === "/") {
|
|
430
|
+
relativePath = `/${name}`;
|
|
431
|
+
} else {
|
|
432
|
+
relativePath = `${dir}/${name}`;
|
|
433
|
+
}
|
|
434
|
+
} else {
|
|
435
|
+
relativePath = join(dirname(prefix), name);
|
|
436
|
+
}
|
|
437
|
+
} else {
|
|
438
|
+
// For standalone entries, preserve ~/ if original prefix was ~/
|
|
439
|
+
if (prefix.startsWith("~")) {
|
|
440
|
+
relativePath = `~/${name}`;
|
|
441
|
+
} else {
|
|
442
|
+
relativePath = name;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
suggestions.push({
|
|
447
|
+
value: isDirectory ? `${relativePath}/` : relativePath,
|
|
448
|
+
label: name + (isDirectory ? "/" : ""),
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Sort directories first, then alphabetically
|
|
453
|
+
suggestions.sort((a, b) => {
|
|
454
|
+
const aIsDir = a.value.endsWith("/");
|
|
455
|
+
const bIsDir = b.value.endsWith("/");
|
|
456
|
+
if (aIsDir && !bIsDir) return -1;
|
|
457
|
+
if (!aIsDir && bIsDir) return 1;
|
|
458
|
+
return a.label.localeCompare(b.label);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
return suggestions;
|
|
462
|
+
} catch (_e) {
|
|
463
|
+
// Directory doesn't exist or not accessible
|
|
464
|
+
return [];
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// Score an entry against the query (higher = better match)
|
|
469
|
+
// isDirectory adds bonus to prioritize folders
|
|
470
|
+
private scoreEntry(filePath: string, query: string, isDirectory: boolean): number {
|
|
471
|
+
const fileName = basename(filePath);
|
|
472
|
+
const lowerFileName = fileName.toLowerCase();
|
|
473
|
+
const lowerQuery = query.toLowerCase();
|
|
474
|
+
|
|
475
|
+
let score = 0;
|
|
476
|
+
|
|
477
|
+
// Exact filename match (highest)
|
|
478
|
+
if (lowerFileName === lowerQuery) score = 100;
|
|
479
|
+
// Filename starts with query
|
|
480
|
+
else if (lowerFileName.startsWith(lowerQuery)) score = 80;
|
|
481
|
+
// Substring match in filename
|
|
482
|
+
else if (lowerFileName.includes(lowerQuery)) score = 50;
|
|
483
|
+
// Substring match in full path
|
|
484
|
+
else if (filePath.toLowerCase().includes(lowerQuery)) score = 30;
|
|
485
|
+
|
|
486
|
+
// Directories get a bonus to appear first
|
|
487
|
+
if (isDirectory && score > 0) score += 10;
|
|
488
|
+
|
|
489
|
+
return score;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Fuzzy file search using fd (fast, respects .gitignore)
|
|
493
|
+
private getFuzzyFileSuggestions(query: string): AutocompleteItem[] {
|
|
494
|
+
if (!this.fdPath) {
|
|
495
|
+
// fd not available, return empty results
|
|
496
|
+
return [];
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
try {
|
|
500
|
+
const entries = walkDirectoryWithFd(this.basePath, this.fdPath, query, 100);
|
|
501
|
+
|
|
502
|
+
// Score entries
|
|
503
|
+
const scoredEntries = entries
|
|
504
|
+
.map((entry) => ({
|
|
505
|
+
...entry,
|
|
506
|
+
score: query ? this.scoreEntry(entry.path, query, entry.isDirectory) : 1,
|
|
507
|
+
}))
|
|
508
|
+
.filter((entry) => entry.score > 0);
|
|
509
|
+
|
|
510
|
+
// Sort by score (descending) and take top 20
|
|
511
|
+
scoredEntries.sort((a, b) => b.score - a.score);
|
|
512
|
+
const topEntries = scoredEntries.slice(0, 20);
|
|
513
|
+
|
|
514
|
+
// Build suggestions
|
|
515
|
+
const suggestions: AutocompleteItem[] = [];
|
|
516
|
+
for (const { path: entryPath, isDirectory } of topEntries) {
|
|
517
|
+
// fd already includes trailing / for directories
|
|
518
|
+
const pathWithoutSlash = isDirectory ? entryPath.slice(0, -1) : entryPath;
|
|
519
|
+
const entryName = basename(pathWithoutSlash);
|
|
520
|
+
|
|
521
|
+
suggestions.push({
|
|
522
|
+
value: `@${entryPath}`,
|
|
523
|
+
label: entryName + (isDirectory ? "/" : ""),
|
|
524
|
+
description: pathWithoutSlash,
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
return suggestions;
|
|
529
|
+
} catch {
|
|
530
|
+
return [];
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Force file completion (called on Tab key) - always returns suggestions
|
|
535
|
+
getForceFileSuggestions(
|
|
536
|
+
lines: string[],
|
|
537
|
+
cursorLine: number,
|
|
538
|
+
cursorCol: number,
|
|
539
|
+
): { items: AutocompleteItem[]; prefix: string } | null {
|
|
540
|
+
const currentLine = lines[cursorLine] || "";
|
|
541
|
+
const textBeforeCursor = currentLine.slice(0, cursorCol);
|
|
542
|
+
|
|
543
|
+
// Don't trigger if we're typing a slash command at the start of the line
|
|
544
|
+
if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) {
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// Force extract path prefix - this will always return something
|
|
549
|
+
const pathMatch = this.extractPathPrefix(textBeforeCursor, true);
|
|
550
|
+
if (pathMatch !== null) {
|
|
551
|
+
const suggestions = this.getFileSuggestions(pathMatch);
|
|
552
|
+
if (suggestions.length === 0) return null;
|
|
553
|
+
|
|
554
|
+
return {
|
|
555
|
+
items: suggestions,
|
|
556
|
+
prefix: pathMatch,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return null;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// Check if we should trigger file completion (called on Tab key)
|
|
564
|
+
shouldTriggerFileCompletion(lines: string[], cursorLine: number, cursorCol: number): boolean {
|
|
565
|
+
const currentLine = lines[cursorLine] || "";
|
|
566
|
+
const textBeforeCursor = currentLine.slice(0, cursorCol);
|
|
567
|
+
|
|
568
|
+
// Don't trigger if we're typing a slash command at the start of the line
|
|
569
|
+
if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) {
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { Component } from "../tui.js";
|
|
2
|
+
import { applyBackgroundToLine, visibleWidth } from "../utils.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Box component - a container that applies padding and background to all children
|
|
6
|
+
*/
|
|
7
|
+
export class Box implements Component {
|
|
8
|
+
children: Component[] = [];
|
|
9
|
+
private paddingX: number;
|
|
10
|
+
private paddingY: number;
|
|
11
|
+
private bgFn?: (text: string) => string;
|
|
12
|
+
|
|
13
|
+
// Cache for rendered output
|
|
14
|
+
private cachedWidth?: number;
|
|
15
|
+
private cachedChildLines?: string;
|
|
16
|
+
private cachedBgSample?: string;
|
|
17
|
+
private cachedLines?: string[];
|
|
18
|
+
|
|
19
|
+
constructor(paddingX = 1, paddingY = 1, bgFn?: (text: string) => string) {
|
|
20
|
+
this.paddingX = paddingX;
|
|
21
|
+
this.paddingY = paddingY;
|
|
22
|
+
this.bgFn = bgFn;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
addChild(component: Component): void {
|
|
26
|
+
this.children.push(component);
|
|
27
|
+
this.invalidateCache();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
removeChild(component: Component): void {
|
|
31
|
+
const index = this.children.indexOf(component);
|
|
32
|
+
if (index !== -1) {
|
|
33
|
+
this.children.splice(index, 1);
|
|
34
|
+
this.invalidateCache();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
clear(): void {
|
|
39
|
+
this.children = [];
|
|
40
|
+
this.invalidateCache();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
setBgFn(bgFn?: (text: string) => string): void {
|
|
44
|
+
this.bgFn = bgFn;
|
|
45
|
+
// Don't invalidate here - we'll detect bgFn changes by sampling output
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private invalidateCache(): void {
|
|
49
|
+
this.cachedWidth = undefined;
|
|
50
|
+
this.cachedChildLines = undefined;
|
|
51
|
+
this.cachedBgSample = undefined;
|
|
52
|
+
this.cachedLines = undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
invalidate(): void {
|
|
56
|
+
this.invalidateCache();
|
|
57
|
+
for (const child of this.children) {
|
|
58
|
+
child.invalidate?.();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
render(width: number): string[] {
|
|
63
|
+
if (this.children.length === 0) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const contentWidth = Math.max(1, width - this.paddingX * 2);
|
|
68
|
+
const leftPad = " ".repeat(this.paddingX);
|
|
69
|
+
|
|
70
|
+
// Render all children
|
|
71
|
+
const childLines: string[] = [];
|
|
72
|
+
for (const child of this.children) {
|
|
73
|
+
const lines = child.render(contentWidth);
|
|
74
|
+
for (const line of lines) {
|
|
75
|
+
childLines.push(leftPad + line);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (childLines.length === 0) {
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Check if bgFn output changed by sampling
|
|
84
|
+
const bgSample = this.bgFn ? this.bgFn("test") : undefined;
|
|
85
|
+
|
|
86
|
+
// Check cache validity
|
|
87
|
+
const childLinesKey = childLines.join("\n");
|
|
88
|
+
if (
|
|
89
|
+
this.cachedLines &&
|
|
90
|
+
this.cachedWidth === width &&
|
|
91
|
+
this.cachedChildLines === childLinesKey &&
|
|
92
|
+
this.cachedBgSample === bgSample
|
|
93
|
+
) {
|
|
94
|
+
return this.cachedLines;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Apply background and padding
|
|
98
|
+
const result: string[] = [];
|
|
99
|
+
|
|
100
|
+
// Top padding
|
|
101
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
102
|
+
result.push(this.applyBg("", width));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Content
|
|
106
|
+
for (const line of childLines) {
|
|
107
|
+
result.push(this.applyBg(line, width));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Bottom padding
|
|
111
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
112
|
+
result.push(this.applyBg("", width));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Update cache
|
|
116
|
+
this.cachedWidth = width;
|
|
117
|
+
this.cachedChildLines = childLinesKey;
|
|
118
|
+
this.cachedBgSample = bgSample;
|
|
119
|
+
this.cachedLines = result;
|
|
120
|
+
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private applyBg(line: string, width: number): string {
|
|
125
|
+
const visLen = visibleWidth(line);
|
|
126
|
+
const padNeeded = Math.max(0, width - visLen);
|
|
127
|
+
const padded = line + " ".repeat(padNeeded);
|
|
128
|
+
|
|
129
|
+
if (this.bgFn) {
|
|
130
|
+
return applyBackgroundToLine(padded, width, this.bgFn);
|
|
131
|
+
}
|
|
132
|
+
return padded;
|
|
133
|
+
}
|
|
134
|
+
}
|