pi-cc-extensions 0.8.30 → 0.8.32

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.
Files changed (53) hide show
  1. package/README.en.md +164 -174
  2. package/README.md +113 -115
  3. package/extensions/claude-code-style.ts +371 -208
  4. package/extensions/compact-thinking.ts +58 -0
  5. package/extensions/fixed-editor.ts +185 -0
  6. package/extensions/index.ts +10 -4
  7. package/extensions/subagent-notification.ts +30 -23
  8. package/node_modules/@tifan/pi-fixed-editor/README.md +36 -0
  9. package/node_modules/@tifan/pi-fixed-editor/package.json +46 -0
  10. package/node_modules/@tifan/pi-fixed-editor/src/cluster.ts +113 -0
  11. package/node_modules/@tifan/pi-fixed-editor/src/index.ts +259 -0
  12. package/node_modules/@tifan/pi-fixed-editor/src/terminal-split.ts +1337 -0
  13. package/package.json +9 -5
  14. package/extensions/ask-user-question/LICENSE +0 -21
  15. package/extensions/ask-user-question/README.md +0 -63
  16. package/extensions/ask-user-question/ask-user-question.ts +0 -266
  17. package/extensions/ask-user-question/config.ts +0 -79
  18. package/extensions/ask-user-question/events.ts +0 -55
  19. package/extensions/ask-user-question/index.ts +0 -17
  20. package/extensions/ask-user-question/reconcile.ts +0 -47
  21. package/extensions/ask-user-question/rpc-fallback.ts +0 -172
  22. package/extensions/ask-user-question/state/build-questionnaire.ts +0 -314
  23. package/extensions/ask-user-question/state/dialog-height.ts +0 -16
  24. package/extensions/ask-user-question/state/key-router.ts +0 -291
  25. package/extensions/ask-user-question/state/questionnaire-session.ts +0 -193
  26. package/extensions/ask-user-question/state/row-intent.ts +0 -143
  27. package/extensions/ask-user-question/state/selectors/contract.ts +0 -24
  28. package/extensions/ask-user-question/state/selectors/derivations.ts +0 -40
  29. package/extensions/ask-user-question/state/selectors/focus.ts +0 -17
  30. package/extensions/ask-user-question/state/selectors/projections.ts +0 -102
  31. package/extensions/ask-user-question/state/state-reducer.ts +0 -299
  32. package/extensions/ask-user-question/state/state.ts +0 -49
  33. package/extensions/ask-user-question/tool/format-answer.ts +0 -29
  34. package/extensions/ask-user-question/tool/response-envelope.ts +0 -50
  35. package/extensions/ask-user-question/tool/types.ts +0 -149
  36. package/extensions/ask-user-question/tool/validate-questionnaire.ts +0 -64
  37. package/extensions/ask-user-question/view/component-binding.ts +0 -45
  38. package/extensions/ask-user-question/view/components/inline-input.ts +0 -109
  39. package/extensions/ask-user-question/view/components/multi-select-view.ts +0 -198
  40. package/extensions/ask-user-question/view/components/option-list-view.ts +0 -77
  41. package/extensions/ask-user-question/view/components/preview/markdown-content-cache.ts +0 -76
  42. package/extensions/ask-user-question/view/components/preview/preview-block-renderer.ts +0 -114
  43. package/extensions/ask-user-question/view/components/preview/preview-box-renderer.ts +0 -86
  44. package/extensions/ask-user-question/view/components/preview/preview-layout-decider.ts +0 -219
  45. package/extensions/ask-user-question/view/components/preview/preview-pane.ts +0 -230
  46. package/extensions/ask-user-question/view/components/submit-picker.ts +0 -66
  47. package/extensions/ask-user-question/view/components/tab-bar.ts +0 -57
  48. package/extensions/ask-user-question/view/components/wrapping-select.ts +0 -307
  49. package/extensions/ask-user-question/view/dialog-builder.ts +0 -225
  50. package/extensions/ask-user-question/view/props-adapter.ts +0 -123
  51. package/extensions/ask-user-question/view/stateful-view.ts +0 -24
  52. package/extensions/ask-user-question/view/tab-components.ts +0 -16
  53. package/extensions/ask-user-question/view/tab-content-strategy.ts +0 -264
@@ -1,76 +0,0 @@
1
- import type { Theme } from "@earendil-works/pi-coding-agent";
2
- import { Markdown, type MarkdownTheme, visibleWidth } from "@earendil-works/pi-tui";
3
- import type { QuestionData } from "../../../tool/types.js";
4
- import { stripFenceMarkers } from "./preview-box-renderer.js";
5
-
6
- /** CC parity in side-by-side layout. */
7
- export const MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE = 20;
8
- /** Preserves narrow-terminal protection in stacked layout. */
9
- export const MAX_PREVIEW_HEIGHT_STACKED = 15;
10
- export const NO_PREVIEW_TEXT = "No preview available";
11
- /** 1 blank separator + 1 affordance text row reserved when `hasAnyPreview` (height stability of the affordance row's offset relative to the box). */
12
- export const NOTES_AFFORDANCE_OVERHEAD = 2;
13
-
14
- /**
15
- * Per-question cache for rendered markdown previews. Width-keyed: switching the
16
- * inner width invalidates every cached `Markdown`'s render output (pi-tui's
17
- * `Markdown.render(width)` re-wraps when width changes).
18
- *
19
- * Replaces the inline `previewTexts`, `markdownCache`, `cachedWidth` triple from
20
- * the previous monolithic `preview-pane.ts`. One Markdown per option, lazy on
21
- * first request, never re-constructed — count semantics frozen by tests.
22
- */
23
- export class MarkdownContentCache {
24
- private readonly previewTexts: Map<number, string>;
25
- private readonly markdownCache: Map<number, Markdown>;
26
- private cachedWidth: number | undefined;
27
- private readonly theme: Theme;
28
- private readonly markdownTheme: MarkdownTheme;
29
-
30
- constructor(question: QuestionData, theme: Theme, markdownTheme: MarkdownTheme) {
31
- this.theme = theme;
32
- this.markdownTheme = markdownTheme;
33
- this.previewTexts = new Map();
34
- for (let i = 0; i < question.options.length; i++) {
35
- const raw = question.options[i]?.preview;
36
- if (raw && raw.length > 0) this.previewTexts.set(i, raw);
37
- }
38
- this.markdownCache = new Map();
39
- }
40
-
41
- hasAnyPreview(): boolean {
42
- return this.previewTexts.size > 0;
43
- }
44
-
45
- has(optionIndex: number): boolean {
46
- return this.previewTexts.has(optionIndex);
47
- }
48
-
49
- /**
50
- * Compute the body lines for a given option at a given inner width. Width changes
51
- * invalidate the per-Markdown render cache.
52
- */
53
- bodyFor(optionIndex: number, innerWidth: number): string[] {
54
- if (this.cachedWidth !== innerWidth) {
55
- for (const md of this.markdownCache.values()) md.invalidate();
56
- this.cachedWidth = innerWidth;
57
- }
58
- const text = this.previewTexts.get(optionIndex);
59
- if (!text) {
60
- const placeholder = this.theme.fg("dim", NO_PREVIEW_TEXT);
61
- const pad = Math.max(0, innerWidth - visibleWidth(placeholder));
62
- return [placeholder + " ".repeat(pad)];
63
- }
64
- let md = this.markdownCache.get(optionIndex);
65
- if (!md) {
66
- md = new Markdown(text, 0, 0, this.markdownTheme);
67
- this.markdownCache.set(optionIndex, md);
68
- }
69
- return stripFenceMarkers(md.render(innerWidth));
70
- }
71
-
72
- invalidate(): void {
73
- for (const md of this.markdownCache.values()) md.invalidate();
74
- this.cachedWidth = undefined;
75
- }
76
- }
@@ -1,114 +0,0 @@
1
- import type { Theme } from "@earendil-works/pi-coding-agent";
2
- import type { MarkdownTheme } from "@earendil-works/pi-tui";
3
- import type { QuestionData } from "../../../tool/types.js";
4
- import {
5
- MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE,
6
- MAX_PREVIEW_HEIGHT_STACKED,
7
- MarkdownContentCache,
8
- NOTES_AFFORDANCE_OVERHEAD,
9
- } from "./markdown-content-cache.js";
10
- import {
11
- BORDER_HORIZONTAL_OVERHEAD,
12
- BORDER_INNER_PADDING_HORIZONTAL,
13
- BORDER_VERTICAL_OVERHEAD,
14
- computeBoxDimensions,
15
- renderBorderedBox,
16
- } from "./preview-box-renderer.js";
17
- import type { PreviewLayoutMode } from "./preview-layout-decider.js";
18
-
19
- /**
20
- * Affordance text shown below the bordered preview when focused on a preview-bearing option.
21
- * Re-exported by `preview-pane.ts` for the existing test surface.
22
- */
23
- export const NOTES_AFFORDANCE_TEXT = "Notes: press n to add notes";
24
-
25
- export interface PreviewBlockRendererConfig {
26
- question: QuestionData;
27
- theme: Theme;
28
- markdownTheme: MarkdownTheme;
29
- }
30
-
31
- /**
32
- * Renders the bordered markdown preview block for a single question (one block per render call,
33
- * for the option at `optionIndex`). Owns a per-question `MarkdownContentCache`.
34
- *
35
- * NOT a `Component` — pure render-and-measure helper consumed by `PreviewPane`. The layout mode
36
- * is threaded as an explicit param (never re-derived from column width post-split).
37
- *
38
- * The affordance row is always emitted (visually empty when gated) so the preview block's row
39
- * count is height-stable across affordance-state transitions.
40
- */
41
- export class PreviewBlockRenderer {
42
- private readonly theme: Theme;
43
- private readonly cache: MarkdownContentCache;
44
-
45
- constructor(config: PreviewBlockRendererConfig) {
46
- this.theme = config.theme;
47
- this.cache = new MarkdownContentCache(config.question, config.theme, config.markdownTheme);
48
- }
49
-
50
- hasAnyPreview(): boolean {
51
- return this.cache.hasAnyPreview();
52
- }
53
-
54
- has(optionIndex: number): boolean {
55
- return this.cache.has(optionIndex);
56
- }
57
-
58
- invalidate(): void {
59
- this.cache.invalidate();
60
- }
61
-
62
- /**
63
- * Height contribution of the preview block: `BORDER_VERTICAL_OVERHEAD + contentRows +
64
- * NOTES_AFFORDANCE_OVERHEAD`. Always returns the same value as `renderBlock(...).length`
65
- * — the affordance overhead is constant, not gated by `focused`/`notesVisible`.
66
- */
67
- blockHeight(width: number, optionIndex: number, mode: PreviewLayoutMode): number {
68
- const cap =
69
- mode === "side-by-side" ? MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE : MAX_PREVIEW_HEIGHT_STACKED;
70
- const contentBudget = Math.max(1, cap - BORDER_VERTICAL_OVERHEAD - NOTES_AFFORDANCE_OVERHEAD);
71
- const innerWidth = Math.max(
72
- 1,
73
- width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL,
74
- );
75
- const rawRows = this.cache.bodyFor(optionIndex, innerWidth).length;
76
- const contentRows = Math.min(rawRows, contentBudget);
77
- return BORDER_VERTICAL_OVERHEAD + contentRows + NOTES_AFFORDANCE_OVERHEAD;
78
- }
79
-
80
- /**
81
- * Render the full preview block at `width`: bordered box + blank separator + affordance row.
82
- * `focused` and `notesVisible` together gate the affordance text (visible only when the
83
- * focused option carries a preview AND notes mode is inactive). The affordance row is ALWAYS
84
- * emitted (as an empty string when gated) so the row count is invariant.
85
- */
86
- renderBlock(
87
- width: number,
88
- optionIndex: number,
89
- mode: PreviewLayoutMode,
90
- focused: boolean,
91
- notesVisible: boolean,
92
- ): string[] {
93
- const cap =
94
- mode === "side-by-side" ? MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE : MAX_PREVIEW_HEIGHT_STACKED;
95
- const contentBudget = Math.max(1, cap - BORDER_VERTICAL_OVERHEAD - NOTES_AFFORDANCE_OVERHEAD);
96
- const maxInnerWidth = Math.max(
97
- 1,
98
- width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL,
99
- );
100
-
101
- const raw = this.cache.bodyFor(optionIndex, maxInnerWidth);
102
- const truncated = raw.length > contentBudget;
103
- const hidden = truncated ? raw.length - contentBudget : 0;
104
- const contentLines = truncated ? raw.slice(0, contentBudget) : raw;
105
-
106
- const { boxWidth } = computeBoxDimensions(contentLines, maxInnerWidth);
107
- const colorFn = (s: string) => this.theme.fg("accent", s);
108
- const boxedLines = renderBorderedBox(contentLines, boxWidth, colorFn, hidden);
109
-
110
- const showAffordance = focused && !notesVisible && this.cache.has(optionIndex);
111
- const affordance = showAffordance ? this.theme.fg("muted", NOTES_AFFORDANCE_TEXT) : "";
112
- return [...boxedLines, "", affordance];
113
- }
114
- }
@@ -1,86 +0,0 @@
1
- import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
2
-
3
- const ANSI_SGR_RE = /\x1b\[[0-9;]*m/g;
4
- const ANSI_OSC8_RE = /\x1b\]8;[^\x07\x1b]*(?:\x07|\x1b\\)/g;
5
- const FENCE_MARKER_RE = /^`{3}/;
6
-
7
- /** Top + bottom border rows consumed by `renderBorderedBox`. */
8
- export const BORDER_VERTICAL_OVERHEAD = 2;
9
- /** Left + right vertical bar columns (`│ ... │`) consumed by `renderBorderedBox`. */
10
- export const BORDER_HORIZONTAL_OVERHEAD = 2;
11
- /** Inner horizontal padding (1 col) between each border bar and content area. */
12
- export const BORDER_INNER_PADDING_HORIZONTAL = 1;
13
- /** Floor for the preview box's inner content width — CC parity (`PreviewBox.minWidth`). */
14
- export const BOX_MIN_CONTENT_WIDTH = 40;
15
-
16
- /**
17
- * Drops fenced-code-block marker lines (` ``` ` opener/closer) from rendered markdown.
18
- * pi-tui's Markdown emits literal opening ` ```lang ` and closing ` ``` ` lines around
19
- * code blocks; this strip leaves only the highlighted code body. Inline code
20
- * (`codespan`) is unaffected — pi-tui already renders it without backticks.
21
- */
22
- export function stripFenceMarkers(lines: readonly string[]): string[] {
23
- return lines.filter((line) => {
24
- const clean = line.replace(ANSI_SGR_RE, "").replace(ANSI_OSC8_RE, "");
25
- return !FENCE_MARKER_RE.test(clean);
26
- });
27
- }
28
-
29
- /**
30
- * Wraps `lines` in a 4-sided ASCII border with 1 col of inner horizontal padding.
31
- * Layout per content row: `│` + ` ` + content padded to `contentInner` + ` ` + `│`,
32
- * where `contentInner = width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL`.
33
- * Top/bottom dash runs span corner-to-corner (`width - BORDER_HORIZONTAL_OVERHEAD`). When
34
- * `hidden > 0`, the bottom-row dash run is replaced with ` ✂ ── N lines hidden ── ` (corners stay).
35
- */
36
- export function renderBorderedBox(
37
- lines: readonly string[],
38
- width: number,
39
- colorFn: (s: string) => string,
40
- hidden = 0,
41
- ): string[] {
42
- const dashSpan = Math.max(1, width - BORDER_HORIZONTAL_OVERHEAD);
43
- const contentInner = Math.max(1, dashSpan - 2 * BORDER_INNER_PADDING_HORIZONTAL);
44
- const pad = " ".repeat(BORDER_INNER_PADDING_HORIZONTAL);
45
- const top = colorFn(`┌${"─".repeat(dashSpan)}┐`);
46
- const out: string[] = [top];
47
- for (const line of lines) {
48
- const padded = truncateToWidth(line, contentInner, "", true);
49
- out.push(`${colorFn("│")}${pad}${padded}${pad}${colorFn("│")}`);
50
- }
51
- if (hidden > 0) {
52
- const indicator = ` ✂ ── ${hidden} lines hidden ── `;
53
- const space = dashSpan - indicator.length;
54
- const leftFill = "─".repeat(Math.max(0, Math.floor(space / 2)));
55
- const rightFill = "─".repeat(Math.max(0, dashSpan - leftFill.length - indicator.length));
56
- out.push(colorFn(`└${leftFill}${indicator}${rightFill}┘`));
57
- } else {
58
- out.push(colorFn(`└${"─".repeat(dashSpan)}┘`));
59
- }
60
- return out;
61
- }
62
-
63
- /**
64
- * Compute box dimensions from content lines. Pure of args.
65
- *
66
- * CC parity:
67
- * contentWidth = max(minWidth, widestRenderedLine)
68
- * boxWidth = min(contentWidth + 4, effectiveMaxWidth)
69
- *
70
- * Trailing whitespace is stripped before measuring because pi-tui's
71
- * `Markdown.render(width)` pads every line to `width`, which would otherwise force
72
- * the box to fill the whole column allocation.
73
- */
74
- export function computeBoxDimensions(
75
- contentLines: readonly string[],
76
- maxInnerWidth: number,
77
- ): { innerWidth: number; boxWidth: number } {
78
- let widest = Math.min(BOX_MIN_CONTENT_WIDTH, maxInnerWidth);
79
- for (const line of contentLines) {
80
- const w = visibleWidth(line.replace(/\s+$/, ""));
81
- if (w > widest) widest = w;
82
- }
83
- const innerWidth = Math.min(widest, maxInnerWidth);
84
- const boxWidth = innerWidth + BORDER_HORIZONTAL_OVERHEAD + 2 * BORDER_INNER_PADDING_HORIZONTAL;
85
- return { innerWidth, boxWidth };
86
- }
@@ -1,219 +0,0 @@
1
- import { visibleWidth } from "@earendil-works/pi-tui";
2
- import type { QuestionData } from "../../../tool/types.js";
3
- import type { WrappingSelectItem } from "../wrapping-select.js";
4
- import {
5
- BORDER_HORIZONTAL_OVERHEAD,
6
- BORDER_INNER_PADDING_HORIZONTAL,
7
- } from "./preview-box-renderer.js";
8
-
9
- /** Min terminal/pane width for the side-by-side layout to engage. */
10
- export const PREVIEW_MIN_WIDTH = 100;
11
- /** Visual gap between options column and preview column in side-by-side. */
12
- export const PREVIEW_COLUMN_GAP = 2;
13
- /** 1 col padding inside the preview column (between gap and `│`). */
14
- export const PREVIEW_PADDING_LEFT = 1;
15
- /** Empty rows between options and preview blocks in stacked (narrow) layout. */
16
- export const STACKED_GAP_ROWS = 1;
17
-
18
- /** Floor for the adaptive left column width — prevents collapse on short labels. */
19
- export const MIN_LEFT = 30;
20
- /** Ceiling ratio: left column never exceeds this fraction of pane width. */
21
- export const MAX_LEFT_RATIO = 0.5;
22
- /** Floor for the preview column width — prevents right-side collapse on narrow terminals. */
23
- export const MIN_PREVIEW_WIDTH = 45;
24
- /** visibleWidth(" ✔") = 2 (space + ✔ codepoint). Reserved on the longest-label measurement
25
- * so a confirmed row never gets truncated when MIN_LEFT clamps the column. */
26
- export const CONFIRMED_OVERHEAD = 2;
27
-
28
- export type PreviewLayoutMode = "side-by-side" | "stacked";
29
-
30
- /**
31
- * Decide layout mode from terminal + pane widths. Pure of inputs.
32
- *
33
- * The terminal-width gate is the AND check from the previous `preview-pane.ts` —
34
- * lifted here so the decision is computed ONCE per render and threaded explicitly
35
- * through `previewBlockHeight`. Removes the bug class where `previewBlockHeight`
36
- * re-derived `sideBySide` from a column width (already < pane width post-split),
37
- * capping height too short.
38
- */
39
- export function decideLayout(terminalWidth: number, paneWidth: number): PreviewLayoutMode {
40
- return terminalWidth >= PREVIEW_MIN_WIDTH && paneWidth >= PREVIEW_MIN_WIDTH
41
- ? "side-by-side"
42
- : "stacked";
43
- }
44
-
45
- /**
46
- * Compute the adaptive left column width from option labels.
47
- * Pure function — deterministic for a given (items, totalForNumbering, paneWidth).
48
- *
49
- * Pipeline:
50
- * 1. Measure: longest visible label width + prefix overhead + confirmed-mark overhead
51
- * 2. Clamp: floor MIN_LEFT, ceiling paneWidth * MAX_LEFT_RATIO
52
- * 3. Safety net: never exceed available = paneWidth - GAP - MIN_PREVIEW_WIDTH
53
- */
54
- export function adaptiveLeftWidth(
55
- items: readonly WrappingSelectItem[],
56
- totalForNumbering: number,
57
- paneWidth: number,
58
- ): number {
59
- const prefixW = String(Math.max(1, totalForNumbering)).length + 4; // digits + "❯ " + ". "
60
- const confirmedOverhead = CONFIRMED_OVERHEAD; // visibleWidth(" ✔") = 2 (space + ✔ codepoint)
61
- let maxLabel = 0;
62
- for (const item of items) {
63
- const w = visibleWidth(item.label);
64
- if (w > maxLabel) maxLabel = w;
65
- }
66
- const desired = maxLabel + prefixW + confirmedOverhead;
67
- const ratioCapped = Math.min(desired, Math.floor(paneWidth * MAX_LEFT_RATIO));
68
- const available = paneWidth - PREVIEW_COLUMN_GAP - MIN_PREVIEW_WIDTH;
69
- return Math.max(MIN_LEFT, Math.min(ratioCapped, Math.max(1, available)));
70
- }
71
-
72
- /**
73
- * Cross-tab maximum left-column width. Aggregates `adaptiveLeftWidth` over every tab
74
- * and returns the widest result, so the options column stays stable on tab switch.
75
- *
76
- * Pure function — `tabs.length` MUST equal `itemsByTab.length`. Numbering uses
77
- * `items.length` for every tab (the chat row slot that once added +1 on single-select
78
- * has been removed).
79
- * Floor is `MIN_LEFT` so an all-empty input still produces a usable column.
80
- */
81
- export function crossTabMaxLeftWidth(
82
- tabs: ReadonlyArray<{ multiSelect?: boolean }>,
83
- itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>,
84
- paneWidth: number,
85
- ): number {
86
- let max = MIN_LEFT;
87
- for (let i = 0; i < tabs.length; i++) {
88
- const items = itemsByTab[i] ?? [];
89
- const totalForNumbering = items.length;
90
- const tabWidth = adaptiveLeftWidth(items, totalForNumbering, paneWidth);
91
- if (tabWidth > max) max = tabWidth;
92
- }
93
- return max;
94
- }
95
-
96
- /**
97
- * Source-line probe: measures the widest source line across all options' previews.
98
- * Returns 0 when no option carries a preview.
99
- *
100
- * V1 heuristic — works well for tree/table/heading/list content where source-line
101
- * width ≈ rendered width. Paragraphs overstate (source width "wants" the full pane);
102
- * the worst case is "donation does nothing useful" — fallback to the label-driven path.
103
- * Upgrade path: replace with a render-width probe that invokes Markdown at a probe width.
104
- *
105
- * Pure function — O(total chars) per question; no Markdown invocation, no cache interaction.
106
- */
107
- /** Per-question widest preview line; questions are immutable for a dialog lifetime. */
108
- const previewSourceWidthCache = new WeakMap<QuestionData, number>();
109
-
110
- export function previewSourceWidth(question: QuestionData): number {
111
- const cached = previewSourceWidthCache.get(question);
112
- if (cached !== undefined) return cached;
113
-
114
- let max = 0;
115
- for (const option of question.options) {
116
- const text = option.preview;
117
- if (!text) continue;
118
- for (const line of text.split("\n")) {
119
- const w = visibleWidth(line);
120
- if (w > max) max = w;
121
- }
122
- }
123
- previewSourceWidthCache.set(question, max);
124
- return max;
125
- }
126
-
127
- /**
128
- * Cross-tab/cross-option preview budget. Iterates all questions, computes each question's
129
- * preview appetite via `previewSourceWidth`, adds box + padding overhead (5 cols), and
130
- * returns the widest result. Floor is `MIN_PREVIEW_WIDTH` so a previewless question still
131
- * reserves a usable column. Ceiling ensures the left column retains its `MIN_LEFT` floor.
132
- *
133
- * Pure function — same cross-tab max pattern as `crossTabMaxLeftWidth`.
134
- */
135
- export function crossTabPreviewBudget(
136
- questions: readonly QuestionData[],
137
- paneWidth: number,
138
- ): number {
139
- let max = MIN_PREVIEW_WIDTH;
140
- for (const question of questions) {
141
- const rawWidth = previewSourceWidth(question);
142
- const capped = Math.min(rawWidth, paneWidth - PREVIEW_COLUMN_GAP - MIN_LEFT);
143
- const budget =
144
- capped +
145
- BORDER_HORIZONTAL_OVERHEAD +
146
- 2 * BORDER_INNER_PADDING_HORIZONTAL +
147
- PREVIEW_PADDING_LEFT;
148
- if (budget > max) max = budget;
149
- }
150
- return max;
151
- }
152
-
153
- /**
154
- * Cross-tab left-column width with slack donation. Combines the label-driven width
155
- * (from `crossTabMaxLeftWidth`) with the slack donated by narrow previews.
156
- *
157
- * Pipeline:
158
- * 1. `labelDriven` = `crossTabMaxLeftWidth(tabs, itemsByTab, paneWidth)`
159
- * 2. `previewBudget` = `crossTabPreviewBudget(questions, paneWidth)`
160
- * 3. `slackDonation` = `paneWidth − GAP − previewBudget`
161
- * 4. Return `min(max(labelDriven, slackDonation), ceiling)` where `ceiling = paneWidth − GAP − MIN_PREVIEW_WIDTH`
162
- *
163
- * Invariants:
164
- * - Floor: result ≥ MIN_LEFT (labelDriven ≥ MIN_LEFT)
165
- * - Preview floor: right column ≥ MIN_PREVIEW_WIDTH (ceiling enforces this)
166
- * - Cross-tab stability: both reductions are tab-independent
167
- * - Determinism: pure of (questions, itemsByTab, paneWidth)
168
- *
169
- * `crossTabMaxLeftWidth` is NOT replaced — it continues to exist as the primitive.
170
- * `MAX_LEFT_RATIO` caps the label-driven path inside `adaptiveLeftWidth`; donation
171
- * operates above the cap when preview slack is available.
172
- */
173
- export function crossTabLeftWidthWithDonation(
174
- tabs: ReadonlyArray<{ multiSelect?: boolean }>,
175
- itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>,
176
- questions: readonly QuestionData[],
177
- paneWidth: number,
178
- ): number {
179
- const labelDriven = crossTabMaxLeftWidth(tabs, itemsByTab, paneWidth);
180
- // Compact-content guard: labels at MIN_LEFT fit below the floor, so widening
181
- // the column would only inject dead space between the option list and the
182
- // preview box. Skip donation and preserve the compact intent.
183
- if (labelDriven <= MIN_LEFT) return labelDriven;
184
- const previewBudget = crossTabPreviewBudget(questions, paneWidth);
185
- const slackDonation = paneWidth - PREVIEW_COLUMN_GAP - previewBudget;
186
- const ceiling = paneWidth - PREVIEW_COLUMN_GAP - MIN_PREVIEW_WIDTH;
187
- return Math.min(Math.max(labelDriven, slackDonation), Math.max(1, ceiling));
188
- }
189
-
190
- /**
191
- * Width allocation for side-by-side mode.
192
- * `adaptiveLeft` is the pre-computed left column width (from `adaptiveLeftWidth`,
193
- * cross-tab aggregated). The Math.max(1, ...) calls keep both columns >= 1 col on
194
- * extreme inputs.
195
- */
196
- export function columnWidths(
197
- paneWidth: number,
198
- adaptiveLeft: number,
199
- ): { leftWidth: number; rightWidth: number; gap: number } {
200
- const gap = PREVIEW_COLUMN_GAP;
201
- const leftWidth = Math.min(adaptiveLeft, Math.max(1, paneWidth - gap - 1));
202
- const rightWidth = Math.max(1, paneWidth - leftWidth - gap);
203
- return { leftWidth, rightWidth, gap };
204
- }
205
-
206
- /**
207
- * Returns the widths actually passed to `options.render` and `previewLines` inside
208
- * `render()`. Stacked uses the full pane width for both; side-by-side splits via
209
- * `columnWidths`, with the preview column offset by `PREVIEW_PADDING_LEFT`.
210
- */
211
- export function bodyWidths(
212
- paneWidth: number,
213
- mode: PreviewLayoutMode,
214
- adaptiveLeft: number,
215
- ): { optionsWidth: number; previewWidth: number } {
216
- if (mode === "stacked") return { optionsWidth: paneWidth, previewWidth: paneWidth };
217
- const { leftWidth, rightWidth } = columnWidths(paneWidth, adaptiveLeft);
218
- return { optionsWidth: leftWidth, previewWidth: Math.max(1, rightWidth - PREVIEW_PADDING_LEFT) };
219
- }