返回 DeepSeek-Reasonix
workspacePanelFormat.ts
根目录 / desktop / frontend / src / lib / workspacePanelFormat.ts
1 import type { DirEntry } from "./types";
2
3 export function workspaceEntryPath(dir: string, entry: DirEntry): string {
4 const prefix = dir === "" || dir.endsWith("/") ? dir : `${dir}/`;
5 return prefix + entry.name + (entry.isDir ? "/" : "");
6 }
7
8 export function workspaceBasename(path: string): string {
9 const parts = path.split("/").filter(Boolean);
10 return parts[parts.length - 1] ?? "";
11 }
12
13 export function workspaceParentPath(path: string): string {
14 return path.replace(/\/$/, "").split("/").filter(Boolean).slice(0, -1).join("/");
15 }
16
17 export function workspaceParentDirs(path: string): string[] {
18 const parts = path.split("/").filter(Boolean);
19 const dirs = [""];
20 let current = "";
21 for (let index = 0; index < parts.length - 1; index++) {
22 current += `${parts[index]}/`;
23 dirs.push(current);
24 }
25 return dirs;
26 }
27
28 export function workspaceTopLevelDirPath(path: string): string {
29 const first = path.split("/").find(Boolean);
30 return first ? `${first}/` : "";
31 }
32
33 export function workspaceShortCwd(cwd?: string): string {
34 if (!cwd) return "";
35 const parts = cwd.split("/").filter(Boolean);
36 return parts.length <= 2 ? cwd : `…/${parts.slice(-2).join("/")}`;
37 }
38
39 export function workspaceFormatBytes(bytes: number): string {
40 if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
41 if (bytes >= 1024) return `${Math.ceil(bytes / 1024)} KB`;
42 return `${bytes} B`;
43 }
44
45 export function workspaceFormatCommitDate(value: string): string {
46 const date = new Date(value);
47 if (Number.isNaN(date.getTime())) return value;
48 const month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][date.getMonth()];
49 return `${String(date.getDate()).padStart(2, "0")} ${month} ${date.getFullYear()} ${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;
50 }
51 export function isAbsoluteDisplayPath(path: string): boolean {
52 return path.startsWith("/") || path.startsWith("\\\\") || /^[A-Za-z]:[\\/]/.test(path);
53 }
54
55 export function formatWorkspaceSource(path: string, body: string): string {
56 if (!/\.json$/i.test(path)) return body;
57 try { return JSON.stringify(JSON.parse(body), null, 2); } catch { return body; }
58 }
59
59 lines TYPESCRIPT