@aeondave/pi-theme-1337 0.5.1 → 0.6.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/CHANGELOG.md +10 -0
- package/README.md +11 -4
- package/docs/architecture.md +12 -7
- package/docs/configuration.md +16 -0
- package/docs/theme-motion.md +3 -3
- package/package.json +6 -6
- package/src/config.ts +400 -357
- package/src/draft.ts +10 -0
- package/src/editor.ts +545 -443
- package/src/format.ts +464 -464
- package/src/git-status-cache.ts +198 -0
- package/src/git-status-reader.ts +28 -0
- package/src/git-status.ts +72 -0
- package/src/header-controller.ts +58 -0
- package/src/header.ts +151 -0
- package/src/index.ts +1264 -1195
- package/src/status-style.ts +26 -0
- package/src/tool-adapter.ts +491 -491
- package/src/tool-groups.ts +192 -192
- package/src/tool-lexicon.ts +200 -200
- package/src/tool-view.ts +788 -788
- package/src/width.ts +1 -1
- package/themes/1337-amber.json +94 -94
- package/themes/1337-fallout.json +94 -94
- package/themes/1337-ice.json +94 -94
- package/themes/1337-skynet-dark.pi1337 +1 -1
- package/themes/1337-skynet.json +61 -61
- package/themes/1337-skynet.pi1337 +1 -1
- package/themes/1337.json +85 -85
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { type GitStatus } from "./git-status.ts";
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_GIT_STATUS_TTL_MS = 1_000;
|
|
4
|
+
|
|
5
|
+
export type GitStatusReader = (cwd: string) => Promise<GitStatus | undefined>;
|
|
6
|
+
export type GitStatusNotifier = (cwd: string, status: GitStatus | undefined) => void;
|
|
7
|
+
|
|
8
|
+
export interface GitStatusCacheOptions {
|
|
9
|
+
readonly read: GitStatusReader;
|
|
10
|
+
readonly notify?: GitStatusNotifier;
|
|
11
|
+
readonly now?: () => number;
|
|
12
|
+
readonly ttlMs?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface GitStatusRequestOptions {
|
|
16
|
+
/** Skip the TTL; if a read is active, queue exactly one coalesced follow-up. */
|
|
17
|
+
readonly force?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface GitStatusCache {
|
|
21
|
+
/** Return the last accepted snapshot, even when it is stale. */
|
|
22
|
+
get(cwd: string): GitStatus | undefined;
|
|
23
|
+
/** Read when missing or stale; concurrent requests for a cwd share work. */
|
|
24
|
+
request(cwd: string, options?: GitStatusRequestOptions): Promise<GitStatus | undefined>;
|
|
25
|
+
/** Drop cached values and ignore all reads already in flight; service remains usable. */
|
|
26
|
+
reset(): void;
|
|
27
|
+
/** Permanently stop the service, clear values, and ignore late reads. */
|
|
28
|
+
dispose(): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface CacheEntry {
|
|
32
|
+
value: GitStatus | undefined;
|
|
33
|
+
hasValue: boolean;
|
|
34
|
+
updatedAt: number;
|
|
35
|
+
inFlight: Promise<GitStatus | undefined> | undefined;
|
|
36
|
+
forceQueued: boolean;
|
|
37
|
+
forceFollowup: Promise<GitStatus | undefined> | undefined;
|
|
38
|
+
resolveForceFollowup: ((value: GitStatus | undefined) => void) | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function emptyEntry(): CacheEntry {
|
|
42
|
+
return {
|
|
43
|
+
value: undefined,
|
|
44
|
+
hasValue: false,
|
|
45
|
+
updatedAt: 0,
|
|
46
|
+
inFlight: undefined,
|
|
47
|
+
forceQueued: false,
|
|
48
|
+
forceFollowup: undefined,
|
|
49
|
+
resolveForceFollowup: undefined,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function copyStatus(value: unknown): GitStatus | undefined {
|
|
54
|
+
try {
|
|
55
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
56
|
+
const candidate = value as Partial<Record<keyof GitStatus, unknown>>;
|
|
57
|
+
const counts = [candidate.staged, candidate.modified, candidate.untracked, candidate.conflicted];
|
|
58
|
+
if (!counts.every((count) => typeof count === "number" && Number.isSafeInteger(count) && count >= 0)) return undefined;
|
|
59
|
+
return {
|
|
60
|
+
staged: candidate.staged as number,
|
|
61
|
+
modified: candidate.modified as number,
|
|
62
|
+
untracked: candidate.untracked as number,
|
|
63
|
+
conflicted: candidate.conflicted as number,
|
|
64
|
+
};
|
|
65
|
+
} catch {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function sameStatus(left: GitStatus | undefined, right: GitStatus | undefined): boolean {
|
|
71
|
+
return left === undefined || right === undefined
|
|
72
|
+
? left === right
|
|
73
|
+
: left.staged === right.staged && left.modified === right.modified &&
|
|
74
|
+
left.untracked === right.untracked && left.conflicted === right.conflicted;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Small event-driven cache for read-only Git status. It starts no timers and
|
|
79
|
+
* imports no Pi runtime; callers decide when to request and when to repaint.
|
|
80
|
+
*/
|
|
81
|
+
export function createGitStatusCache(options: GitStatusCacheOptions): GitStatusCache {
|
|
82
|
+
const ttlMs = Number.isFinite(options.ttlMs) && (options.ttlMs ?? 0) >= 0
|
|
83
|
+
? options.ttlMs ?? DEFAULT_GIT_STATUS_TTL_MS
|
|
84
|
+
: DEFAULT_GIT_STATUS_TTL_MS;
|
|
85
|
+
const now = options.now ?? Date.now;
|
|
86
|
+
const entries = new Map<string, CacheEntry>();
|
|
87
|
+
let generation = 0;
|
|
88
|
+
let disposed = false;
|
|
89
|
+
|
|
90
|
+
const current = (cwd: string, entry: CacheEntry, readGeneration: number): boolean =>
|
|
91
|
+
!disposed && generation === readGeneration && entries.get(cwd) === entry;
|
|
92
|
+
|
|
93
|
+
const notify = (cwd: string, status: GitStatus | undefined): void => {
|
|
94
|
+
try {
|
|
95
|
+
options.notify?.(cwd, status === undefined ? undefined : { ...status });
|
|
96
|
+
} catch {
|
|
97
|
+
// A repaint callback must never break cache completion.
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const startRead = (cwd: string, entry: CacheEntry): Promise<GitStatus | undefined> => {
|
|
102
|
+
const readGeneration = generation;
|
|
103
|
+
const operation = Promise.resolve()
|
|
104
|
+
.then(() => current(cwd, entry, readGeneration) ? options.read(cwd) : undefined)
|
|
105
|
+
.catch(() => undefined)
|
|
106
|
+
.then((raw) => {
|
|
107
|
+
if (!current(cwd, entry, readGeneration)) return undefined;
|
|
108
|
+
const value = copyStatus(raw);
|
|
109
|
+
const changed = !sameStatus(entry.value, value);
|
|
110
|
+
entry.value = value;
|
|
111
|
+
entry.hasValue = true;
|
|
112
|
+
try {
|
|
113
|
+
entry.updatedAt = now();
|
|
114
|
+
} catch {
|
|
115
|
+
entry.updatedAt = Number.NaN;
|
|
116
|
+
}
|
|
117
|
+
if (changed) notify(cwd, value);
|
|
118
|
+
if (!current(cwd, entry, readGeneration)) return undefined;
|
|
119
|
+
return value === undefined ? undefined : { ...value };
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
entry.inFlight = operation;
|
|
123
|
+
void operation.then(() => {
|
|
124
|
+
if (!current(cwd, entry, readGeneration) || entry.inFlight !== operation) return;
|
|
125
|
+
entry.inFlight = undefined;
|
|
126
|
+
if (!entry.forceQueued) return;
|
|
127
|
+
|
|
128
|
+
entry.forceQueued = false;
|
|
129
|
+
const resolveFollowup = entry.resolveForceFollowup;
|
|
130
|
+
entry.resolveForceFollowup = undefined;
|
|
131
|
+
entry.forceFollowup = undefined;
|
|
132
|
+
// Start before releasing forced callers so requests in the same turn
|
|
133
|
+
// coalesce against this follow-up instead of missing the refresh.
|
|
134
|
+
void startRead(cwd, entry).then((result) => resolveFollowup?.(result));
|
|
135
|
+
});
|
|
136
|
+
return operation;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const invalidate = (): void => {
|
|
140
|
+
generation++;
|
|
141
|
+
for (const entry of entries.values()) {
|
|
142
|
+
entry.resolveForceFollowup?.(undefined);
|
|
143
|
+
entry.resolveForceFollowup = undefined;
|
|
144
|
+
entry.forceFollowup = undefined;
|
|
145
|
+
entry.forceQueued = false;
|
|
146
|
+
}
|
|
147
|
+
entries.clear();
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
get(cwd) {
|
|
152
|
+
if (disposed) return undefined;
|
|
153
|
+
const value = entries.get(cwd)?.value;
|
|
154
|
+
return value === undefined ? undefined : { ...value };
|
|
155
|
+
},
|
|
156
|
+
request(cwd, requestOptions = {}) {
|
|
157
|
+
if (disposed) return Promise.resolve(undefined);
|
|
158
|
+
let entry = entries.get(cwd);
|
|
159
|
+
if (entry === undefined) {
|
|
160
|
+
entry = emptyEntry();
|
|
161
|
+
entries.set(cwd, entry);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const force = requestOptions.force === true;
|
|
165
|
+
if (entry.inFlight !== undefined) {
|
|
166
|
+
if (!force) return entry.inFlight;
|
|
167
|
+
if (entry.forceFollowup !== undefined) return entry.forceFollowup;
|
|
168
|
+
|
|
169
|
+
entry.forceQueued = true;
|
|
170
|
+
entry.forceFollowup = new Promise((resolve) => {
|
|
171
|
+
entry!.resolveForceFollowup = resolve;
|
|
172
|
+
});
|
|
173
|
+
return entry.forceFollowup;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!force && entry.hasValue) {
|
|
177
|
+
let age = Number.NaN;
|
|
178
|
+
try {
|
|
179
|
+
age = now() - entry.updatedAt;
|
|
180
|
+
} catch {
|
|
181
|
+
// A broken injected clock makes the entry stale, never fresh.
|
|
182
|
+
}
|
|
183
|
+
if (Number.isFinite(age) && age >= 0 && age < ttlMs) {
|
|
184
|
+
return Promise.resolve(entry.value === undefined ? undefined : { ...entry.value });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return startRead(cwd, entry);
|
|
188
|
+
},
|
|
189
|
+
reset() {
|
|
190
|
+
if (!disposed) invalidate();
|
|
191
|
+
},
|
|
192
|
+
dispose() {
|
|
193
|
+
if (disposed) return;
|
|
194
|
+
disposed = true;
|
|
195
|
+
invalidate();
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Bounded Git I/O through Pi's public executor; parsing and caching live separately. */
|
|
2
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { parseGitStatusPorcelain, type GitStatus } from "./git-status.ts";
|
|
4
|
+
|
|
5
|
+
export function createGitStatusReader(exec: ExtensionAPI["exec"]) {
|
|
6
|
+
const pending = new Set<AbortController>();
|
|
7
|
+
return {
|
|
8
|
+
async read(cwd: string): Promise<GitStatus | undefined> {
|
|
9
|
+
const controller = new AbortController();
|
|
10
|
+
pending.add(controller);
|
|
11
|
+
try {
|
|
12
|
+
const result = await exec("git", ["--no-optional-locks", "status", "--porcelain=v1", "-z", "--untracked-files=normal"], {
|
|
13
|
+
cwd, signal: controller.signal, timeout: 1500, maxBuffer: 1024 * 1024,
|
|
14
|
+
});
|
|
15
|
+
if (controller.signal.aborted || result.code !== 0 || result.killed || result.truncated) return undefined;
|
|
16
|
+
return parseGitStatusPorcelain(result.stdout);
|
|
17
|
+
} catch {
|
|
18
|
+
return undefined; // missing Git, non-repository, timeout: no invented clean state
|
|
19
|
+
} finally {
|
|
20
|
+
pending.delete(controller);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
cancel(): void {
|
|
24
|
+
for (const controller of pending) controller.abort();
|
|
25
|
+
pending.clear();
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Counts from one `git status --porcelain=v1 -z` snapshot. */
|
|
2
|
+
export interface GitStatus {
|
|
3
|
+
/** Tracked paths whose index differs from HEAD. */
|
|
4
|
+
readonly staged: number;
|
|
5
|
+
/** Tracked paths whose worktree differs from the index. */
|
|
6
|
+
readonly modified: number;
|
|
7
|
+
/** Untracked paths. */
|
|
8
|
+
readonly untracked: number;
|
|
9
|
+
/** Unmerged paths, counted separately from staged and modified. */
|
|
10
|
+
readonly conflicted: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const STATUS_COLUMNS = new Set([" ", "M", "T", "A", "D", "R", "C", "U"]);
|
|
14
|
+
const CONFLICT_CODES = new Set(["DD", "AU", "UD", "UA", "DU", "AA", "UU"]);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Parse Git's NUL-delimited porcelain-v1 status without retaining or exposing
|
|
18
|
+
* paths. A missing final NUL, malformed record, or truncated rename source is
|
|
19
|
+
* unknown (`undefined`), never a clean tree.
|
|
20
|
+
*/
|
|
21
|
+
export function parseGitStatusPorcelain(output: unknown): GitStatus | undefined {
|
|
22
|
+
if (typeof output !== "string") return undefined;
|
|
23
|
+
if (output === "") return { staged: 0, modified: 0, untracked: 0, conflicted: 0 };
|
|
24
|
+
if (!output.endsWith("\0")) return undefined;
|
|
25
|
+
|
|
26
|
+
const records = output.slice(0, -1).split("\0");
|
|
27
|
+
if (records.length === 0 || records.some((record) => record.length === 0)) return undefined;
|
|
28
|
+
|
|
29
|
+
let staged = 0;
|
|
30
|
+
let modified = 0;
|
|
31
|
+
let untracked = 0;
|
|
32
|
+
let conflicted = 0;
|
|
33
|
+
|
|
34
|
+
for (let index = 0; index < records.length; index++) {
|
|
35
|
+
const record = records[index];
|
|
36
|
+
if (record === undefined || record.length < 4 || record[2] !== " ") return undefined;
|
|
37
|
+
|
|
38
|
+
const x = record[0];
|
|
39
|
+
const y = record[1];
|
|
40
|
+
const pair = `${x}${y}`;
|
|
41
|
+
if (pair === " ") return undefined;
|
|
42
|
+
if (pair === "??") {
|
|
43
|
+
untracked++;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
// `!!` is a valid porcelain-v1 ignored entry, though the requested
|
|
47
|
+
// command does not include ignored paths. It contributes no dirty count.
|
|
48
|
+
if (pair === "!!") continue;
|
|
49
|
+
if (x === undefined || y === undefined || !STATUS_COLUMNS.has(x) || !STATUS_COLUMNS.has(y)) return undefined;
|
|
50
|
+
|
|
51
|
+
// Porcelain-v1 has seven defined unmerged pairs, including AA and DD.
|
|
52
|
+
// Other U combinations are malformed rather than ordinary changes.
|
|
53
|
+
if (CONFLICT_CODES.has(pair)) {
|
|
54
|
+
conflicted++;
|
|
55
|
+
} else if (x === "U" || y === "U") {
|
|
56
|
+
return undefined;
|
|
57
|
+
} else {
|
|
58
|
+
if (x !== " ") staged++;
|
|
59
|
+
if (y !== " ") modified++;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Under `-z`, a rename/copy has a second NUL field for the source path.
|
|
63
|
+
// Its content is intentionally discarded; filenames never leave here.
|
|
64
|
+
if (x === "R" || x === "C" || y === "R" || y === "C") {
|
|
65
|
+
index++;
|
|
66
|
+
const source = records[index];
|
|
67
|
+
if (source === undefined || source.length === 0) return undefined;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return { staged, modified, untracked, conflicted };
|
|
72
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public `setHeader` adapter for the pure startup header.
|
|
3
|
+
*
|
|
4
|
+
* Ownership is learned when Pi invokes our factory. Pi disposes a custom header
|
|
5
|
+
* before replacing it; that notification lets clear() avoid removing a header
|
|
6
|
+
* another extension installed later. Pi exposes no getter for the current
|
|
7
|
+
* custom header, so disposal is the public ownership signal.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import { renderHeader } from "./header.ts";
|
|
12
|
+
|
|
13
|
+
export interface HeaderControllerOptions {
|
|
14
|
+
maxWidth?: number;
|
|
15
|
+
minMargin?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type HeaderContext = Pick<ExtensionContext, "cwd" | "ui">;
|
|
19
|
+
|
|
20
|
+
/** Owns installation and safe removal of this extension's custom header. */
|
|
21
|
+
export function createHeaderController(options: HeaderControllerOptions = {}) {
|
|
22
|
+
let ui: HeaderContext["ui"] | undefined;
|
|
23
|
+
let generation = 0;
|
|
24
|
+
let ownedGeneration: number | undefined;
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
install(ctx: HeaderContext): void {
|
|
28
|
+
ui = ctx.ui;
|
|
29
|
+
const thisGeneration = ++generation;
|
|
30
|
+
ctx.ui.setHeader((_tui, factoryTheme) => {
|
|
31
|
+
ownedGeneration = thisGeneration;
|
|
32
|
+
return {
|
|
33
|
+
render(width: number): string[] {
|
|
34
|
+
const theme: Theme = ctx.ui.theme ?? factoryTheme;
|
|
35
|
+
return renderHeader({
|
|
36
|
+
width,
|
|
37
|
+
maxWidth: options.maxWidth,
|
|
38
|
+
minMargin: options.minMargin,
|
|
39
|
+
palette: theme.name ?? "system",
|
|
40
|
+
cwd: ctx.cwd,
|
|
41
|
+
fg: (role, text) => theme.fg(role, text),
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
invalidate(): void {},
|
|
45
|
+
dispose(): void {
|
|
46
|
+
if (ownedGeneration === thisGeneration) ownedGeneration = undefined;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
clear(): void {
|
|
53
|
+
if (ownedGeneration === undefined || !ui) return;
|
|
54
|
+
ownedGeneration = undefined;
|
|
55
|
+
ui.setHeader(undefined);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
package/src/header.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure compact startup header for the 1337 theme package.
|
|
3
|
+
*
|
|
4
|
+
* The caller supplies the live palette's foreground colorizer and the current
|
|
5
|
+
* workspace. This module owns no Pi state and uses the same terminal ruler and
|
|
6
|
+
* centered cap geometry as the framed editor.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { sanitizeInlineText, shortCwd } from "./format.ts";
|
|
10
|
+
import { chooseBox, truncateToWidth, visibleWidth } from "./width.ts";
|
|
11
|
+
|
|
12
|
+
export type HeaderColorRole = "accent" | "muted" | "dim";
|
|
13
|
+
|
|
14
|
+
export interface HeaderOptions {
|
|
15
|
+
/** Terminal width in columns. */
|
|
16
|
+
width: number;
|
|
17
|
+
/** Same outer cap used by the framed editor. */
|
|
18
|
+
maxWidth?: number;
|
|
19
|
+
/** Minimum side margin before the editor's narrow-width fallback. */
|
|
20
|
+
minMargin?: number;
|
|
21
|
+
/** Current theme/palette name (for example, `1337-neon`). */
|
|
22
|
+
palette: string;
|
|
23
|
+
/** Current working directory. */
|
|
24
|
+
cwd: string;
|
|
25
|
+
/** Semantic theme foreground colorizer, typically `theme.fg`. */
|
|
26
|
+
fg: (role: HeaderColorRole, text: string) => string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const DEFAULT_MAX_WIDTH = 100;
|
|
30
|
+
const DEFAULT_MIN_MARGIN = 2;
|
|
31
|
+
const BRAND = "1337";
|
|
32
|
+
const SEPARATOR = " · ";
|
|
33
|
+
const MIN_LABEL_WIDTH = 3;
|
|
34
|
+
const MIN_PATH_WIDTH = 3;
|
|
35
|
+
|
|
36
|
+
interface HeaderLineParts {
|
|
37
|
+
brand: string;
|
|
38
|
+
palette?: string;
|
|
39
|
+
cwd?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Turn the internal theme id into a short, readable palette label. */
|
|
43
|
+
function shortPaletteLabel(value: string): string {
|
|
44
|
+
const safe = sanitizeInlineText(value);
|
|
45
|
+
if (!safe) return "";
|
|
46
|
+
const name = safe.replace(/^1337(?:[-\s]+)?/i, "").trim();
|
|
47
|
+
if (!name) return "PHOSPHOR";
|
|
48
|
+
const normalized = name.replace(/[-_]+/g, " ").replace(/\s+/g, " ").trim();
|
|
49
|
+
if (/^cyberpunk 2077 edgerunner$/i.test(normalized)) return "EDGERUNNER";
|
|
50
|
+
return truncateToWidth(normalized.toUpperCase(), 24);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Prefer a whole path, then a visible final path segment; never split a filename. */
|
|
54
|
+
function fitPath(path: string, width: number): string {
|
|
55
|
+
if (!path || width <= 0) return "";
|
|
56
|
+
if (visibleWidth(path) <= width) return path;
|
|
57
|
+
const basename = path.split("/").filter(Boolean).at(-1) ?? "";
|
|
58
|
+
if (!basename) return "";
|
|
59
|
+
const tail = `…/${basename}`;
|
|
60
|
+
if (visibleWidth(tail) <= width) return tail;
|
|
61
|
+
return visibleWidth(basename) <= width ? basename : "";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function paletteAndPathLine(width: number, palette: string, cwd: string): HeaderLineParts {
|
|
65
|
+
const base = `${BRAND}${SEPARATOR}`;
|
|
66
|
+
const baseWidth = visibleWidth(base);
|
|
67
|
+
const separatorWidth = visibleWidth(SEPARATOR);
|
|
68
|
+
|
|
69
|
+
if (palette && cwd) {
|
|
70
|
+
// First keep both details whole, abbreviating only the path when needed.
|
|
71
|
+
const fullPalettePrefix = `${base}${palette}${SEPARATOR}`;
|
|
72
|
+
const fullPalettePath = fitPath(cwd, width - visibleWidth(fullPalettePrefix));
|
|
73
|
+
if (fullPalettePath) return { brand: BRAND, palette, cwd: fullPalettePath };
|
|
74
|
+
|
|
75
|
+
// On smaller terminals, let the palette label shorten before dropping the
|
|
76
|
+
// workspace. Retain enough room for a useful path tail.
|
|
77
|
+
const pathBudget = Math.min(8, visibleWidth(cwd));
|
|
78
|
+
const paletteBudget = width - baseWidth - separatorWidth - pathBudget;
|
|
79
|
+
if (paletteBudget >= MIN_LABEL_WIDTH && pathBudget >= MIN_PATH_WIDTH) {
|
|
80
|
+
const compactPalette = truncateToWidth(palette, paletteBudget);
|
|
81
|
+
const compactPath = fitPath(cwd, pathBudget);
|
|
82
|
+
if (visibleWidth(compactPalette) >= MIN_LABEL_WIDTH && compactPath) {
|
|
83
|
+
return { brand: BRAND, palette: compactPalette, cwd: compactPath };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (palette) {
|
|
89
|
+
const paletteBudget = width - baseWidth;
|
|
90
|
+
if (visibleWidth(palette) <= paletteBudget) return { brand: BRAND, palette };
|
|
91
|
+
if (paletteBudget >= MIN_LABEL_WIDTH) {
|
|
92
|
+
return { brand: BRAND, palette: truncateToWidth(palette, paletteBudget) };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (cwd) {
|
|
97
|
+
const compactPath = fitPath(cwd, width - baseWidth);
|
|
98
|
+
if (compactPath) return { brand: BRAND, cwd: compactPath };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (visibleWidth(BRAND) <= width) return { brand: BRAND };
|
|
102
|
+
return { brand: truncateToWidth(BRAND, width) };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function commandHint(width: number): string {
|
|
106
|
+
const both = `/theme-1337${SEPARATOR}/settings`;
|
|
107
|
+
if (visibleWidth(both) <= width) return both;
|
|
108
|
+
if (visibleWidth("/theme-1337") <= width) return "/theme-1337";
|
|
109
|
+
if (visibleWidth("/settings") <= width) return "/settings";
|
|
110
|
+
return "";
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Render a vibrant, compact 1–2 line startup header. Dynamic palette/path text
|
|
115
|
+
* is sanitized before formatting, and each line is bounded by the editor's
|
|
116
|
+
* centered box geometry so even very narrow terminals cannot overflow.
|
|
117
|
+
*/
|
|
118
|
+
export function renderHeader(options: HeaderOptions): string[] {
|
|
119
|
+
const rawWidth = Number.isFinite(options.width) ? Math.max(0, Math.floor(options.width)) : 0;
|
|
120
|
+
if (rawWidth === 0) return [];
|
|
121
|
+
const maxWidth = options.maxWidth === Number.POSITIVE_INFINITY
|
|
122
|
+
? Number.POSITIVE_INFINITY
|
|
123
|
+
: Number.isFinite(options.maxWidth)
|
|
124
|
+
? Math.max(1, Math.floor(options.maxWidth!))
|
|
125
|
+
: DEFAULT_MAX_WIDTH;
|
|
126
|
+
const minMargin = Number.isFinite(options.minMargin) ? Math.max(0, Math.floor(options.minMargin!)) : DEFAULT_MIN_MARGIN;
|
|
127
|
+
const geometry = chooseBox(rawWidth, maxWidth, minMargin);
|
|
128
|
+
const inset = geometry.boxWidth >= 28 ? 2 : geometry.boxWidth >= 12 ? 1 : 0;
|
|
129
|
+
const contentWidth = Math.max(1, geometry.boxWidth - inset);
|
|
130
|
+
const leftPad = " ".repeat(geometry.leftMargin + inset);
|
|
131
|
+
const palette = shortPaletteLabel(typeof options.palette === "string" ? options.palette : "");
|
|
132
|
+
const cwd = typeof options.cwd === "string" ? shortCwd(options.cwd) : "";
|
|
133
|
+
const brandLine = paletteAndPathLine(contentWidth, palette, cwd);
|
|
134
|
+
const coloredBrandLine = [
|
|
135
|
+
options.fg("accent", brandLine.brand),
|
|
136
|
+
brandLine.palette ? `${options.fg("dim", SEPARATOR)}${options.fg("muted", brandLine.palette)}` : "",
|
|
137
|
+
brandLine.cwd ? `${options.fg("dim", SEPARATOR)}${options.fg("dim", brandLine.cwd)}` : "",
|
|
138
|
+
].join("");
|
|
139
|
+
const lines = [
|
|
140
|
+
`${leftPad}${coloredBrandLine}`,
|
|
141
|
+
];
|
|
142
|
+
const hint = commandHint(contentWidth);
|
|
143
|
+
if (hint) {
|
|
144
|
+
const commands = hint.split(SEPARATOR);
|
|
145
|
+
const coloredHint = commands.length === 2
|
|
146
|
+
? `${options.fg("muted", commands[0])}${options.fg("dim", SEPARATOR)}${options.fg("muted", commands[1])}`
|
|
147
|
+
: options.fg("muted", hint);
|
|
148
|
+
lines.push(`${leftPad}${coloredHint}`);
|
|
149
|
+
}
|
|
150
|
+
return lines;
|
|
151
|
+
}
|