@rosetears/aili-pi 0.1.0 → 0.1.3
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 +15 -6
- package/THIRD_PARTY_NOTICES.md +14 -3
- package/extensions/header/index.ts +92 -0
- package/extensions/matrix/index.ts +375 -0
- package/extensions/zentui/config.ts +1014 -0
- package/extensions/zentui/extension-status.ts +96 -0
- package/extensions/zentui/fixed-editor/cluster.ts +98 -0
- package/extensions/zentui/fixed-editor/compositor.ts +719 -0
- package/extensions/zentui/fixed-editor/index.ts +223 -0
- package/extensions/zentui/fixed-editor/input.ts +85 -0
- package/extensions/zentui/fixed-editor/pi-compat.ts +296 -0
- package/extensions/zentui/fixed-editor/selection.ts +217 -0
- package/extensions/zentui/fixed-editor/terminal-modes.ts +75 -0
- package/extensions/zentui/fixed-editor/types.ts +37 -0
- package/extensions/zentui/footer-format.ts +279 -0
- package/extensions/zentui/footer.ts +595 -0
- package/extensions/zentui/format.ts +434 -0
- package/extensions/zentui/git.ts +384 -0
- package/extensions/zentui/gradient.ts +70 -0
- package/extensions/zentui/icons.ts +252 -0
- package/extensions/zentui/index.ts +580 -0
- package/extensions/zentui/live-context.ts +75 -0
- package/extensions/zentui/package-version.ts +650 -0
- package/extensions/zentui/project-refresh.ts +104 -0
- package/extensions/zentui/project-state.ts +59 -0
- package/extensions/zentui/prototype-patch-registry.ts +111 -0
- package/extensions/zentui/runtime.ts +841 -0
- package/extensions/zentui/selector-border.ts +77 -0
- package/extensions/zentui/session-lifecycle.ts +60 -0
- package/extensions/zentui/settings-command.ts +897 -0
- package/extensions/zentui/state.ts +55 -0
- package/extensions/zentui/style.ts +332 -0
- package/extensions/zentui/thinking-message.ts +159 -0
- package/extensions/zentui/tool-execution.ts +189 -0
- package/extensions/zentui/ui.ts +618 -0
- package/extensions/zentui/user-message.ts +252 -0
- package/licenses/pi-sakura-cyberdeck-MIT.txt +21 -0
- package/licenses/pi-zentui-MIT.txt +21 -0
- package/manifests/capabilities.json +1 -1
- package/manifests/live-verification.json +14 -8
- package/manifests/provenance.json +18 -5
- package/manifests/sbom.json +19 -3
- package/notices/pi-sakura-cyberdeck-NOTICE.txt +6 -0
- package/package.json +12 -3
- package/scripts/local-package-e2e.ts +1 -1
- package/scripts/sync-global-skills.d.mts +13 -0
- package/scripts/sync-global-skills.mjs +134 -0
- package/src/runtime/credential-guard.ts +58 -0
- package/src/runtime/doctor.ts +1 -1
- package/src/runtime/index.ts +2 -2
- package/src/runtime/path-boundaries.ts +1 -1
- package/src/runtime/registry.ts +6 -2
- package/src/runtime/rem-head.txt +38 -0
- package/src/runtime/rose-context.ts +1 -1
- package/src/runtime/subagents.ts +74 -403
- package/templates/APPEND_SYSTEM.md +10 -8
- package/themes/rem-cyberdeck.json +32 -0
- package/upstream/opencode-global-agents.lock.json +34 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import { homedir, hostname, userInfo } from "node:os";
|
|
2
|
+
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
5
|
+
import type {
|
|
6
|
+
ColorSource,
|
|
7
|
+
ColorSpec,
|
|
8
|
+
ContextStyle,
|
|
9
|
+
ContextThresholds,
|
|
10
|
+
GitBranchMaxLength,
|
|
11
|
+
PathDisplayMode,
|
|
12
|
+
} from "./config.js";
|
|
13
|
+
import type { GitCommitInfo, GitMetricsInfo } from "./git.js";
|
|
14
|
+
import type { IconMode } from "./icons.js";
|
|
15
|
+
import { resolveOsIcon, resolvePackageIcon, resolveRuntimeSymbol } from "./icons.js";
|
|
16
|
+
import type { PackageVersionResult } from "./package-version.js";
|
|
17
|
+
import type { RuntimeInfo } from "./runtime.js";
|
|
18
|
+
import { renderStyleForSource } from "./style.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Starship `git_commit` style — render a short hash, optionally with an
|
|
22
|
+
* exact-match tag. See https://starship.rs/config/#git-commit
|
|
23
|
+
*
|
|
24
|
+
* Visibility is decided by the caller; this helper only formats the data.
|
|
25
|
+
* `hashLength` is clamped to [4, 40] upstream.
|
|
26
|
+
*/
|
|
27
|
+
export function formatGitCommitSegment(
|
|
28
|
+
theme: Pick<Theme, "fg">,
|
|
29
|
+
commit: GitCommitInfo | undefined,
|
|
30
|
+
config: { hashLength: number; onlyDetached: boolean; showTag: boolean },
|
|
31
|
+
colorSource: ColorSource,
|
|
32
|
+
style: ColorSpec,
|
|
33
|
+
): string {
|
|
34
|
+
if (!commit?.oid) return "";
|
|
35
|
+
// Starship's only_detached hides the whole module when attached.
|
|
36
|
+
if (config.onlyDetached && !commit.detached) return "";
|
|
37
|
+
const hash = commit.oid.slice(0, config.hashLength);
|
|
38
|
+
const tag = config.showTag && commit.tag ? commit.tag : "";
|
|
39
|
+
if (!hash && !tag) return "";
|
|
40
|
+
const label = [hash, tag].filter(Boolean).join(" ");
|
|
41
|
+
return renderStyleForSource(theme, colorSource, style, label);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Starship `git_metrics` style — render `+added −deleted` line counts.
|
|
46
|
+
* See https://starship.rs/config/#git-metrics
|
|
47
|
+
*
|
|
48
|
+
* When `onlyNonzero` is true, each zero component is omitted independently
|
|
49
|
+
* and the whole segment hides at 0/0.
|
|
50
|
+
*/
|
|
51
|
+
export function formatGitMetricsSegment(
|
|
52
|
+
theme: Pick<Theme, "fg">,
|
|
53
|
+
metrics: GitMetricsInfo | null | undefined,
|
|
54
|
+
config: { onlyNonzero: boolean },
|
|
55
|
+
colorSource: ColorSource,
|
|
56
|
+
addedStyle: ColorSpec,
|
|
57
|
+
deletedStyle: ColorSpec,
|
|
58
|
+
): string {
|
|
59
|
+
if (!metrics) return "";
|
|
60
|
+
const showAdded = !config.onlyNonzero || metrics.added > 0;
|
|
61
|
+
const showDeleted = !config.onlyNonzero || metrics.deleted > 0;
|
|
62
|
+
if (!showAdded && !showDeleted) return "";
|
|
63
|
+
const parts: string[] = [];
|
|
64
|
+
if (showAdded) {
|
|
65
|
+
parts.push(renderStyleForSource(theme, colorSource, addedStyle, `+${metrics.added}`));
|
|
66
|
+
}
|
|
67
|
+
if (showDeleted) {
|
|
68
|
+
parts.push(renderStyleForSource(theme, colorSource, deletedStyle, `−${metrics.deleted}`));
|
|
69
|
+
}
|
|
70
|
+
return parts.join(" ");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type UsageTotals = {
|
|
74
|
+
input: number;
|
|
75
|
+
output: number;
|
|
76
|
+
cacheRead: number;
|
|
77
|
+
cacheWrite: number;
|
|
78
|
+
latestCacheHitRate?: number;
|
|
79
|
+
cost: number;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type ContextColorTier = "normal" | "warning" | "error";
|
|
83
|
+
|
|
84
|
+
type SessionEntry = {
|
|
85
|
+
type?: string;
|
|
86
|
+
id?: string | number;
|
|
87
|
+
timestamp?: string | number;
|
|
88
|
+
message?: {
|
|
89
|
+
role?: string;
|
|
90
|
+
usage?: AssistantMessage["usage"];
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
type UsageCacheEntry = {
|
|
95
|
+
key: string;
|
|
96
|
+
totals: UsageTotals;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
let usageTotalsCache: UsageCacheEntry | undefined;
|
|
100
|
+
let usageTotalsComputeCount = 0;
|
|
101
|
+
|
|
102
|
+
export function formatCount(value: number): string {
|
|
103
|
+
if (value < 1000) return value.toString();
|
|
104
|
+
if (value < 10_000) return `${(value / 1000).toFixed(1)}k`;
|
|
105
|
+
if (value < 1_000_000) return `${Math.round(value / 1000)}k`;
|
|
106
|
+
if (value < 10_000_000) return `${(value / 1_000_000).toFixed(1)}M`;
|
|
107
|
+
return `${Math.round(value / 1_000_000)}M`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function formatProviderLabel(provider: string | undefined): string {
|
|
111
|
+
if (!provider) return "Unknown";
|
|
112
|
+
|
|
113
|
+
const known: Record<string, string> = {
|
|
114
|
+
anthropic: "Anthropic",
|
|
115
|
+
gemini: "Google",
|
|
116
|
+
google: "Google",
|
|
117
|
+
ollama: "Ollama",
|
|
118
|
+
openai: "OpenAI",
|
|
119
|
+
"openai-codex": "OpenAI",
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
known[provider] ?? provider.replace(/[-_]/g, " ").replace(/\b\w/g, (char) => char.toUpperCase())
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function calculateCacheHitRate(
|
|
128
|
+
input: number,
|
|
129
|
+
cacheRead: number,
|
|
130
|
+
cacheWrite: number,
|
|
131
|
+
): number | undefined {
|
|
132
|
+
const promptTokens = input + cacheRead + cacheWrite;
|
|
133
|
+
return promptTokens > 0 ? (cacheRead / promptTokens) * 100 : undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function entryIdentity(entry: SessionEntry | undefined): string {
|
|
137
|
+
if (!entry) return "";
|
|
138
|
+
const usage = entry.message?.usage;
|
|
139
|
+
const usageKey = usage
|
|
140
|
+
? `${usage.input ?? 0}:${usage.output ?? 0}:${usage.cacheRead ?? 0}:${usage.cacheWrite ?? 0}:${usage.cost?.total ?? 0}`
|
|
141
|
+
: "";
|
|
142
|
+
return `${entry.id ?? ""}|${entry.timestamp ?? ""}|${entry.type ?? ""}|${entry.message?.role ?? ""}|${usageKey}`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function buildUsageFingerprint(entries: readonly SessionEntry[]): string {
|
|
146
|
+
const first = entries[0];
|
|
147
|
+
const last = entries[entries.length - 1];
|
|
148
|
+
return `${entries.length}\0${entryIdentity(first)}\0${entryIdentity(last)}`;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function computeUsageTotals(entries: readonly SessionEntry[]): UsageTotals {
|
|
152
|
+
usageTotalsComputeCount += 1;
|
|
153
|
+
let input = 0;
|
|
154
|
+
let output = 0;
|
|
155
|
+
let cacheRead = 0;
|
|
156
|
+
let cacheWrite = 0;
|
|
157
|
+
let latestCacheHitRate: number | undefined;
|
|
158
|
+
let cost = 0;
|
|
159
|
+
|
|
160
|
+
for (const entry of entries) {
|
|
161
|
+
if (entry.type !== "message" || entry.message?.role !== "assistant") continue;
|
|
162
|
+
const usage = entry.message.usage;
|
|
163
|
+
const entryInput = usage?.input ?? 0;
|
|
164
|
+
const entryCacheRead = usage?.cacheRead ?? 0;
|
|
165
|
+
const entryCacheWrite = usage?.cacheWrite ?? 0;
|
|
166
|
+
|
|
167
|
+
input += entryInput;
|
|
168
|
+
output += usage?.output ?? 0;
|
|
169
|
+
cacheRead += entryCacheRead;
|
|
170
|
+
cacheWrite += entryCacheWrite;
|
|
171
|
+
cost += usage?.cost?.total ?? 0;
|
|
172
|
+
latestCacheHitRate = calculateCacheHitRate(entryInput, entryCacheRead, entryCacheWrite);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return Object.freeze({ input, output, cacheRead, cacheWrite, latestCacheHitRate, cost });
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function invalidateUsageTotalsCache(): void {
|
|
179
|
+
usageTotalsCache = undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Test helper: number of full usage scans performed since process start / last reset. */
|
|
183
|
+
export function __usageTotalsComputeCount(): number {
|
|
184
|
+
return usageTotalsComputeCount;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Test helper: reset memoization counters/cache. */
|
|
188
|
+
export function __resetUsageTotalsCacheForTests(): void {
|
|
189
|
+
usageTotalsCache = undefined;
|
|
190
|
+
usageTotalsComputeCount = 0;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function getUsageTotals(ctx: ExtensionContext): UsageTotals {
|
|
194
|
+
const sessionManager = ctx.sessionManager as {
|
|
195
|
+
getEntries?: () => SessionEntry[];
|
|
196
|
+
getBranch: () => SessionEntry[];
|
|
197
|
+
};
|
|
198
|
+
const entries = sessionManager.getEntries?.() ?? sessionManager.getBranch();
|
|
199
|
+
const key = buildUsageFingerprint(entries);
|
|
200
|
+
if (usageTotalsCache?.key === key) return usageTotalsCache.totals;
|
|
201
|
+
|
|
202
|
+
const totals = computeUsageTotals(entries);
|
|
203
|
+
usageTotalsCache = { key, totals };
|
|
204
|
+
return totals;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function buildTokenLabel(totals: UsageTotals, cacheHitIcon = ""): string {
|
|
208
|
+
const parts: string[] = [];
|
|
209
|
+
if (totals.input) parts.push(`↑${formatCount(totals.input)}`);
|
|
210
|
+
if (totals.output) parts.push(`↓${formatCount(totals.output)}`);
|
|
211
|
+
|
|
212
|
+
const hasCacheTokens = totals.cacheRead > 0 || totals.cacheWrite > 0;
|
|
213
|
+
if (hasCacheTokens && totals.latestCacheHitRate !== undefined) {
|
|
214
|
+
const cacheHitRate = `${totals.latestCacheHitRate.toFixed(1)}%`;
|
|
215
|
+
parts.push(cacheHitIcon ? `${cacheHitIcon} ${cacheHitRate}` : cacheHitRate);
|
|
216
|
+
}
|
|
217
|
+
return parts.length > 0 ? parts.join(" ") : "↑0 ↓0";
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function buildCostLabel(totals: UsageTotals): string {
|
|
221
|
+
return `$${totals.cost.toFixed(3)}`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function buildSessionDurationLabel(startEpoch: number): string {
|
|
225
|
+
const totalSeconds = Math.max(0, Math.floor((Date.now() - startEpoch) / 1000));
|
|
226
|
+
const hours = Math.floor(totalSeconds / 3600);
|
|
227
|
+
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
|
228
|
+
const seconds = totalSeconds % 60;
|
|
229
|
+
if (hours > 0) return `${hours}h ${minutes}m`;
|
|
230
|
+
if (minutes > 0) return `${minutes}m ${seconds}s`;
|
|
231
|
+
return `${seconds}s`;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function contextColorTier(
|
|
235
|
+
percent: number | null | undefined,
|
|
236
|
+
thresholds: ContextThresholds = { warning: 70, error: 90 },
|
|
237
|
+
): ContextColorTier {
|
|
238
|
+
if (percent === null || percent === undefined || !Number.isFinite(percent)) return "normal";
|
|
239
|
+
if (percent >= thresholds.error) return "error";
|
|
240
|
+
if (percent >= thresholds.warning) return "warning";
|
|
241
|
+
return "normal";
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function buildContextGauge(percent: number, width = 10, ascii = false): string {
|
|
245
|
+
const clamped = Math.max(0, Math.min(100, percent));
|
|
246
|
+
const filled = Math.round((clamped / 100) * width);
|
|
247
|
+
const on = ascii ? "#" : "█";
|
|
248
|
+
const off = ascii ? "-" : "░";
|
|
249
|
+
return `${on.repeat(filled)}${off.repeat(Math.max(0, width - filled))}`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function formatContextPercentLabel(
|
|
253
|
+
percent: number | null | undefined,
|
|
254
|
+
contextWindow: number | undefined,
|
|
255
|
+
): string {
|
|
256
|
+
if (!contextWindow || contextWindow <= 0) return "--";
|
|
257
|
+
const percentLabel =
|
|
258
|
+
percent === null || percent === undefined
|
|
259
|
+
? "?"
|
|
260
|
+
: `${Math.max(0, Math.min(999, Math.round(percent)))}%`;
|
|
261
|
+
return `${percentLabel}/${formatCount(contextWindow)}`;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function buildContextDisplayLabel(options: {
|
|
265
|
+
percent: number | null | undefined;
|
|
266
|
+
contextWindow: number | undefined;
|
|
267
|
+
style?: ContextStyle;
|
|
268
|
+
asciiGauge?: boolean;
|
|
269
|
+
}): string {
|
|
270
|
+
const { percent, contextWindow, style = "text", asciiGauge = false } = options;
|
|
271
|
+
if (!contextWindow || contextWindow <= 0) return "--";
|
|
272
|
+
|
|
273
|
+
const text = formatContextPercentLabel(percent, contextWindow);
|
|
274
|
+
const numericPercent =
|
|
275
|
+
percent === null || percent === undefined || !Number.isFinite(percent)
|
|
276
|
+
? 0
|
|
277
|
+
: Math.max(0, Math.min(100, percent));
|
|
278
|
+
const gauge = buildContextGauge(numericPercent, 10, asciiGauge);
|
|
279
|
+
|
|
280
|
+
if (style === "gauge") return `[${gauge}]`;
|
|
281
|
+
if (style === "text+gauge") return `[${gauge}] ${text}`;
|
|
282
|
+
return text;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function buildContextLabel(ctx: ExtensionContext): string {
|
|
286
|
+
const usage = ctx.getContextUsage();
|
|
287
|
+
const contextWindow = ctx.model?.contextWindow ?? usage?.contextWindow;
|
|
288
|
+
return formatContextPercentLabel(usage?.percent, contextWindow);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function formatRuntimeSegment(
|
|
292
|
+
theme: Pick<Theme, "fg">,
|
|
293
|
+
runtime: RuntimeInfo | undefined,
|
|
294
|
+
prefixStyle: ColorSpec,
|
|
295
|
+
colorSource: ColorSource,
|
|
296
|
+
mode: IconMode = "auto",
|
|
297
|
+
): string {
|
|
298
|
+
if (!runtime) return "";
|
|
299
|
+
const symbol = resolveRuntimeSymbol(runtime.name, runtime.symbol, mode);
|
|
300
|
+
const label = runtime.version ? `${symbol} ${runtime.version}` : symbol;
|
|
301
|
+
return `${renderStyleForSource(theme, colorSource, prefixStyle, "via")} ${renderStyleForSource(theme, colorSource, runtime.style, label)}`;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Render the package-version segment in Starship `is <glyph> <version>` shape.
|
|
306
|
+
*
|
|
307
|
+
* Distinct from the runtime segment: this surfaces the project's own
|
|
308
|
+
* manifest version (e.g. `package.json#version`), not the installed
|
|
309
|
+
* toolchain version. Glyph comes from the Starship Nerd Font preset
|
|
310
|
+
* (https://starship.rs/presets/nerd-font); default color `208` matches
|
|
311
|
+
* the Starship `package` module default
|
|
312
|
+
* (https://starship.rs/config/#package-version).
|
|
313
|
+
*/
|
|
314
|
+
export function formatPackageVersionSegment(
|
|
315
|
+
theme: Pick<Theme, "fg">,
|
|
316
|
+
pkg: PackageVersionResult | undefined,
|
|
317
|
+
colorSource: ColorSource,
|
|
318
|
+
mode: IconMode = "auto",
|
|
319
|
+
configuredIcon: string = "",
|
|
320
|
+
versionStyle: ColorSpec = "208",
|
|
321
|
+
): string {
|
|
322
|
+
if (!pkg) return "";
|
|
323
|
+
const icon = resolvePackageIcon(configuredIcon, mode);
|
|
324
|
+
const label = `${icon} ${pkg.version}`;
|
|
325
|
+
return `${renderStyleForSource(theme, colorSource, "", "is")} ${renderStyleForSource(theme, colorSource, versionStyle, label)}`;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export type FormatCwdOptions = {
|
|
329
|
+
mode?: PathDisplayMode;
|
|
330
|
+
/** Trailing directory components to keep in full mode. 0 = unlimited. */
|
|
331
|
+
depth?: number;
|
|
332
|
+
home?: string;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
function normalizeDisplayPath(cwd: string): string {
|
|
336
|
+
const withSlashes = cwd.replace(/\\/g, "/");
|
|
337
|
+
if (withSlashes === "/" || /^\/+$/.test(withSlashes)) return "/";
|
|
338
|
+
const stripped = withSlashes.replace(/\/+$/, "");
|
|
339
|
+
return stripped === "" ? withSlashes : stripped;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function toHomePath(path: string, home: string): string {
|
|
343
|
+
if (!home) return path;
|
|
344
|
+
const homeNorm = home.replace(/\\/g, "/").replace(/\/+$/, "");
|
|
345
|
+
if (!homeNorm) return path;
|
|
346
|
+
if (path === homeNorm) return "~";
|
|
347
|
+
if (path.startsWith(`${homeNorm}/`)) return `~${path.slice(homeNorm.length)}`;
|
|
348
|
+
return path;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Starship-style: keep last `depth` components; prefix with `…/` when parents were dropped. */
|
|
352
|
+
function applyPathDepth(path: string, depth: number): string {
|
|
353
|
+
if (!Number.isFinite(depth) || depth <= 0) return path;
|
|
354
|
+
const limit = Math.floor(depth);
|
|
355
|
+
if (path === "~" || path === "/") return path;
|
|
356
|
+
|
|
357
|
+
let components: string[];
|
|
358
|
+
if (path.startsWith("~/")) {
|
|
359
|
+
components = path.slice(2).split("/").filter(Boolean);
|
|
360
|
+
} else if (/^[A-Za-z]:\//.test(path)) {
|
|
361
|
+
components = path.slice(3).split("/").filter(Boolean);
|
|
362
|
+
} else if (path.startsWith("/")) {
|
|
363
|
+
components = path.slice(1).split("/").filter(Boolean);
|
|
364
|
+
} else {
|
|
365
|
+
components = path.split("/").filter(Boolean);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (components.length <= limit) return path;
|
|
369
|
+
return `…/${components.slice(-limit).join("/")}`;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export function formatCwdLabel(cwd: string, cwdIcon: string, options?: FormatCwdOptions): string {
|
|
373
|
+
const mode = options?.mode ?? "basename";
|
|
374
|
+
const normalized = normalizeDisplayPath(cwd);
|
|
375
|
+
let pathText: string;
|
|
376
|
+
if (mode === "full") {
|
|
377
|
+
const home =
|
|
378
|
+
options?.home ??
|
|
379
|
+
(() => {
|
|
380
|
+
try {
|
|
381
|
+
return homedir();
|
|
382
|
+
} catch {
|
|
383
|
+
return "";
|
|
384
|
+
}
|
|
385
|
+
})();
|
|
386
|
+
pathText = applyPathDepth(toHomePath(normalized, home), options?.depth ?? 0);
|
|
387
|
+
} else if (normalized === "/") {
|
|
388
|
+
pathText = "/";
|
|
389
|
+
} else {
|
|
390
|
+
const parts = normalized.split("/").filter(Boolean);
|
|
391
|
+
pathText = parts[parts.length - 1] ?? cwd;
|
|
392
|
+
}
|
|
393
|
+
return cwdIcon ? `${cwdIcon} ${pathText}` : pathText;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function stripAnsi(text: string): string {
|
|
397
|
+
return text.replace(/\u001B\[[0-9;]*m/g, "");
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
export function formatGitBranchText(
|
|
401
|
+
branch: string,
|
|
402
|
+
maxLength: GitBranchMaxLength = "full",
|
|
403
|
+
): string {
|
|
404
|
+
if (maxLength === "full" || visibleWidth(branch) <= maxLength) return branch;
|
|
405
|
+
return stripAnsi(truncateToWidth(branch, maxLength, "…"));
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export function formatUsernameHostLabel(icon: string): string {
|
|
409
|
+
try {
|
|
410
|
+
const user = userInfo().username;
|
|
411
|
+
const host = hostname();
|
|
412
|
+
if (!user || !host) return "";
|
|
413
|
+
const label = `${user}@${host}`;
|
|
414
|
+
return icon ? `${icon} ${label}` : label;
|
|
415
|
+
} catch {
|
|
416
|
+
return "";
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export function formatTimeLabel(icon: string): string {
|
|
421
|
+
const now = new Date();
|
|
422
|
+
const hours = String(now.getHours()).padStart(2, "0");
|
|
423
|
+
const minutes = String(now.getMinutes()).padStart(2, "0");
|
|
424
|
+
const label = `${hours}:${minutes}`;
|
|
425
|
+
return icon ? `${icon} ${label}` : label;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
export function formatOsLabel(
|
|
429
|
+
configuredIcon: string,
|
|
430
|
+
mode: IconMode = "auto",
|
|
431
|
+
platform: string = process.platform,
|
|
432
|
+
): string {
|
|
433
|
+
return resolveOsIcon(configuredIcon, mode, platform);
|
|
434
|
+
}
|