@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.
@@ -0,0 +1,428 @@
1
+ import {
2
+ CustomEditor,
3
+ type ExtensionContext,
4
+ type ReadonlyFooterDataProvider,
5
+ type Theme,
6
+ } from "@earendil-works/pi-coding-agent";
7
+ import {
8
+ isKeyRelease,
9
+ matchesKey,
10
+ type Component,
11
+ type TuiMainScreen,
12
+ type TuiAltScreen,
13
+ } from "@earendil-works/pi-tui";
14
+ import { getDockRegistry, type DockRegistry } from "@lune_99/pi-lune-extensions-core/dock/host";
15
+ import type { LuneDockDisplayMode } from "@lune_99/pi-lune-extensions-core/dock";
16
+ import { DockFooter, replacesFooter, type DockFooterInput, type DockLayout } from "./dock-footer.ts";
17
+ import type { VisibleDockItem } from "./dock-renderer.ts";
18
+ import { DockStatusSource, type DockStatus } from "./dock-status.ts";
19
+
20
+ export const WIDGET_ID = "pi-dock";
21
+
22
+ export class DockComponent implements Component {
23
+ private isFocused = false;
24
+ private readonly controller: DockController;
25
+ private readonly theme: Theme;
26
+
27
+ constructor(controller: DockController, theme: Theme) {
28
+ this.controller = controller;
29
+ this.theme = theme;
30
+ }
31
+
32
+ get focused(): boolean {
33
+ return this.isFocused;
34
+ }
35
+
36
+ set focused(value: boolean) {
37
+ this.isFocused = value;
38
+ if (value) this.controller.onFocusRestored(this);
39
+ }
40
+
41
+ render(width: number): string[] {
42
+ const input = this.controller.footerInput(this.focused);
43
+ return input ? this.controller.view.render(width, this.theme, input) : [];
44
+ }
45
+
46
+ handleInput(data: string): void {
47
+ this.controller.handleFocusedInput(data);
48
+ }
49
+
50
+ invalidate(): void {
51
+ this.controller.invalidateSnapshots();
52
+ }
53
+
54
+ dispose(): void {
55
+ this.controller.releaseComponent(this);
56
+ }
57
+ }
58
+
59
+ /** The part of the dock settings the controller acts on; enabling and disabling belong to the caller. */
60
+ export interface DockControllerSettings extends DockLayout {
61
+ displayMode: LuneDockDisplayMode;
62
+ hidden: readonly string[];
63
+ order: Readonly<Record<string, number>>;
64
+ }
65
+
66
+ export class DockController {
67
+ items: readonly VisibleDockItem[] = [];
68
+ selectedId: string | undefined;
69
+ displayMode: LuneDockDisplayMode = "detail";
70
+ private layout: DockLayout = {
71
+ style: "ruled",
72
+ showModel: true,
73
+ showWorkspace: true,
74
+ showUsage: true,
75
+ };
76
+ private hidden: ReadonlySet<string> = new Set();
77
+ private order: Readonly<Record<string, number>> = {};
78
+
79
+ configure(settings: DockControllerSettings): void {
80
+ const tookFooter = replacesFooter(this.layout.style);
81
+ this.displayMode = settings.displayMode;
82
+ this.layout = {
83
+ style: settings.style,
84
+ showModel: settings.showModel,
85
+ showWorkspace: settings.showWorkspace,
86
+ showUsage: settings.showUsage,
87
+ };
88
+ this.hidden = new Set(settings.hidden);
89
+ this.order = settings.order;
90
+ // Only a style switch touches the footer, so an unrelated setting never reclaims it from another extension.
91
+ if (this.ctx && tookFooter !== replacesFooter(this.layout.style)) this.syncFooter();
92
+ this.refresh();
93
+ }
94
+
95
+ invalidateSnapshots(): void {
96
+ if (!this.ctx) return;
97
+ const components = new Set<Component>();
98
+ for (const { snapshot } of this.registry.getContributions(this.ctx.ui)) {
99
+ components.add(snapshot.base);
100
+ if (snapshot.detail) components.add(snapshot.detail);
101
+ if (snapshot.full) components.add(snapshot.full);
102
+ }
103
+ for (const component of components) component.invalidate();
104
+ }
105
+ /** Owned by the controller, not the component: the widget factory may build a new component at any time. */
106
+ readonly view = new DockFooter();
107
+ private ctx: ExtensionContext | undefined;
108
+ private footerData: ReadonlyFooterDataProvider | undefined;
109
+ private footerMounted = false;
110
+ private tui: TuiMainScreen | TuiAltScreen | undefined;
111
+ private component: DockComponent | undefined;
112
+ private editor: Component | undefined;
113
+ private releaseHost: (() => void) | undefined;
114
+ private unsubscribeInput: (() => void) | undefined;
115
+ private queuedGeneration: number | undefined;
116
+ private generation = 0;
117
+ private opening = false;
118
+ private readonly registry: DockRegistry;
119
+ private readonly statusSource: DockStatusSource;
120
+
121
+ constructor(
122
+ registry: DockRegistry = getDockRegistry(),
123
+ statusSource: DockStatusSource = new DockStatusSource(),
124
+ ) {
125
+ this.registry = registry;
126
+ this.statusSource = statusSource;
127
+ }
128
+
129
+ /** Undefined once detached: the widget may render one last frame after its footer data is gone. */
130
+ collectStatus(): DockStatus | undefined {
131
+ if (!this.ctx || !this.footerData) return undefined;
132
+ return this.statusSource.collect(this.ctx, this.footerData);
133
+ }
134
+
135
+ /** Nothing to draw before attachment, or when a style that shows statistics has no footer data yet. */
136
+ footerInput(focused: boolean): DockFooterInput | undefined {
137
+ if (!this.ctx) return undefined;
138
+ const status = replacesFooter(this.layout.style) ? this.collectStatus() : undefined;
139
+ if (replacesFooter(this.layout.style) && !status) return undefined;
140
+ return {
141
+ entries: this.items,
142
+ status,
143
+ selectedId: focused ? this.selectedId : undefined,
144
+ mode: this.displayMode,
145
+ layout: this.layout,
146
+ };
147
+ }
148
+
149
+ attach(ctx: ExtensionContext): void {
150
+ this.detach();
151
+ if (ctx.mode !== "tui") return;
152
+ this.ctx = ctx;
153
+ if (replacesFooter(this.layout.style)) this.mountFooter();
154
+ this.mountWidget();
155
+ this.unsubscribeInput = ctx.ui.onTerminalInput((data) => {
156
+ try {
157
+ return this.handleEntryInput(data);
158
+ } catch (error) {
159
+ this.reportError(error);
160
+ return { consume: true };
161
+ }
162
+ });
163
+ try {
164
+ this.releaseHost = this.registry.attachHost(ctx.ui, this);
165
+ this.refresh();
166
+ } catch (error) {
167
+ this.detach();
168
+ throw error;
169
+ }
170
+ }
171
+
172
+ detach(): void {
173
+ this.generation++;
174
+ this.queuedGeneration = undefined;
175
+ this.unsubscribeInput?.();
176
+ this.unsubscribeInput = undefined;
177
+ this.leaveFocus();
178
+ const ctx = this.ctx;
179
+ this.ctx = undefined;
180
+ ctx?.ui.setWidget(WIDGET_ID, undefined);
181
+ // Restoring unconditionally would evict a footer another extension installed after ours.
182
+ if (this.footerMounted) ctx?.ui.setFooter(undefined);
183
+ this.footerData = undefined;
184
+ this.releaseHost?.();
185
+ this.releaseHost = undefined;
186
+ this.tui = undefined;
187
+ this.component = undefined;
188
+ this.editor = undefined;
189
+ this.items = [];
190
+ this.selectedId = undefined;
191
+ this.opening = false;
192
+ }
193
+
194
+ invalidate(): void {
195
+ if (!this.ctx || this.queuedGeneration === this.generation) return;
196
+ const generation = this.generation;
197
+ this.queuedGeneration = generation;
198
+ queueMicrotask(() => {
199
+ if (this.queuedGeneration !== generation) return;
200
+ this.queuedGeneration = undefined;
201
+ if (generation !== this.generation || !this.ctx) return;
202
+ try {
203
+ this.refresh();
204
+ } catch (error) {
205
+ this.reportError(error);
206
+ }
207
+ });
208
+ }
209
+
210
+ /** A tree jump must not let an old panel continuation recapture the new branch's focus. */
211
+ resetInteraction(): void {
212
+ this.generation++;
213
+ this.opening = false;
214
+ this.leaveFocus();
215
+ this.refresh();
216
+ }
217
+
218
+ releaseComponent(component: DockComponent): void {
219
+ if (this.component !== component) return;
220
+ this.leaveFocus();
221
+ this.component = undefined;
222
+ }
223
+
224
+ /** An unrelated overlay may restore an empty dock after its final item disappeared. */
225
+ onFocusRestored(component: DockComponent): void {
226
+ if (this.items.length > 0) return;
227
+ const generation = this.generation;
228
+ queueMicrotask(() => {
229
+ if (
230
+ generation === this.generation &&
231
+ this.component === component &&
232
+ this.items.length === 0
233
+ ) {
234
+ this.leaveFocus();
235
+ }
236
+ });
237
+ }
238
+
239
+ /**
240
+ * The dock draws the model, directory and usage rows itself, so Pi's own footer must render
241
+ * nothing. The footer factory is also the only place Pi hands extensions its footer data.
242
+ */
243
+ private syncFooter(): void {
244
+ if (replacesFooter(this.layout.style)) {
245
+ this.mountFooter();
246
+ } else if (this.footerMounted) {
247
+ this.ctx!.ui.setFooter(undefined);
248
+ }
249
+ }
250
+
251
+ private mountFooter(): void {
252
+ this.ctx!.ui.setFooter((_tui, _theme, footerData) => {
253
+ // Pi shares one provider for the whole app, so it outlives this footer being replaced.
254
+ this.footerData = footerData;
255
+ this.footerMounted = true;
256
+ const unsubscribe = footerData.onBranchChange(() => this.tui?.requestRender());
257
+ return {
258
+ render: () => [],
259
+ invalidate() {},
260
+ dispose: () => {
261
+ unsubscribe();
262
+ this.footerMounted = false;
263
+ },
264
+ };
265
+ });
266
+ }
267
+
268
+ private mountWidget(): void {
269
+ const previous = this.component;
270
+ this.ctx!.ui.setWidget(
271
+ WIDGET_ID,
272
+ (tui, theme) => {
273
+ // Both concrete Pi renderers expose focus inspection; the shared TUI interface
274
+ // omits that method even though it is public on their common base class.
275
+ this.tui = tui as TuiMainScreen | TuiAltScreen;
276
+ this.component = new DockComponent(this, theme);
277
+ if (previous && this.tui.getFocusedComponent() === previous) {
278
+ this.tui.setFocus(this.component);
279
+ }
280
+ return this.component;
281
+ },
282
+ { placement: "belowEditor" },
283
+ );
284
+ }
285
+
286
+ private synchronizeTui(): void {
287
+ // Pi moves the same component tree between regular/fullscreen renderers and sets
288
+ // the retired renderer's focus to null. Re-running our factory obtains the active
289
+ // renderer without replacing the main editor or accessing private Pi state.
290
+ if (this.ctx && this.tui && this.tui.getFocusedComponent() === null) this.mountWidget();
291
+ }
292
+
293
+ private refresh(): void {
294
+ if (!this.ctx) return;
295
+ this.synchronizeTui();
296
+ const previousIndex = this.items.findIndex((entry) => entry.id === this.selectedId);
297
+ this.items = this.registry
298
+ .getContributions(this.ctx.ui)
299
+ .filter(({ id }) => !this.hidden.has(id))
300
+ .sort((a, b) => (this.order[a.id] ?? 0) - (this.order[b.id] ?? 0))
301
+ .map(({ id, snapshot }) => ({ id, snapshot }));
302
+ if (!this.items.some((entry) => entry.id === this.selectedId)) {
303
+ this.selectedId =
304
+ this.items[Math.max(0, Math.min(previousIndex, this.items.length - 1))]?.id;
305
+ }
306
+ if (this.items.length === 0) this.leaveFocus();
307
+ this.tui?.requestRender();
308
+ }
309
+
310
+ private handleEntryInput(data: string): { consume: true } | undefined {
311
+ if (isKeyRelease(data) || this.opening || !this.ctx || !this.component || !this.tui) return;
312
+ const forced = matchesKey(data, "super+down");
313
+ if (!forced && !matchesKey(data, "down")) return;
314
+ this.synchronizeTui();
315
+ if (this.tui.hasOverlay()) return;
316
+ const focused = this.tui.getFocusedComponent();
317
+ // CustomEditor is Pi's main-editor integration contract. An extension selector or
318
+ // standalone editor must keep its own navigation even when the main text is empty.
319
+ if (!(focused instanceof CustomEditor) || focused.isShowingAutocomplete()) return;
320
+ if (!forced && this.ctx.ui.getEditorText().length !== 0) return;
321
+ this.refresh();
322
+ if (this.items.length === 0) return;
323
+ this.editor = focused;
324
+ this.tui.setFocus(this.component);
325
+ this.tui.requestRender();
326
+ return { consume: true };
327
+ }
328
+
329
+ handleFocusedInput(data: string): void {
330
+ try {
331
+ this.processFocusedInput(data);
332
+ } catch (error) {
333
+ this.reportError(error);
334
+ }
335
+ }
336
+
337
+ private processFocusedInput(data: string): void {
338
+ // A release must never be mistaken for "some other key": that would hand focus back.
339
+ if (isKeyRelease(data)) return;
340
+ this.synchronizeTui();
341
+ if (!this.component?.focused || !this.tui || this.opening) return;
342
+ this.refresh();
343
+ if (this.items.length === 0) return;
344
+ if (matchesKey(data, "left")) {
345
+ this.moveSelection(-1);
346
+ } else if (matchesKey(data, "right")) {
347
+ this.moveSelection(1);
348
+ } else if (matchesKey(data, "home")) {
349
+ this.selectAt(0);
350
+ } else if (matchesKey(data, "end")) {
351
+ this.selectAt(this.items.length - 1);
352
+ } else if (matchesKey(data, "enter")) {
353
+ void this.openSelected();
354
+ } else if (matchesKey(data, "escape") || matchesKey(data, "up")) {
355
+ // Returning to the editor must consume Up instead of selecting its previous input.
356
+ this.leaveFocus();
357
+ } else if (!matchesKey(data, "down")) {
358
+ // Typing, paste and app shortcuts return to the editor without losing the input.
359
+ const editor = this.editor;
360
+ this.leaveFocus();
361
+ editor?.handleInput?.(data);
362
+ }
363
+ }
364
+
365
+ /** Wraps around; the view scrolls only as far as needed to keep the selection visible. */
366
+ private moveSelection(delta: number): void {
367
+ const count = this.items.length;
368
+ const index = this.items.findIndex((entry) => entry.id === this.selectedId);
369
+ this.selectAt((index + delta + count) % count);
370
+ }
371
+
372
+ private selectAt(index: number): void {
373
+ this.selectedId = this.items[index]?.id;
374
+ this.tui?.requestRender();
375
+ }
376
+
377
+ private leaveFocus(): void {
378
+ if (this.component?.focused && this.tui?.getFocusedComponent() === this.component) {
379
+ this.tui.setFocus(this.editor ?? null);
380
+ this.tui.requestRender();
381
+ }
382
+ }
383
+
384
+ private async openSelected(): Promise<void> {
385
+ const ctx = this.ctx;
386
+ if (!ctx || !this.tui || !this.selectedId) return;
387
+ const contribution = this.registry
388
+ .getContributions(ctx.ui)
389
+ .find((entry) => entry.id === this.selectedId);
390
+ if (!contribution) return;
391
+ this.opening = true;
392
+ const generation = this.generation;
393
+ const ui = ctx.ui;
394
+ // Open against the editor, so an overlay never records the dock as a stale preFocus.
395
+ this.leaveFocus();
396
+ try {
397
+ await contribution.provider.activate();
398
+ } catch (error) {
399
+ ui.notify(
400
+ `Lune dock failed to open ${contribution.id}: ${error instanceof Error ? error.message : String(error)}`,
401
+ "error",
402
+ );
403
+ } finally {
404
+ if (generation === this.generation && this.ctx === ctx) {
405
+ this.opening = false;
406
+ try {
407
+ this.refresh();
408
+ } catch (error) {
409
+ this.reportError(error);
410
+ return;
411
+ }
412
+ if (this.items.length > 0 && this.component && !this.tui?.hasOverlay()) {
413
+ const focused = this.tui?.getFocusedComponent();
414
+ if (focused instanceof CustomEditor) this.editor = focused;
415
+ this.tui?.setFocus(this.component);
416
+ this.tui?.requestRender();
417
+ }
418
+ }
419
+ }
420
+ }
421
+
422
+ private reportError(error: unknown): void {
423
+ this.ctx?.ui.notify(
424
+ `Lune dock failed: ${error instanceof Error ? error.message : String(error)}`,
425
+ "error",
426
+ );
427
+ }
428
+ }
@@ -0,0 +1,212 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import { visibleWidth } from "@earendil-works/pi-tui";
3
+ import type { LuneDockDisplayMode } from "@lune_99/pi-lune-extensions-core/dock";
4
+ import { DockView, type VisibleDockItem } from "./dock-renderer.ts";
5
+ import {
6
+ CELL_SEPARATOR,
7
+ renderContextCell,
8
+ renderInfoRow,
9
+ renderModelSegment,
10
+ renderStatusRow,
11
+ type DockStatus,
12
+ } from "./dock-status.ts";
13
+
14
+ export const DOCK_STYLES = ["ruled", "boxed", "compact", "minimal", "classic"] as const;
15
+ export type DockStyle = (typeof DOCK_STYLES)[number];
16
+
17
+ /** Which of the footer's statistics a style shows; classic leaves Pi's footer in charge of them. */
18
+ export interface DockLayout {
19
+ style: DockStyle;
20
+ showModel: boolean;
21
+ showWorkspace: boolean;
22
+ showUsage: boolean;
23
+ }
24
+
25
+ export interface DockFooterInput {
26
+ entries: readonly VisibleDockItem[];
27
+ /** Undefined for the classic style, which draws no statistics. */
28
+ status: DockStatus | undefined;
29
+ /** Set only while the dock has focus: an unfocused dock draws no selection and no key hints. */
30
+ selectedId: string | undefined;
31
+ mode: LuneDockDisplayMode;
32
+ layout: DockLayout;
33
+ }
34
+
35
+ /** Classic keeps Pi's own footer; every other style takes it over. */
36
+ export function replacesFooter(style: DockStyle): boolean {
37
+ return style !== "classic";
38
+ }
39
+
40
+ /** Below this width the model name would crowd out the cells, so the cells keep the whole row. */
41
+ const MIN_MODEL_ROW_WIDTH = 40;
42
+ /** A box narrower than this has no room for its border and a cell, so it degrades to compact. */
43
+ const MIN_BOX_WIDTH = 24;
44
+
45
+ /** Longest first, so a narrow row still advertises some key. */
46
+ const KEY_HINTS = ["←→ select · enter open · esc back", "enter open · esc back", "enter open"];
47
+
48
+ interface Rows {
49
+ dock: string | undefined;
50
+ info: string | undefined;
51
+ statuses: string | undefined;
52
+ }
53
+
54
+ /**
55
+ * Takes over Pi's footer: the module cells beside the model, then the working directory beside
56
+ * the usage statistics, framed according to the chosen style.
57
+ */
58
+ export class DockFooter {
59
+ /** Owned here so the cell window survives the widget component being re-created. */
60
+ private readonly cells = new DockView();
61
+
62
+ render(width: number, theme: Theme, input: DockFooterInput): string[] {
63
+ width = Math.max(0, Math.floor(width));
64
+ if (width === 0) return [];
65
+ switch (input.layout.style) {
66
+ case "classic":
67
+ return this.renderClassic(width, theme, input);
68
+ case "boxed":
69
+ return width < MIN_BOX_WIDTH
70
+ ? this.renderCompact(width, theme, input)
71
+ : this.renderBoxed(width, theme, input);
72
+ case "compact":
73
+ case "minimal":
74
+ return this.renderCompact(width, theme, input);
75
+ case "ruled":
76
+ return this.renderRuled(width, theme, input);
77
+ }
78
+ }
79
+
80
+ /** The original single row: cells with the key hint on the right, nothing else. */
81
+ private renderClassic(width: number, theme: Theme, input: DockFooterInput): string[] {
82
+ const row = this.cells.render(input.entries, width, theme, input.selectedId, input.mode)[0];
83
+ if (row === undefined) return [];
84
+ if (input.selectedId === undefined) return [row];
85
+ for (const hint of KEY_HINTS) {
86
+ const gap = width - visibleWidth(row) - visibleWidth(hint);
87
+ if (gap >= 3) return [row + " ".repeat(gap) + theme.fg("dim", hint)];
88
+ }
89
+ return [row];
90
+ }
91
+
92
+ private renderRuled(width: number, theme: Theme, input: DockFooterInput): string[] {
93
+ const rows = this.renderRows(width, theme, input, false);
94
+ const rule = theme.fg("borderMuted", "─".repeat(width));
95
+ const lines = [rule];
96
+ // The rule under the dock row carries the key hints and doubles as the closing rule.
97
+ if (rows.dock !== undefined) {
98
+ lines.push(rows.dock, input.selectedId === undefined ? rule : this.renderHintRule(width, theme));
99
+ }
100
+ if (rows.info !== undefined) lines.push(rows.info);
101
+ if (rows.statuses !== undefined) lines.push(rows.statuses);
102
+ if (rows.info !== undefined || rows.statuses !== undefined) lines.push(rule);
103
+ return lines.length === 1 ? [] : lines;
104
+ }
105
+
106
+ private renderBoxed(width: number, theme: Theme, input: DockFooterInput): string[] {
107
+ // "│ " before and " │" after leave 4 columns, 1 of which the rows already spend on their gutter.
108
+ const rows = this.renderRows(width - 3, theme, input, false);
109
+ const border = (text: string) => theme.fg("borderMuted", text);
110
+ const frame = (left: string, fill: string, right: string) => border(left) + fill + border(right);
111
+ const wrap = (row: string) =>
112
+ border("│") + row + " ".repeat(Math.max(0, width - 3 - visibleWidth(row))) + " " + border("│");
113
+ const plain = border("─".repeat(width - 2));
114
+ const hinted = input.selectedId === undefined ? plain : this.renderHintRule(width - 2, theme);
115
+
116
+ const more = rows.info !== undefined || rows.statuses !== undefined;
117
+ const lines: string[] = [];
118
+ if (rows.dock !== undefined) {
119
+ lines.push(wrap(rows.dock), frame(more ? "├" : "╰", hinted, more ? "┤" : "╯"));
120
+ }
121
+ if (rows.info !== undefined) lines.push(wrap(rows.info));
122
+ if (rows.statuses !== undefined) lines.push(wrap(rows.statuses));
123
+ if (lines.length === 0) return [];
124
+ if (rows.dock === undefined || more) lines.push(frame("╰", plain, "╯"));
125
+ return [frame("╭", plain, "╮"), ...lines];
126
+ }
127
+
128
+ /** No frame at all; with no rule to ride on, the key hint takes the model's place while focused. */
129
+ private renderCompact(width: number, theme: Theme, input: DockFooterInput): string[] {
130
+ const rows = this.renderRows(width, theme, input, true);
131
+ return [rows.dock, rows.info, rows.statuses].filter((row) => row !== undefined);
132
+ }
133
+
134
+ private renderRows(width: number, theme: Theme, input: DockFooterInput, inlineHints: boolean): Rows {
135
+ const { status, layout } = input;
136
+ const showInfo = status !== undefined && layout.style !== "minimal";
137
+ return {
138
+ dock: this.renderDockRow(width, theme, input, inlineHints),
139
+ info:
140
+ showInfo && (layout.showWorkspace || layout.showUsage)
141
+ ? renderInfoRow(status, width, theme, {
142
+ workspace: layout.showWorkspace,
143
+ usage: layout.showUsage,
144
+ })
145
+ : undefined,
146
+ statuses: status && renderStatusRow(status.extensionStatuses, width, theme),
147
+ };
148
+ }
149
+
150
+ private renderDockRow(
151
+ width: number,
152
+ theme: Theme,
153
+ input: DockFooterInput,
154
+ inlineHints: boolean,
155
+ ): string | undefined {
156
+ const hasCells = input.entries.length > 0;
157
+ const right = this.renderRightSegment(width, theme, input, inlineHints);
158
+ if (!hasCells && !right) return undefined;
159
+
160
+ const tail = (hasCells && right ? theme.fg("dim", CELL_SEPARATOR) : "") + right;
161
+ const left = hasCells
162
+ ? (this.cells.render(input.entries, width - visibleWidth(tail), theme, input.selectedId, input.mode)[0] ?? "")
163
+ : "";
164
+ return left + " ".repeat(Math.max(0, width - visibleWidth(left) - visibleWidth(tail))) + tail;
165
+ }
166
+
167
+ /** Key hint while focused (when inline), otherwise the model; the minimal style adds context use. */
168
+ private renderRightSegment(
169
+ width: number,
170
+ theme: Theme,
171
+ input: DockFooterInput,
172
+ inlineHints: boolean,
173
+ ): string {
174
+ const { status, layout } = input;
175
+ const minimal = layout.style === "minimal";
176
+ // Minimal has no info row to share the line with, so the right side may claim more of it.
177
+ const budget = Math.floor(width * (minimal ? 0.6 : 0.5)) - visibleWidth(CELL_SEPARATOR);
178
+ if (inlineHints && input.selectedId !== undefined) {
179
+ const hint = KEY_HINTS.find((candidate) => visibleWidth(candidate) <= budget);
180
+ if (hint) return theme.fg("dim", hint);
181
+ }
182
+ if (!status || width < MIN_MODEL_ROW_WIDTH) return "";
183
+ if (!minimal || !layout.showUsage) {
184
+ return layout.showModel ? renderModelSegment(status, theme, budget) : "";
185
+ }
186
+
187
+ // Context use outranks the provider prefix: drop the provider before the counter.
188
+ const context = renderContextCell(status, theme);
189
+ for (const showProvider of new Set([status.showProvider, false])) {
190
+ const model = layout.showModel
191
+ ? renderModelSegment({ ...status, showProvider }, theme, Infinity)
192
+ : "";
193
+ const combined = model ? model + theme.fg("dim", CELL_SEPARATOR) + context : context;
194
+ if (visibleWidth(combined) <= budget) return combined;
195
+ }
196
+ return layout.showModel ? renderModelSegment(status, theme, budget) : "";
197
+ }
198
+
199
+ /** The hint rides the rule under the cells, so it never takes width from a cell or the model. */
200
+ private renderHintRule(width: number, theme: Theme): string {
201
+ for (const hint of KEY_HINTS) {
202
+ const fill = width - visibleWidth(hint) - 3;
203
+ if (fill < 3) continue;
204
+ return (
205
+ theme.fg("borderMuted", "─".repeat(fill)) +
206
+ ` ${theme.fg("dim", hint)} ` +
207
+ theme.fg("borderMuted", "─")
208
+ );
209
+ }
210
+ return theme.fg("borderMuted", "─".repeat(width));
211
+ }
212
+ }