@lune_99/pi-dock 0.0.0-stage → 0.1.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 +80 -0
- package/LICENSE +21 -0
- package/README.md +83 -2
- package/docs/dock-overview.png +0 -0
- package/docs/dock-overview.svg +12 -0
- package/docs/dock-styles.png +0 -0
- package/docs/dock-styles.svg +30 -0
- package/docs/reference.md +188 -0
- package/package.json +63 -5
- package/src/dock-controller.ts +428 -0
- package/src/dock-footer.ts +212 -0
- package/src/dock-panel.ts +164 -0
- package/src/dock-renderer.ts +104 -0
- package/src/dock-settings.ts +32 -0
- package/src/dock-status.ts +421 -0
- package/src/index.ts +46 -0
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { isAbsolute, relative, sep } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
getAgentDir,
|
|
5
|
+
getLastAssistantUsage,
|
|
6
|
+
SettingsManager,
|
|
7
|
+
type ExtensionContext,
|
|
8
|
+
type ReadonlyFooterDataProvider,
|
|
9
|
+
type SessionEntry,
|
|
10
|
+
type Theme,
|
|
11
|
+
} from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
13
|
+
|
|
14
|
+
/** Nerd Font glyphs, written as escapes so the source stays free of invisible private-use characters. */
|
|
15
|
+
const FOLDER_ICON = "";
|
|
16
|
+
const BRANCH_ICON = "";
|
|
17
|
+
|
|
18
|
+
export const CELL_SEPARATOR = " │ ";
|
|
19
|
+
const SEPARATOR_WIDTH = visibleWidth(CELL_SEPARATOR);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Every statistic owns a slot whose width is fixed up front. Values change on every turn, and a
|
|
23
|
+
* slot that tracked its text would shift every neighbour with it, so each value is padded to the
|
|
24
|
+
* widest text it can take instead.
|
|
25
|
+
*/
|
|
26
|
+
const COUNT_WIDTH = 4; // "999", "9.9k", "999k", "9.9M"
|
|
27
|
+
const PERCENT_WIDTH = 5; // "9.9%", "99.9%", "100%"
|
|
28
|
+
const COST_WIDTH = 7; // "$99.999"
|
|
29
|
+
|
|
30
|
+
export interface DockUsage {
|
|
31
|
+
input: number;
|
|
32
|
+
output: number;
|
|
33
|
+
cacheRead: number;
|
|
34
|
+
cacheWrite: number;
|
|
35
|
+
cost: number;
|
|
36
|
+
/** Share of the latest prompt served from cache, in percent. */
|
|
37
|
+
cacheHitRate: number | undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Everything the info rows display, so rendering never touches Pi. */
|
|
41
|
+
export interface DockStatus {
|
|
42
|
+
cwd: string;
|
|
43
|
+
branch: string | null;
|
|
44
|
+
sessionName: string | undefined;
|
|
45
|
+
model: { provider: string; id: string; thinking: string | undefined } | undefined;
|
|
46
|
+
/** Pi names the provider only when more than one is available. */
|
|
47
|
+
showProvider: boolean;
|
|
48
|
+
usage: DockUsage;
|
|
49
|
+
/** Pi reports no tokens or share until the first response after a compaction. */
|
|
50
|
+
context: { tokens: number | null; percent: number | null; window: number };
|
|
51
|
+
autoCompact: boolean;
|
|
52
|
+
extensionStatuses: readonly string[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Pi keeps its formatter private; this matches the rounding of its own footer. The extra `G` tier
|
|
57
|
+
* keeps every result within four columns, which is all a counter slot reserves.
|
|
58
|
+
*/
|
|
59
|
+
function formatTokens(count: number): string {
|
|
60
|
+
if (count < 1000) return String(count);
|
|
61
|
+
const units = ["k", "M", "G"];
|
|
62
|
+
let scaled = count / 1000;
|
|
63
|
+
for (let unit = 0; ; unit++) {
|
|
64
|
+
const text = scaled < 9.95 ? scaled.toFixed(1) : String(Math.round(scaled));
|
|
65
|
+
// Rounding can carry into the next unit (999.6k), which must read 1.0M, not 1000k.
|
|
66
|
+
if (Number(text) < 1000 || unit === units.length - 1) return text + units[unit];
|
|
67
|
+
scaled /= 1000;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Whole percent once the decimal would no longer fit, so 100% costs no extra column. */
|
|
72
|
+
function formatPercent(percent: number): string {
|
|
73
|
+
return percent >= 99.95 ? `${Math.round(percent)}%` : `${percent.toFixed(1)}%`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Spends fewer decimals as the amount grows so the text never outgrows its slot. */
|
|
77
|
+
function formatCost(cost: number): string {
|
|
78
|
+
for (const decimals of [3, 2, 1, 0]) {
|
|
79
|
+
const text = `$${cost.toFixed(decimals)}`;
|
|
80
|
+
if (text.length <= COST_WIDTH) return text;
|
|
81
|
+
}
|
|
82
|
+
return `$${Math.round(cost)}`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function abbreviateHome(cwd: string): string {
|
|
86
|
+
const fromHome = relative(homedir(), cwd);
|
|
87
|
+
if (fromHome === "") return "~";
|
|
88
|
+
if (fromHome === ".." || fromHome.startsWith(`..${sep}`) || isAbsolute(fromHome)) return cwd;
|
|
89
|
+
return `~${sep}${fromHome}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface UsageLike {
|
|
93
|
+
input: number;
|
|
94
|
+
output: number;
|
|
95
|
+
cacheRead: number;
|
|
96
|
+
cacheWrite: number;
|
|
97
|
+
cost: { total: number };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Totals span every session entry, not just the current branch, as Pi's footer does. */
|
|
101
|
+
function summarizeUsage(entries: SessionEntry[]): DockUsage {
|
|
102
|
+
const usage: DockUsage = {
|
|
103
|
+
input: 0,
|
|
104
|
+
output: 0,
|
|
105
|
+
cacheRead: 0,
|
|
106
|
+
cacheWrite: 0,
|
|
107
|
+
cost: 0,
|
|
108
|
+
cacheHitRate: undefined,
|
|
109
|
+
};
|
|
110
|
+
const add = (part: UsageLike): void => {
|
|
111
|
+
usage.input += part.input;
|
|
112
|
+
usage.output += part.output;
|
|
113
|
+
usage.cacheRead += part.cacheRead;
|
|
114
|
+
usage.cacheWrite += part.cacheWrite;
|
|
115
|
+
usage.cost += part.cost.total;
|
|
116
|
+
};
|
|
117
|
+
for (const entry of entries) {
|
|
118
|
+
if (entry.type === "usage") {
|
|
119
|
+
add(entry.usage);
|
|
120
|
+
} else if (entry.type === "message") {
|
|
121
|
+
if (entry.message.role === "assistant") add(entry.message.usage);
|
|
122
|
+
else if (entry.message.role === "toolResult" && entry.message.usage) {
|
|
123
|
+
add(entry.message.usage);
|
|
124
|
+
}
|
|
125
|
+
} else if (
|
|
126
|
+
(entry.type === "branch_summary" || entry.type === "compaction") &&
|
|
127
|
+
entry.usage
|
|
128
|
+
) {
|
|
129
|
+
add(entry.usage);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const latest = getLastAssistantUsage(entries);
|
|
133
|
+
if (latest) {
|
|
134
|
+
const prompt = latest.input + latest.cacheRead + latest.cacheWrite;
|
|
135
|
+
usage.cacheHitRate = prompt > 0 ? (latest.cacheRead / prompt) * 100 : undefined;
|
|
136
|
+
}
|
|
137
|
+
return usage;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function readAutoCompact(ctx: ExtensionContext): boolean {
|
|
141
|
+
return SettingsManager.create(ctx.cwd, getAgentDir(), {
|
|
142
|
+
projectTrusted: ctx.isProjectTrusted(),
|
|
143
|
+
}).getCompactionEnabled();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Reads the statistics Pi publishes to extensions; the dock replaces Pi's footer, so it must show them. */
|
|
147
|
+
export class DockStatusSource {
|
|
148
|
+
private usage: { key: string; value: DockUsage } | undefined;
|
|
149
|
+
private autoCompact: { ctx: ExtensionContext; value: boolean } | undefined;
|
|
150
|
+
private readonly readAutoCompact: (ctx: ExtensionContext) => boolean;
|
|
151
|
+
|
|
152
|
+
constructor(readCompaction: (ctx: ExtensionContext) => boolean = readAutoCompact) {
|
|
153
|
+
this.readAutoCompact = readCompaction;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
collect(ctx: ExtensionContext, footerData: ReadonlyFooterDataProvider): DockStatus {
|
|
157
|
+
const model = ctx.model;
|
|
158
|
+
const level = ctx.thinkingLevel ?? "off";
|
|
159
|
+
const contextUsage = ctx.getContextUsage();
|
|
160
|
+
const statuses = [...footerData.getExtensionStatuses()]
|
|
161
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
162
|
+
.map(([, text]) =>
|
|
163
|
+
text
|
|
164
|
+
.replace(/[\r\n\t]/g, " ")
|
|
165
|
+
.replace(/ +/g, " ")
|
|
166
|
+
.trim(),
|
|
167
|
+
)
|
|
168
|
+
.filter((text) => text.length > 0);
|
|
169
|
+
return {
|
|
170
|
+
cwd: abbreviateHome(ctx.sessionManager.getCwd()),
|
|
171
|
+
branch: footerData.getGitBranch(),
|
|
172
|
+
sessionName: ctx.sessionManager.getSessionName(),
|
|
173
|
+
model: model && {
|
|
174
|
+
provider: model.provider,
|
|
175
|
+
id: model.id,
|
|
176
|
+
thinking: model.reasoning ? (level === "off" ? "thinking off" : level) : undefined,
|
|
177
|
+
},
|
|
178
|
+
showProvider: footerData.getAvailableProviderCount() > 1,
|
|
179
|
+
usage: this.collectUsage(ctx),
|
|
180
|
+
context: {
|
|
181
|
+
tokens: contextUsage?.tokens ?? null,
|
|
182
|
+
percent: contextUsage?.percent ?? null,
|
|
183
|
+
window: contextUsage?.contextWindow ?? model?.contextWindow ?? 0,
|
|
184
|
+
},
|
|
185
|
+
autoCompact: this.collectAutoCompact(ctx),
|
|
186
|
+
extensionStatuses: statuses,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** The scan is linear in the session and runs every frame; entries are append-only and each append moves the leaf. */
|
|
191
|
+
private collectUsage(ctx: ExtensionContext): DockUsage {
|
|
192
|
+
const entries = ctx.sessionManager.getEntries();
|
|
193
|
+
const key = `${ctx.sessionManager.getSessionId()}\0${ctx.sessionManager.getLeafId()}\0${entries.length}`;
|
|
194
|
+
if (this.usage?.key !== key) this.usage = { key, value: summarizeUsage(entries) };
|
|
195
|
+
return this.usage.value;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Settings files are read once per session context, not per frame. */
|
|
199
|
+
private collectAutoCompact(ctx: ExtensionContext): boolean {
|
|
200
|
+
if (this.autoCompact?.ctx !== ctx) {
|
|
201
|
+
this.autoCompact = { ctx, value: this.readAutoCompact(ctx) };
|
|
202
|
+
}
|
|
203
|
+
return this.autoCompact.value;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The provider is the first thing to go when the model name has to fit a narrow budget. */
|
|
208
|
+
export function renderModelSegment(status: DockStatus, theme: Theme, width: number): string {
|
|
209
|
+
const model = status.model;
|
|
210
|
+
const name = model?.id ?? "no-model";
|
|
211
|
+
const thinking = model?.thinking ? theme.fg("dim", ` • ${model.thinking}`) : "";
|
|
212
|
+
const bare = name + thinking;
|
|
213
|
+
const withProvider =
|
|
214
|
+
status.showProvider && model ? theme.fg("accent", `(${model.provider}) `) + bare : bare;
|
|
215
|
+
return visibleWidth(withProvider) <= width ? withProvider : truncateToWidth(bare, width);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Right-aligned, so a growing number extends leftwards into its own padding and nothing else moves. */
|
|
219
|
+
function field(text: string, width: number): string {
|
|
220
|
+
const gap = width - visibleWidth(text);
|
|
221
|
+
return gap >= 0 ? " ".repeat(gap) + text : truncateToWidth(text, width);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function value(theme: Theme, text: string, width: number): string {
|
|
225
|
+
return field(theme.fg("muted", text), width);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const CONTEXT_JOINER = " · ";
|
|
229
|
+
const CONTEXT_WIDTH = COUNT_WIDTH + 1 + COUNT_WIDTH + visibleWidth(CONTEXT_JOINER) + PERCENT_WIDTH;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Tokens in use over the window, then the share. Turns warning/error at the same thresholds as Pi's
|
|
233
|
+
* own footer. The window is constant for a model, so only the used count and share need room to grow.
|
|
234
|
+
*/
|
|
235
|
+
export function renderContextCell(status: DockStatus, theme: Theme): string {
|
|
236
|
+
const { tokens, percent, window } = status.context;
|
|
237
|
+
const used = field(tokens === null ? "?" : formatTokens(tokens), COUNT_WIDTH);
|
|
238
|
+
const share = field(percent === null ? "?" : formatPercent(percent), PERCENT_WIDTH);
|
|
239
|
+
return theme.fg(
|
|
240
|
+
(percent ?? 0) > 90 ? "error" : (percent ?? 0) > 70 ? "warning" : "muted",
|
|
241
|
+
field(`${used}/${formatTokens(window)}${CONTEXT_JOINER}${share}`, CONTEXT_WIDTH),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
interface StatSlot {
|
|
246
|
+
/** Higher survives longer when the row is too narrow. */
|
|
247
|
+
keep: number;
|
|
248
|
+
/** Columns reserved for the slot with every part present, whatever the values are. */
|
|
249
|
+
width: number;
|
|
250
|
+
/** Nothing is drawn for a slot without data, and `render` leaves out any part that has none. */
|
|
251
|
+
hasData(status: DockStatus): boolean;
|
|
252
|
+
render(status: DockStatus, theme: Theme): string;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** A counter that is still zero is left out, so a provider that never reports it shows no gap. */
|
|
256
|
+
function counters(theme: Theme, ...parts: [label: string, count: number][]): string {
|
|
257
|
+
return parts
|
|
258
|
+
.filter(([, count]) => count > 0)
|
|
259
|
+
.map(
|
|
260
|
+
([label, count]) =>
|
|
261
|
+
theme.fg("muted", label) + value(theme, formatTokens(count), COUNT_WIDTH),
|
|
262
|
+
)
|
|
263
|
+
.join(" ");
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Which slots exist depends on the session, never on the numbers: auto-compaction is a setting
|
|
268
|
+
* read once per session, so (auto) only claims its columns when it will be shown. Whether a slot
|
|
269
|
+
* is drawn is a separate question, answered by `hasData`.
|
|
270
|
+
*/
|
|
271
|
+
function statSlots(status: DockStatus): StatSlot[] {
|
|
272
|
+
const counterWidth = 2 * (1 + COUNT_WIDTH) + 1;
|
|
273
|
+
const slots: StatSlot[] = [
|
|
274
|
+
{
|
|
275
|
+
keep: 3,
|
|
276
|
+
width: counterWidth,
|
|
277
|
+
hasData: ({ usage }) => usage.input > 0 || usage.output > 0,
|
|
278
|
+
render: ({ usage }, theme) => counters(theme, ["↑", usage.input], ["↓", usage.output]),
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
keep: 1,
|
|
282
|
+
width: counterWidth,
|
|
283
|
+
hasData: ({ usage }) => usage.cacheRead > 0 || usage.cacheWrite > 0,
|
|
284
|
+
render: ({ usage }, theme) =>
|
|
285
|
+
counters(theme, ["R", usage.cacheRead], ["W", usage.cacheWrite]),
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
keep: 2,
|
|
289
|
+
width: 2 + PERCENT_WIDTH,
|
|
290
|
+
// A provider that reports no cache traffic has no hit rate worth quoting.
|
|
291
|
+
hasData: ({ usage }) =>
|
|
292
|
+
(usage.cacheRead > 0 || usage.cacheWrite > 0) && usage.cacheHitRate !== undefined,
|
|
293
|
+
render: ({ usage }, theme) =>
|
|
294
|
+
theme.fg("muted", "CH") +
|
|
295
|
+
value(theme, formatPercent(usage.cacheHitRate ?? 0), PERCENT_WIDTH),
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
keep: 4,
|
|
299
|
+
width: COST_WIDTH,
|
|
300
|
+
hasData: ({ usage }) => usage.cost > 0,
|
|
301
|
+
render: ({ usage }, theme) => value(theme, formatCost(usage.cost), COST_WIDTH),
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
keep: 6,
|
|
305
|
+
width: CONTEXT_WIDTH,
|
|
306
|
+
hasData: () => true,
|
|
307
|
+
render: (current, theme) => renderContextCell(current, theme),
|
|
308
|
+
},
|
|
309
|
+
];
|
|
310
|
+
if (status.autoCompact) {
|
|
311
|
+
slots.push({
|
|
312
|
+
keep: 5,
|
|
313
|
+
width: 6,
|
|
314
|
+
hasData: () => true,
|
|
315
|
+
render: (_current, theme) => theme.fg("accent", "(auto)"),
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
return slots;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function slotsWidth(slots: readonly StatSlot[]): number {
|
|
322
|
+
return slots.reduce((sum, slot) => sum + slot.width, 0) + SEPARATOR_WIDTH * (slots.length - 1);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Drops the least important slots until the rest fit. Only the reserved widths are compared, so
|
|
327
|
+
* the same terminal width always yields the same slots, whatever the numbers currently say.
|
|
328
|
+
*/
|
|
329
|
+
function fitSlots(slots: StatSlot[], budget: number): StatSlot[] {
|
|
330
|
+
const kept = [...slots];
|
|
331
|
+
while (kept.length > 1 && slotsWidth(kept) > budget) {
|
|
332
|
+
const weakest = kept.reduce((low, slot) => (slot.keep < low.keep ? slot : low));
|
|
333
|
+
kept.splice(kept.indexOf(weakest), 1);
|
|
334
|
+
}
|
|
335
|
+
return kept;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Keeps the trailing segments, which name the project, and elides the leading ones. */
|
|
339
|
+
function shortenPath(path: string, width: number): string {
|
|
340
|
+
if (visibleWidth(path) <= width) return path;
|
|
341
|
+
const segments = path.split(sep);
|
|
342
|
+
for (let count = segments.length - 1; count >= 1; count--) {
|
|
343
|
+
const candidate = `…${sep}${segments.slice(-count).join(sep)}`;
|
|
344
|
+
if (visibleWidth(candidate) <= width) return candidate;
|
|
345
|
+
}
|
|
346
|
+
return truncateToWidth(segments.at(-1) ?? path, Math.max(0, width));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export interface InfoRowParts {
|
|
350
|
+
workspace: boolean;
|
|
351
|
+
usage: boolean;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Room kept for the directory and branch before any statistic is placed. A fixed share, not the
|
|
356
|
+
* current path, so a branch switch cannot make a counter appear or vanish.
|
|
357
|
+
*/
|
|
358
|
+
const MIN_WORKSPACE_WIDTH = 40;
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Working directory and branch on the left, usage statistics right-aligned in reserved slots.
|
|
362
|
+
* The slots reserved follow from the terminal width alone, and a slot with no data yet is simply
|
|
363
|
+
* not drawn. Changing numbers only ever rewrite the padding inside their own slot.
|
|
364
|
+
*/
|
|
365
|
+
export function renderInfoRow(
|
|
366
|
+
status: DockStatus,
|
|
367
|
+
width: number,
|
|
368
|
+
theme: Theme,
|
|
369
|
+
parts: InfoRowParts = { workspace: true, usage: true },
|
|
370
|
+
): string {
|
|
371
|
+
const branch = status.branch ? ` ${BRANCH_ICON} (${status.branch})` : "";
|
|
372
|
+
const session = status.sessionName ? ` • ${status.sessionName}` : "";
|
|
373
|
+
const fixedWidth = visibleWidth(` ${FOLDER_ICON} ${branch}${session}`);
|
|
374
|
+
|
|
375
|
+
const bar = parts.workspace ? theme.fg("dim", CELL_SEPARATOR) : "";
|
|
376
|
+
const budget = width - (parts.workspace ? MIN_WORKSPACE_WIDTH + visibleWidth(bar) : 0);
|
|
377
|
+
const reserved = parts.usage ? fitSlots(statSlots(status), budget) : [];
|
|
378
|
+
const slots = reserved.filter((slot) => slot.hasData(status));
|
|
379
|
+
// The right block is right-aligned, so a slot gaining data never moves the slots to its right;
|
|
380
|
+
// the directory is shortened against the full reservation so it does not move either.
|
|
381
|
+
const reservedWidth = reserved.length > 0 ? visibleWidth(bar) + slotsWidth(reserved) : 0;
|
|
382
|
+
const right =
|
|
383
|
+
slots.length > 0
|
|
384
|
+
? truncateToWidth(
|
|
385
|
+
bar +
|
|
386
|
+
slots
|
|
387
|
+
.map((slot) => slot.render(status, theme))
|
|
388
|
+
.join(theme.fg("dim", CELL_SEPARATOR)),
|
|
389
|
+
width,
|
|
390
|
+
)
|
|
391
|
+
: "";
|
|
392
|
+
|
|
393
|
+
let left = "";
|
|
394
|
+
if (parts.workspace) {
|
|
395
|
+
const cwd = shortenPath(status.cwd, width - reservedWidth - fixedWidth);
|
|
396
|
+
left =
|
|
397
|
+
` ${theme.fg("muted", `${FOLDER_ICON} ${cwd}`)}` +
|
|
398
|
+
(branch && theme.fg("accent", branch)) +
|
|
399
|
+
theme.fg("dim", session);
|
|
400
|
+
}
|
|
401
|
+
const clipped = truncateToWidth(
|
|
402
|
+
left,
|
|
403
|
+
Math.max(0, width - visibleWidth(right)),
|
|
404
|
+
theme.fg("dim", "…"),
|
|
405
|
+
);
|
|
406
|
+
return (
|
|
407
|
+
clipped +
|
|
408
|
+
" ".repeat(Math.max(0, width - visibleWidth(clipped) - visibleWidth(right))) +
|
|
409
|
+
right
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Statuses set through `ctx.ui.setStatus()` lived in Pi's footer, so the dock keeps showing them. */
|
|
414
|
+
export function renderStatusRow(
|
|
415
|
+
statuses: readonly string[],
|
|
416
|
+
width: number,
|
|
417
|
+
theme: Theme,
|
|
418
|
+
): string | undefined {
|
|
419
|
+
if (statuses.length === 0) return undefined;
|
|
420
|
+
return truncateToWidth(` ${statuses.join(" ")}`, width, theme.fg("dim", "…"));
|
|
421
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { bindSettings, registerSettingsPanel, scopedExtension } from "@lune_99/pi-lune-extensions-core";
|
|
3
|
+
import { openSettingsPanel } from "@lune_99/pi-lune-extensions-core/panel/tui";
|
|
4
|
+
import { DockController } from "./dock-controller.ts";
|
|
5
|
+
import { dockPanel } from "./dock-panel.ts";
|
|
6
|
+
import { dockSettings } from "./dock-settings.ts";
|
|
7
|
+
|
|
8
|
+
export default scopedExtension((pi, resources) => {
|
|
9
|
+
const dock = new DockController();
|
|
10
|
+
const settings = resources.use(bindSettings(pi, dockSettings));
|
|
11
|
+
resources.use(registerSettingsPanel(dockPanel, settings));
|
|
12
|
+
// Dock teardown runs before the borrowed settings' own shutdown.
|
|
13
|
+
resources.defer(() => dock.detach());
|
|
14
|
+
|
|
15
|
+
resources.defer(
|
|
16
|
+
pi.on("session_start", async (_event, ctx: ExtensionContext) => {
|
|
17
|
+
dock.detach();
|
|
18
|
+
let attached = false;
|
|
19
|
+
const view = await settings.view(ctx);
|
|
20
|
+
const apply = () => {
|
|
21
|
+
const { enabled, ...appearance } = view.snapshot();
|
|
22
|
+
dock.configure(appearance);
|
|
23
|
+
// Re-attaching would drop focus and the cell window, so only enabling or disabling does it.
|
|
24
|
+
if (enabled && !attached) dock.attach(ctx);
|
|
25
|
+
else if (!enabled && attached) dock.detach();
|
|
26
|
+
attached = enabled;
|
|
27
|
+
};
|
|
28
|
+
apply();
|
|
29
|
+
// Style, visibility and enabling take effect without a restart; the view retires the subscription.
|
|
30
|
+
view.subscribe(apply);
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
resources.defer(pi.on("session_before_tree", () => dock.resetInteraction()));
|
|
34
|
+
resources.defer(pi.on("session_tree", () => dock.invalidate()));
|
|
35
|
+
|
|
36
|
+
pi.registerCommand("dock", {
|
|
37
|
+
description: "Open Lune Dock settings",
|
|
38
|
+
handler: async (_args, ctx) => {
|
|
39
|
+
if (ctx.mode !== "tui") {
|
|
40
|
+
ctx.ui.notify("Dock settings require interactive TUI mode", "warning");
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
await openSettingsPanel(ctx, { section: dockPanel.id });
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
});
|