@juicesharp/rpiv-ask-user-question 1.1.4 → 1.2.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/README.md CHANGED
@@ -11,20 +11,20 @@
11
11
  [![npm version](https://img.shields.io/npm/v/@juicesharp/rpiv-ask-user-question.svg)](https://www.npmjs.com/package/@juicesharp/rpiv-ask-user-question)
12
12
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
13
13
 
14
- Let the model ask you structured clarifying questions instead of guessing. `rpiv-ask-user-question` adds the `ask_user_question` tool to [Pi Agent](https://github.com/badlogic/pi-mono) — a tabbed dialog with single- and multi-select questions, side-by-side previews, per-option notes, and a Submit tab that reviews answers before they go back to the model.
14
+ Let the model ask you structured clarifying questions instead of guessing. `rpiv-ask-user-question` adds the `ask_user_question` tool to [Pi Agent](https://github.com/badlogic/pi-mono) - a tabbed dialog with single- and multi-select questions, side-by-side previews, per-option notes, and a Submit tab that reviews answers before they go back to the model.
15
15
 
16
16
  ![Side-by-side code preview](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/code-preview.jpg)
17
17
 
18
18
  ## Features
19
19
 
20
- - **Multi-question dialogs** — ask several questions in one turn with a tab bar (`Tab` to switch).
21
- - **Preview pane** — render an ASCII diagram, code snippet, or markdown next to each option, side-by-side or stacked depending on terminal width.
22
- - **Per-option notes** — press `n` on a previewed option to attach a free-text note that travels back with the answer.
23
- - **Multi-select questions** — checkboxes with `Space` to toggle, Enter-as-toggle on rows, a `Next` sentinel to advance, and toggles persisted across tab switches.
24
- - **Submit tab** — review every answer before submitting; warns about unanswered questions and offers a Submit picker.
25
- - **Chat row on every tab** — redirect the conversation without leaving the dialog.
26
- - **"Other" free-text fallback** — type a custom answer when no option fits.
27
- - **Localized UI** — sentinel rows, hints, submit/cancel labels, review pane, and notes affordance display in the user's chosen language via `@juicesharp/rpiv-i18n`. Ships Deutsch / English / Español / Français / Português (PT) / Português (BR) / Русский / Українська; switch with `/languages` or `pi --locale <code>`. LLM-facing copy (tool description, schemas, errors) stays English by design.
20
+ - **Multi-question dialogs** - ask several questions in one turn with a tab bar (`Tab` to switch).
21
+ - **Preview pane** - render an ASCII diagram, code snippet, or markdown next to each option, side-by-side or stacked depending on terminal width.
22
+ - **Per-option notes** - press `n` on a previewed option to attach a free-text note that travels back with the answer.
23
+ - **Multi-select questions** - checkboxes with `Space` to toggle, Enter-as-toggle on rows, a `Next` sentinel to advance, and toggles persisted across tab switches.
24
+ - **Submit tab** - review every answer before submitting; warns about unanswered questions and offers a Submit picker.
25
+ - **Chat row on every tab** - redirect the conversation without leaving the dialog.
26
+ - **"Other" free-text fallback** - type a custom answer when no option fits.
27
+ - **Localized UI** - sentinel rows, hints, submit/cancel labels, review pane, and notes affordance display in the user's chosen language via `@juicesharp/rpiv-i18n`. Ships Deutsch / English / Español / Français / Português (PT) / Português (BR) / Русский / Українська; switch with `/languages` or `pi --locale <code>`. LLM-facing copy (tool description, schemas, errors) stays English by design.
28
28
 
29
29
  ## Screens
30
30
 
@@ -32,7 +32,7 @@ Let the model ask you structured clarifying questions instead of guessing. `rpiv
32
32
  |---|---|
33
33
  | ![Single-question dialog](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/single-question.jpg) | ![Multi-tab + ASCII preview](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/multi-tab-preview.jpg) |
34
34
  | ![Multi-select with checkboxes](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/multi-select.jpg) | ![Submit tab review](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/submit-tab.jpg) |
35
- | ![Localized UI — German](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/localized-german-ui.jpg) | |
35
+ | ![Localized UI - German](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/localized-german-ui.jpg) | |
36
36
 
37
37
  ## Install
38
38
 
@@ -44,17 +44,64 @@ Then restart your Pi session.
44
44
 
45
45
  ### Optional: localization
46
46
 
47
- `rpiv-ask-user-question` works standalone — install only this package and you get the full English UI. Install `@juicesharp/rpiv-i18n` alongside it to flip sentinel labels, dialog hints, review-tab heading, and chat-summary lines to your active locale:
47
+ `rpiv-ask-user-question` works standalone - install only this package and you get the full English UI. Install `@juicesharp/rpiv-i18n` alongside it to flip sentinel labels, dialog hints, review-tab heading, and chat-summary lines to your active locale:
48
48
 
49
49
  ```bash
50
50
  pi install npm:@juicesharp/rpiv-i18n
51
51
  ```
52
52
 
53
- With the SDK present, locale resolves from `--locale <code>` → `~/.config/rpiv-i18n/locale.json` → `LANG` / `LC_ALL` → English. The `/languages` interactive picker and `pi --locale <code>` startup flag are also enabled. Without the SDK, the dialog stays online and renders English at every call site — no warning, no crash. Users who installed via `pi install npm:@juicesharp/rpiv-pi` + `/rpiv-setup` get the SDK automatically.
53
+ With the SDK present, locale resolves from `--locale <code>` → `~/.config/rpiv-i18n/locale.json` → `LANG` / `LC_ALL` → English. The `/languages` interactive picker and `pi --locale <code>` startup flag are also enabled. Without the SDK, the dialog stays online and renders English at every call site - no warning, no crash. Users who installed via `pi install npm:@juicesharp/rpiv-pi` + `/rpiv-setup` get the SDK automatically.
54
54
 
55
55
  ## Tool
56
56
 
57
- - **`ask_user_question`** — present one or more structured questions, each with 2+ options, optional `multiSelect`, optional per-option `preview`, and an optional free-text "Other" fallback. Returns the user's selection(s) plus any notes. See the tool's `promptGuidelines` for usage policy.
57
+ - **`ask_user_question`** - present one or more structured questions, each with 2+ options, optional `multiSelect`, optional per-option `preview`, and an optional free-text "Other" fallback. Returns the user's selection(s) plus any notes. See the tool's `promptGuidelines` for usage policy.
58
+
59
+ ### Schema
60
+
61
+ ```ts
62
+ ask_user_question({
63
+ questions: [
64
+ {
65
+ question: string, // full question text, ends with "?"
66
+ header: string, // chip label, max 16 chars
67
+ options: [
68
+ {
69
+ label: string, // 1-5 words, max 60 chars
70
+ description: string, // explains the choice / its trade-off
71
+ preview?: string, // optional markdown shown next to options
72
+ },
73
+ // … 2-4 options total
74
+ ],
75
+ multiSelect?: boolean, // default false
76
+ },
77
+ // … 1-4 questions total
78
+ ]
79
+ })
80
+ ```
81
+
82
+ Reserved option labels (rejected at validation): `"Other"`, plus the runtime sentinels (`"Type something."`, `"Chat about this"`, `"Next →"`).
83
+
84
+ Returns:
85
+
86
+ ```ts
87
+ {
88
+ content: [{ type: "text", text: string }], // human-readable envelope or DECLINE_MESSAGE
89
+ details: {
90
+ answers: Array<{
91
+ questionIndex: number,
92
+ question: string,
93
+ kind: "option" | "custom" | "chat" | "multi",
94
+ answer: string | null,
95
+ selected?: string[], // present for multi-select
96
+ notes?: string, // free-text note, when typed
97
+ preview?: string, // echoed back when option carried a preview
98
+ }>,
99
+ cancelled: boolean,
100
+ error?: "no_ui" | "no_questions" | "empty_options" | "too_many_questions"
101
+ | "duplicate_question" | "duplicate_option_label" | "reserved_label",
102
+ }
103
+ }
104
+ ```
58
105
 
59
106
  ## License
60
107
 
@@ -58,7 +58,7 @@ Preview content is rendered as markdown in a monospace box. Multi-line text with
58
58
  `Use ask_user_question whenever the user's request is underspecified and you cannot proceed without concrete decisions — you can ask up to ${MAX_QUESTIONS} questions per invocation.`,
59
59
  `Each question MUST have ${MIN_OPTIONS}-${MAX_OPTIONS} options. Every option requires a concise label (1-5 words) and a description explaining what the choice means or its trade-offs. The user can additionally type a custom answer ("Type something." row is appended automatically to single-select questions) or pick "Chat about this" to abandon the questionnaire.`,
60
60
  `Set multiSelect: true when multiple answers are valid; this suppresses the "Type something." row. Provide an options[].preview markdown string when an option benefits from richer side-by-side context (mockups, code snippets, diagrams, configs) — single-select only. NOTE: any non-empty preview on a single-select question ALSO suppresses the "Type something." row (no room in the side-by-side layout); "Chat about this" remains the escape hatch. If you recommend a specific option, make it the first option and append "(Recommended)" to its label.`,
61
- "This replaces the AskUserQuestion tool from Claude Code. Do not stack multiple ask_user_question calls back-to-back — group all clarifying questions into one invocation.",
61
+ "Do not stack multiple ask_user_question calls back-to-back — group all clarifying questions into one invocation.",
62
62
  ],
63
63
  parameters: QuestionParamsSchema,
64
64
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-ask-user-question",
3
- "version": "1.1.4",
4
- "description": "Pi extension: lets the model ask you a clarifying question with structured options instead of guessing",
3
+ "version": "1.2.0",
4
+ "description": "Pi extension. A structured questionnaire the model can put to you when it would otherwise guess, with typed options instead of free-form replies.",
5
5
  "keywords": [
6
6
  "pi-package",
7
7
  "pi-extension",
@@ -11,6 +11,7 @@ import { ChatRowView } from "../view/components/chat-row-view.js";
11
11
  import { MultiSelectView } from "../view/components/multi-select-view.js";
12
12
  import { OptionListView } from "../view/components/option-list-view.js";
13
13
  import { PreviewBlockRenderer } from "../view/components/preview/preview-block-renderer.js";
14
+ import { crossTabLeftWidthWithDonation } from "../view/components/preview/preview-layout-decider.js";
14
15
  import type { PreviewPaneProps } from "../view/components/preview/preview-pane.js";
15
16
  import { PreviewPane } from "../view/components/preview/preview-pane.js";
16
17
  import { SubmitPicker } from "../view/components/submit-picker.js";
@@ -124,6 +125,7 @@ class QuestionnaireBuilder {
124
125
 
125
126
  build(): QuestionnaireBuilt {
126
127
  const tabs = this.buildTabComponents();
128
+ this.injectGlobalLeftWidth(tabs);
127
129
  const submitPicker = this.buildSubmitPicker();
128
130
  const tabBar = this.buildTabBar();
129
131
  const heights = this.buildHeightComputers(tabs);
@@ -176,6 +178,23 @@ class QuestionnaireBuilder {
176
178
  return question.multiSelect ? multiSelectBodyHeights(multiSelect!) : previewBodyHeights(preview);
177
179
  }
178
180
 
181
+ /**
182
+ * Compute cross-tab max adaptive left width and inject into each PreviewPane.
183
+ * Mirrors buildHeightComputers pattern — iterates all tabs, takes max.
184
+ * Called after buildTabComponents, before buildDialog (so setGlobalLeftWidth
185
+ * is set before any rendering occurs).
186
+ */
187
+ private injectGlobalLeftWidth(tabs: ReadonlyArray<TabComponents>): void {
188
+ const questions = this.questions;
189
+ const itemsByTab = this.itemsByTab;
190
+ const tabsDescriptor = questions.map((q) => ({ multiSelect: q.multiSelect }));
191
+ const globalLeftWidth = (paneWidth: number): number =>
192
+ crossTabLeftWidthWithDonation(tabsDescriptor, itemsByTab, questions, paneWidth);
193
+ for (const tab of tabs) {
194
+ tab.preview.setGlobalLeftWidth(globalLeftWidth);
195
+ }
196
+ }
197
+
179
198
  private buildSubmitPicker(): SubmitPicker | undefined {
180
199
  return this.isMulti ? new SubmitPicker(this.theme) : undefined;
181
200
  }
package/tool/types.ts CHANGED
@@ -4,7 +4,7 @@ import { LABELS_BY_KIND, ROW_INTENT_META } from "../state/row-intent.js";
4
4
  export const MAX_QUESTIONS = 4;
5
5
  export const MIN_OPTIONS = 2;
6
6
  export const MAX_OPTIONS = 4;
7
- export const MAX_HEADER_LENGTH = 12;
7
+ export const MAX_HEADER_LENGTH = 16;
8
8
  export const MAX_LABEL_LENGTH = 60;
9
9
 
10
10
  /**
@@ -1,14 +1,27 @@
1
+ import { visibleWidth } from "@mariozechner/pi-tui";
2
+ import type { QuestionData } from "../../../tool/types.js";
3
+ import type { WrappingSelectItem } from "../wrapping-select.js";
4
+ import { BORDER_HORIZONTAL_OVERHEAD, BORDER_INNER_PADDING_HORIZONTAL } from "./preview-box-renderer.js";
5
+
1
6
  /** Min terminal/pane width for the side-by-side layout to engage. */
2
7
  export const PREVIEW_MIN_WIDTH = 100;
3
8
  /** Visual gap between options column and preview column in side-by-side. */
4
9
  export const PREVIEW_COLUMN_GAP = 2;
5
- /** Max width of the options column when a side-by-side preview is shown. */
6
- export const PREVIEW_LEFT_COLUMN_MAX_WIDTH = 40;
7
10
  /** 1 col padding inside the preview column (between gap and `│`). */
8
11
  export const PREVIEW_PADDING_LEFT = 1;
9
12
  /** Empty rows between options and preview blocks in stacked (narrow) layout. */
10
13
  export const STACKED_GAP_ROWS = 1;
11
14
 
15
+ /** Floor for the adaptive left column width — prevents collapse on short labels. */
16
+ export const MIN_LEFT = 30;
17
+ /** Ceiling ratio: left column never exceeds this fraction of pane width. */
18
+ export const MAX_LEFT_RATIO = 0.5;
19
+ /** Floor for the preview column width — prevents right-side collapse on narrow terminals. */
20
+ export const MIN_PREVIEW_WIDTH = 45;
21
+ /** visibleWidth(" ✔") = 2 (space + ✔ codepoint). Reserved on the longest-label measurement
22
+ * so a confirmed row never gets truncated when MIN_LEFT clamps the column. */
23
+ export const CONFIRMED_OVERHEAD = 2;
24
+
12
25
  export type PreviewLayoutMode = "side-by-side" | "stacked";
13
26
 
14
27
  /**
@@ -25,14 +38,147 @@ export function decideLayout(terminalWidth: number, paneWidth: number): PreviewL
25
38
  }
26
39
 
27
40
  /**
28
- * Width allocation for side-by-side mode:
29
- * leftWidth = min(PREVIEW_LEFT_COLUMN_MAX_WIDTH, paneWidth - gap - 1)
30
- * rightWidth = remainder after left + gap
31
- * The `Math.max(1, ...)` calls keep both columns >= 1 col on extreme inputs.
41
+ * Compute the adaptive left column width from option labels.
42
+ * Pure function — deterministic for a given (items, totalForNumbering, paneWidth).
43
+ *
44
+ * Pipeline:
45
+ * 1. Measure: longest visible label width + prefix overhead + confirmed-mark overhead
46
+ * 2. Clamp: floor MIN_LEFT, ceiling paneWidth * MAX_LEFT_RATIO
47
+ * 3. Safety net: never exceed available = paneWidth - GAP - MIN_PREVIEW_WIDTH
48
+ */
49
+ export function adaptiveLeftWidth(
50
+ items: readonly WrappingSelectItem[],
51
+ totalForNumbering: number,
52
+ paneWidth: number,
53
+ ): number {
54
+ const prefixW = String(Math.max(1, totalForNumbering)).length + 4; // digits + "❯ " + ". "
55
+ const confirmedOverhead = CONFIRMED_OVERHEAD; // visibleWidth(" ✔") = 2 (space + ✔ codepoint)
56
+ let maxLabel = 0;
57
+ for (const item of items) {
58
+ const w = visibleWidth(item.label);
59
+ if (w > maxLabel) maxLabel = w;
60
+ }
61
+ const desired = maxLabel + prefixW + confirmedOverhead;
62
+ const ratioCapped = Math.min(desired, Math.floor(paneWidth * MAX_LEFT_RATIO));
63
+ const available = paneWidth - PREVIEW_COLUMN_GAP - MIN_PREVIEW_WIDTH;
64
+ return Math.max(MIN_LEFT, Math.min(ratioCapped, Math.max(1, available)));
65
+ }
66
+
67
+ /**
68
+ * Cross-tab maximum left-column width. Aggregates `adaptiveLeftWidth` over every tab
69
+ * and returns the widest result, so the options column stays stable on tab switch.
70
+ *
71
+ * Pure function — `tabs.length` MUST equal `itemsByTab.length`. Multi-select tabs
72
+ * use `items.length` for numbering; single-select tabs add 1 for the chat row slot.
73
+ * Floor is `MIN_LEFT` so an all-empty input still produces a usable column.
74
+ */
75
+ export function crossTabMaxLeftWidth(
76
+ tabs: ReadonlyArray<{ multiSelect?: boolean }>,
77
+ itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>,
78
+ paneWidth: number,
79
+ ): number {
80
+ let max = MIN_LEFT;
81
+ for (let i = 0; i < tabs.length; i++) {
82
+ const items = itemsByTab[i] ?? [];
83
+ const totalForNumbering = tabs[i]?.multiSelect ? items.length : items.length + 1;
84
+ const tabWidth = adaptiveLeftWidth(items, totalForNumbering, paneWidth);
85
+ if (tabWidth > max) max = tabWidth;
86
+ }
87
+ return max;
88
+ }
89
+
90
+ /**
91
+ * Source-line probe: measures the widest source line across all options' previews.
92
+ * Returns 0 when no option carries a preview.
93
+ *
94
+ * V1 heuristic — works well for tree/table/heading/list content where source-line
95
+ * width ≈ rendered width. Paragraphs overstate (source width "wants" the full pane);
96
+ * the worst case is "donation does nothing useful" — fallback to the label-driven path.
97
+ * Upgrade path: replace with a render-width probe that invokes Markdown at a probe width.
98
+ *
99
+ * Pure function — O(total chars) per question; no Markdown invocation, no cache interaction.
100
+ */
101
+ export function previewSourceWidth(question: QuestionData): number {
102
+ let max = 0;
103
+ for (const option of question.options) {
104
+ const text = option.preview;
105
+ if (!text) continue;
106
+ for (const line of text.split("\n")) {
107
+ const w = visibleWidth(line);
108
+ if (w > max) max = w;
109
+ }
110
+ }
111
+ return max;
112
+ }
113
+
114
+ /**
115
+ * Cross-tab/cross-option preview budget. Iterates all questions, computes each question's
116
+ * preview appetite via `previewSourceWidth`, adds box + padding overhead (5 cols), and
117
+ * returns the widest result. Floor is `MIN_PREVIEW_WIDTH` so a previewless question still
118
+ * reserves a usable column. Ceiling ensures the left column retains its `MIN_LEFT` floor.
119
+ *
120
+ * Pure function — same cross-tab max pattern as `crossTabMaxLeftWidth`.
121
+ */
122
+ export function crossTabPreviewBudget(questions: readonly QuestionData[], paneWidth: number): number {
123
+ let max = MIN_PREVIEW_WIDTH;
124
+ for (const question of questions) {
125
+ const rawWidth = previewSourceWidth(question);
126
+ const capped = Math.min(rawWidth, paneWidth - PREVIEW_COLUMN_GAP - MIN_LEFT);
127
+ const budget = capped + BORDER_HORIZONTAL_OVERHEAD + 2 * BORDER_INNER_PADDING_HORIZONTAL + PREVIEW_PADDING_LEFT;
128
+ if (budget > max) max = budget;
129
+ }
130
+ return max;
131
+ }
132
+
133
+ /**
134
+ * Cross-tab left-column width with slack donation. Combines the label-driven width
135
+ * (from `crossTabMaxLeftWidth`) with the slack donated by narrow previews.
136
+ *
137
+ * Pipeline:
138
+ * 1. `labelDriven` = `crossTabMaxLeftWidth(tabs, itemsByTab, paneWidth)`
139
+ * 2. `previewBudget` = `crossTabPreviewBudget(questions, paneWidth)`
140
+ * 3. `slackDonation` = `paneWidth − GAP − previewBudget`
141
+ * 4. Return `min(max(labelDriven, slackDonation), ceiling)` where `ceiling = paneWidth − GAP − MIN_PREVIEW_WIDTH`
142
+ *
143
+ * Invariants:
144
+ * - Floor: result ≥ MIN_LEFT (labelDriven ≥ MIN_LEFT)
145
+ * - Preview floor: right column ≥ MIN_PREVIEW_WIDTH (ceiling enforces this)
146
+ * - Cross-tab stability: both reductions are tab-independent
147
+ * - Determinism: pure of (questions, itemsByTab, paneWidth)
148
+ *
149
+ * `crossTabMaxLeftWidth` is NOT replaced — it continues to exist as the primitive.
150
+ * `MAX_LEFT_RATIO` caps the label-driven path inside `adaptiveLeftWidth`; donation
151
+ * operates above the cap when preview slack is available.
152
+ */
153
+ export function crossTabLeftWidthWithDonation(
154
+ tabs: ReadonlyArray<{ multiSelect?: boolean }>,
155
+ itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>,
156
+ questions: readonly QuestionData[],
157
+ paneWidth: number,
158
+ ): number {
159
+ const labelDriven = crossTabMaxLeftWidth(tabs, itemsByTab, paneWidth);
160
+ // Compact-content guard: labels at MIN_LEFT fit below the floor, so widening
161
+ // the column would only inject dead space between the option list and the
162
+ // preview box. Skip donation and preserve the compact intent.
163
+ if (labelDriven <= MIN_LEFT) return labelDriven;
164
+ const previewBudget = crossTabPreviewBudget(questions, paneWidth);
165
+ const slackDonation = paneWidth - PREVIEW_COLUMN_GAP - previewBudget;
166
+ const ceiling = paneWidth - PREVIEW_COLUMN_GAP - MIN_PREVIEW_WIDTH;
167
+ return Math.min(Math.max(labelDriven, slackDonation), Math.max(1, ceiling));
168
+ }
169
+
170
+ /**
171
+ * Width allocation for side-by-side mode.
172
+ * `adaptiveLeft` is the pre-computed left column width (from `adaptiveLeftWidth`,
173
+ * cross-tab aggregated). The Math.max(1, ...) calls keep both columns >= 1 col on
174
+ * extreme inputs.
32
175
  */
33
- export function columnWidths(paneWidth: number): { leftWidth: number; rightWidth: number; gap: number } {
176
+ export function columnWidths(
177
+ paneWidth: number,
178
+ adaptiveLeft: number,
179
+ ): { leftWidth: number; rightWidth: number; gap: number } {
34
180
  const gap = PREVIEW_COLUMN_GAP;
35
- const leftWidth = Math.min(PREVIEW_LEFT_COLUMN_MAX_WIDTH, Math.max(1, paneWidth - gap - 1));
181
+ const leftWidth = Math.min(adaptiveLeft, Math.max(1, paneWidth - gap - 1));
36
182
  const rightWidth = Math.max(1, paneWidth - leftWidth - gap);
37
183
  return { leftWidth, rightWidth, gap };
38
184
  }
@@ -42,8 +188,12 @@ export function columnWidths(paneWidth: number): { leftWidth: number; rightWidth
42
188
  * `render()`. Stacked uses the full pane width for both; side-by-side splits via
43
189
  * `columnWidths`, with the preview column offset by `PREVIEW_PADDING_LEFT`.
44
190
  */
45
- export function bodyWidths(paneWidth: number, mode: PreviewLayoutMode): { optionsWidth: number; previewWidth: number } {
191
+ export function bodyWidths(
192
+ paneWidth: number,
193
+ mode: PreviewLayoutMode,
194
+ adaptiveLeft: number,
195
+ ): { optionsWidth: number; previewWidth: number } {
46
196
  if (mode === "stacked") return { optionsWidth: paneWidth, previewWidth: paneWidth };
47
- const { leftWidth, rightWidth } = columnWidths(paneWidth);
197
+ const { leftWidth, rightWidth } = columnWidths(paneWidth, adaptiveLeft);
48
198
  return { optionsWidth: leftWidth, previewWidth: Math.max(1, rightWidth - PREVIEW_PADDING_LEFT) };
49
199
  }
@@ -30,7 +30,6 @@ export {
30
30
  } from "./preview-box-renderer.js";
31
31
  export {
32
32
  PREVIEW_COLUMN_GAP,
33
- PREVIEW_LEFT_COLUMN_MAX_WIDTH,
34
33
  PREVIEW_MIN_WIDTH,
35
34
  PREVIEW_PADDING_LEFT,
36
35
  STACKED_GAP_ROWS,
@@ -76,6 +75,14 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
76
75
  private readonly optionListView: OptionListView;
77
76
  private readonly previewBlock: PreviewBlockRenderer;
78
77
  private props: PreviewPaneProps;
78
+ /**
79
+ * Cross-tab max left-width getter. Set exactly once by `buildQuestionnaire.injectGlobalLeftWidth`
80
+ * before any render. Initialized to a throwing sentinel so missing injection is a hard fail
81
+ * rather than a silent fallback to a magic constant — render is illegal until injected.
82
+ */
83
+ private globalLeftWidth: (paneWidth: number) => number = () => {
84
+ throw new Error("PreviewPane.setGlobalLeftWidth must be called before render()");
85
+ };
79
86
 
80
87
  constructor(config: PreviewPaneConfig) {
81
88
  this.question = config.question;
@@ -85,6 +92,14 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
85
92
  this.props = { notesVisible: false, selectedIndex: 0, focused: false };
86
93
  }
87
94
 
95
+ setGlobalLeftWidth(getter: (paneWidth: number) => number): void {
96
+ this.globalLeftWidth = getter;
97
+ }
98
+
99
+ private getAdaptiveLeft(paneWidth: number): number {
100
+ return this.globalLeftWidth(paneWidth);
101
+ }
102
+
88
103
  setProps(props: PreviewPaneProps): void {
89
104
  this.props = props;
90
105
  }
@@ -122,7 +137,8 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
122
137
  if (this.question.multiSelect === true) return this.optionListView.render(width).length;
123
138
  if (!this.previewBlock.hasAnyPreview()) return this.optionListView.render(width).length;
124
139
  const mode = decideLayout(this.getTerminalWidth(), width);
125
- const { optionsWidth, previewWidth } = bodyWidths(width, mode);
140
+ const adaptiveLeft = this.getAdaptiveLeft(width);
141
+ const { optionsWidth, previewWidth } = bodyWidths(width, mode, adaptiveLeft);
126
142
  const optionsHeight = this.optionListView.render(optionsWidth).length;
127
143
  const previewBlockHeight = this.previewBlock.blockHeight(previewWidth, this.props.selectedIndex, mode);
128
144
  if (mode === "side-by-side") return Math.max(optionsHeight, previewBlockHeight);
@@ -133,7 +149,8 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
133
149
  if (this.question.multiSelect === true) return this.optionListView.render(width).length;
134
150
  if (!this.previewBlock.hasAnyPreview()) return this.optionListView.render(width).length;
135
151
  const mode = decideLayout(this.getTerminalWidth(), width);
136
- const { optionsWidth, previewWidth } = bodyWidths(width, mode);
152
+ const adaptiveLeft = this.getAdaptiveLeft(width);
153
+ const { optionsWidth, previewWidth } = bodyWidths(width, mode, adaptiveLeft);
137
154
  const optionsHeight = this.optionListView.render(optionsWidth).length;
138
155
  let maxPreviewBlock = 0;
139
156
  for (let i = 0; i < this.question.options.length; i++) {
@@ -145,7 +162,8 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
145
162
  }
146
163
 
147
164
  private renderSideBySide(width: number, mode: PreviewLayoutMode): string[] {
148
- const { leftWidth, rightWidth, gap } = columnWidths(width);
165
+ const adaptiveLeft = this.getAdaptiveLeft(width);
166
+ const { leftWidth, rightWidth, gap } = columnWidths(width, adaptiveLeft);
149
167
  const leftLines = this.optionListView.render(leftWidth);
150
168
  const rightLines = this.renderPaddedPreviewLines(rightWidth, mode);
151
169
  const rows = Math.max(leftLines.length, rightLines.length);
@@ -1,6 +1,6 @@
1
1
  import type { MultiSelectViewProps } from "./components/multi-select-view.js";
2
2
  import type { OptionListViewProps } from "./components/option-list-view.js";
3
- import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
3
+ import type { PreviewPane } from "./components/preview/preview-pane.js";
4
4
  import type { StatefulView } from "./stateful-view.js";
5
5
 
6
6
  export interface TabBodyHeights {
@@ -10,7 +10,7 @@ export interface TabBodyHeights {
10
10
 
11
11
  export interface TabComponents {
12
12
  optionList: StatefulView<OptionListViewProps>;
13
- preview: StatefulView<PreviewPaneProps>;
13
+ preview: PreviewPane;
14
14
  multiSelect?: StatefulView<MultiSelectViewProps>;
15
15
  bodyHeights: (width: number) => TabBodyHeights;
16
16
  }