@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 96 lines 3.88 KB
Raw
1 import type { ProjectEntry } from "@t3tools/contracts";
2 import { isWindowsAbsolutePath, isHostPath } from "@t3tools/shared/path";
3
4 export interface FileBreadcrumb {
5 label: string;
6 path: string;
7 kind: "project" | "directory" | "file";
8 }
9
10 export interface FileBreadcrumbChild extends ProjectEntry {
11 label: string;
12 }
13
14 export function fileBreadcrumbs(projectName: string, relativePath: string): FileBreadcrumb[] {
15 const hostPath = isHostPath(relativePath);
16 const windowsPath = isWindowsAbsolutePath(relativePath);
17 const separator = windowsPath ? "\\" : "/";
18 // `\` is a legal filename character outside Windows paths.
19 // The server reads `~\` as home; the rest keeps the usual relative-path spelling.
20 const homePath = relativePath.startsWith("~\\") ? `~/${relativePath.slice(2)}` : relativePath;
21 const parts = homePath.split(windowsPath ? /[\\/]/ : "/").filter(Boolean);
22 const root = relativePath.startsWith("\\\\") ? "\\\\" : relativePath.startsWith("/") ? "/" : "";
23 return [
24 ...(hostPath ? [] : [{ label: projectName, path: "", kind: "project" as const }]),
25 ...parts.map((part, index) => {
26 const path = root + parts.slice(0, index + 1).join(separator);
27 return {
28 label: part,
29 // A bare drive (`C:`) means the drive's current directory; its root is `C:\`.
30 // A bare `~` is not a host path; the home folder is `~/`.
31 path: /^[A-Za-z]:$/.test(path) ? `${path}\\` : hostPath && path === "~" ? "~/" : path,
32 kind: index === parts.length - 1 ? ("file" as const) : ("directory" as const),
33 };
34 }),
35 ];
36 }
37
38 /** Use the crumb's path spelling so trailing or repeated separators still match. */
39 export function breadcrumbPathOf(relativePath: string): string {
40 return fileBreadcrumbs("", relativePath).at(-1)?.path ?? relativePath;
41 }
42
43 /** UNC servers are not listable directories; their shares are. */
44 export function isListableHostFolder(path: string): boolean {
45 return isHostPath(path) && !/^\\\\[^\\/]+$/.test(path);
46 }
47
48 function trailCrumbIndex(trail: string, path: string): number {
49 const crumbPath = breadcrumbPathOf(path);
50 return fileBreadcrumbs("", trail).findIndex((crumb) => crumb.path === crumbPath);
51 }
52
53 /** Retain descendant crumbs when navigating up; other navigation resets the trail to path. */
54 export function retainedBreadcrumbTrail(trail: string | undefined, path: string): string {
55 return trail !== undefined && trailCrumbIndex(trail, path) >= 0 ? trail : path;
56 }
57
58 export function trailChildOf(folder: string, trail: string): string | null {
59 const index = trailCrumbIndex(trail, folder);
60 return index < 0 ? null : (fileBreadcrumbs("", trail)[index + 1]?.label ?? null);
61 }
62
63 export function joinHostPath(folder: string, relativePath: string): string {
64 const separator = isWindowsAbsolutePath(folder) ? "\\" : "/";
65 const base = folder.endsWith(separator) ? folder : `${folder}${separator}`;
66 return base + relativePath.split("/").join(separator);
67 }
68
69 export function fileBreadcrumbChildren(
70 entries: readonly ProjectEntry[],
71 directoryPath: string,
72 ): FileBreadcrumbChild[] {
73 let collator: Intl.Collator | undefined;
74 const prefix = directoryPath ? `${directoryPath}/` : "";
75 return entries
76 .flatMap((entry) => {
77 if (!entry.path.startsWith(prefix)) return [];
78 const label = entry.path.slice(prefix.length);
79 if (!label || label.includes("/")) return [];
80 return [{ ...entry, label }];
81 })
82 .toSorted((left, right) => {
83 if (left.kind !== right.kind) return left.kind === "directory" ? -1 : 1;
84 collator ??= new Intl.Collator(undefined, {
85 numeric: true,
86 sensitivity: "base",
87 });
88 return collator.compare(left.label, right.label);
89 });
90 }
91
92 export function fileBreadcrumbParent(directoryPath: string): string | null {
93 if (!directoryPath) return null;
94 const separatorIndex = directoryPath.lastIndexOf("/");
95 return separatorIndex === -1 ? "" : directoryPath.slice(0, separatorIndex);
96 }