Open History to see the last commit
1import type { ProjectEntry } from "@t3tools/contracts";
2import { isWindowsAbsolutePath, isAbsolutePath } from "@t3tools/shared/path";
3
4export interface FileBreadcrumb {
5 label: string;
6 path: string;
7 kind: "project" | "directory" | "file";
8}
9
10export interface FileBreadcrumbChild extends ProjectEntry {
11 label: string;
12}
13
14/**
15 * Crumbs for a workspace-relative path start at the project. An absolute host
16 * path is outside the workspace, so its crumbs start at the filesystem root.
17 */
18export function fileBreadcrumbs(projectName: string, relativePath: string): FileBreadcrumb[] {
19 const hostPath = isAbsolutePath(relativePath);
20 const separator = isWindowsAbsolutePath(relativePath) ? "\\" : "/";
21 const parts = relativePath.split(/[\\/]/).filter(Boolean);
22 const root = relativePath.startsWith("\\\\") ? "\\\\" : hostPath && separator === "/" ? "/" : "";
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 path: /^[A-Za-z]:$/.test(path) ? `${path}\\` : path,
31 kind: index === parts.length - 1 ? ("file" as const) : ("directory" as const),
32 };
33 }),
34 ];
35}
36
37/**
38 * `relativePath` written the way its own crumb names it, so a link with a
39 * trailing or repeated separator still finds its crumb.
40 */
41export function breadcrumbPathOf(relativePath: string): string {
42 return fileBreadcrumbs("", relativePath).at(-1)?.path ?? relativePath;
43}
44
45/**
46 * Whether a host folder crumb names a directory the server can list. A UNC
47 * server (`\\server`) is not one; its shares (`\\server\share`) are.
48 */
49export function isListableHostFolder(path: string): boolean {
50 return isAbsolutePath(path) && !/^\\\\[^\\/]+$/.test(path);
51}
52
53/** Index of `path`'s crumb among `trail`'s crumbs; -1 when the trail does not pass through it. */
54function trailCrumbIndex(trail: string, path: string): number {
55 const crumbPath = breadcrumbPathOf(path);
56 return fileBreadcrumbs("", trail).findIndex((crumb) => crumb.path === crumbPath);
57}
58
59/**
60 * The deepest path the breadcrumbs keep showing after the browser moves to `path`.
61 * Going up keeps the folders below visible so the way back down is one click;
62 * moving anywhere else starts over at `path`.
63 */
64export function retainedBreadcrumbTrail(trail: string | undefined, path: string): string {
65 return trail !== undefined && trailCrumbIndex(trail, path) >= 0 ? trail : path;
66}
67
68/** The entry of `folder` that leads toward `trail`; null when the trail ends at the folder. */
69export function trailChildOf(folder: string, trail: string): string | null {
70 const index = trailCrumbIndex(trail, folder);
71 return index < 0 ? null : (fileBreadcrumbs("", trail)[index + 1]?.label ?? null);
72}
73
74/**
75 * A host path for `relativePath` (as a host folder's listing names it, with `/`)
76 * inside host folder `folder`, using the folder's own separator.
77 */
78export function joinHostPath(folder: string, relativePath: string): string {
79 const separator = isWindowsAbsolutePath(folder) ? "\\" : "/";
80 const base = folder.endsWith(separator) ? folder : `${folder}${separator}`;
81 return base + relativePath.split("/").join(separator);
82}
83
84export function fileBreadcrumbChildren(
85 entries: readonly ProjectEntry[],
86 directoryPath: string,
87): FileBreadcrumbChild[] {
88 let collator: Intl.Collator | undefined;
89 const prefix = directoryPath ? `${directoryPath}/` : "";
90 return entries
91 .flatMap((entry) => {
92 if (!entry.path.startsWith(prefix)) return [];
93 const label = entry.path.slice(prefix.length);
94 if (!label || label.includes("/")) return [];
95 return [{ ...entry, label }];
96 })
97 .toSorted((left, right) => {
98 if (left.kind !== right.kind) return left.kind === "directory" ? -1 : 1;
99 collator ??= new Intl.Collator(undefined, {
100 numeric: true,
101 sensitivity: "base",
102 });
103 return collator.compare(left.label, right.label);
104 });
105}
106
107export function fileBreadcrumbParent(directoryPath: string): string | null {
108 if (!directoryPath) return null;
109 const separatorIndex = directoryPath.lastIndexOf("/");
110 return separatorIndex === -1 ? "" : directoryPath.slice(0, separatorIndex);
111}