@juicesharp/rpiv-ask-user-question 2.13.1 → 2.13.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-ask-user-question",
3
- "version": "2.13.1",
3
+ "version": "2.13.2",
4
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",
@@ -75,6 +75,7 @@
75
75
  "view/components/wrapping-select.ts",
76
76
  "view/dialog-builder.ts",
77
77
  "view/props-adapter.ts",
78
+ "view/scroll-window.ts",
78
79
  "view/stateful-view.ts",
79
80
  "view/tab-components.ts",
80
81
  "view/tab-content-strategy.ts"
@@ -97,6 +98,6 @@
97
98
  }
98
99
  },
99
100
  "dependencies": {
100
- "@juicesharp/rpiv-config": "^2.13.1"
101
+ "@juicesharp/rpiv-config": "^2.13.2"
101
102
  }
102
103
  }
@@ -194,6 +194,7 @@ class QuestionnaireBuilder {
194
194
  });
195
195
  const preview = new PreviewPane({
196
196
  question,
197
+ theme: this.theme,
197
198
  getTerminalWidth: this.getTerminalWidth,
198
199
  optionListView: optionList,
199
200
  previewBlock,
@@ -29,6 +29,26 @@ function contentBudgetFor(mode: PreviewLayoutMode): number {
29
29
  return Math.max(1, cap - BORDER_VERTICAL_OVERHEAD - NOTES_AFFORDANCE_OVERHEAD);
30
30
  }
31
31
 
32
+ /**
33
+ * Rows a preview block uses for `rawRows` of content. Unconstrained (no `maxRows`), content is
34
+ * capped by the layout's fixed budget and the notes affordance is kept. Under a `maxRows`
35
+ * allowance that the full block would exceed, the affordance (2 rows — the footer hint already
36
+ * names the notes key) goes first, then content shrinks so the box still closes with its
37
+ * "N lines hidden" count. Undefined when not even one content row fits inside a border.
38
+ */
39
+ function fitBlock(
40
+ rawRows: number,
41
+ mode: PreviewLayoutMode,
42
+ maxRows: number | undefined,
43
+ ): { contentRows: number; affordance: boolean } | undefined {
44
+ const contentRows = Math.min(rawRows, contentBudgetFor(mode));
45
+ if (maxRows === undefined || BORDER_VERTICAL_OVERHEAD + contentRows + NOTES_AFFORDANCE_OVERHEAD <= maxRows) {
46
+ return { contentRows, affordance: true };
47
+ }
48
+ const fitted = Math.min(contentRows, maxRows - BORDER_VERTICAL_OVERHEAD);
49
+ return fitted >= 1 ? { contentRows: fitted, affordance: false } : undefined;
50
+ }
51
+
32
52
  /** Inner (padding-aware) content width for a total block width. */
33
53
  function innerWidthFor(width: number): number {
34
54
  return Math.max(1, width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL);
@@ -72,23 +92,23 @@ export class PreviewBlockRenderer {
72
92
  }
73
93
 
74
94
  /**
75
- * Height contribution of the preview block: `BORDER_VERTICAL_OVERHEAD + contentRows +
76
- * NOTES_AFFORDANCE_OVERHEAD`. Always returns the same value as `renderBlock(...).length`
77
- * — the affordance overhead is constant, not gated by `focused`/`notesVisible`.
95
+ * Height contribution of the preview block — always equal to `renderBlock(...).length`
96
+ * for the same arguments (the affordance overhead is not gated by `focused`/`notesVisible`).
97
+ * `maxRows` is the row allowance the dialog can give the block; see `fitBlock`.
78
98
  */
79
- blockHeight(width: number, optionIndex: number, mode: PreviewLayoutMode): number {
80
- const contentBudget = contentBudgetFor(mode);
81
- const innerWidth = innerWidthFor(width);
82
- const rawRows = this.cache.bodyFor(optionIndex, innerWidth).length;
83
- const contentRows = Math.min(rawRows, contentBudget);
84
- return BORDER_VERTICAL_OVERHEAD + contentRows + NOTES_AFFORDANCE_OVERHEAD;
99
+ blockHeight(width: number, optionIndex: number, mode: PreviewLayoutMode, maxRows?: number): number {
100
+ const rawRows = this.cache.bodyFor(optionIndex, innerWidthFor(width)).length;
101
+ const fit = fitBlock(rawRows, mode, maxRows);
102
+ return fit ? BORDER_VERTICAL_OVERHEAD + fit.contentRows + (fit.affordance ? NOTES_AFFORDANCE_OVERHEAD : 0) : 0;
85
103
  }
86
104
 
87
105
  /**
88
- * Render the full preview block at `width`: bordered box + blank separator + affordance row.
89
- * `focused` and `notesVisible` together gate the affordance text (visible only when the
90
- * focused option carries a preview AND notes mode is inactive). The affordance row is ALWAYS
91
- * emitted (as an empty string when gated) so the row count is invariant.
106
+ * Render the preview block at `width`: bordered box, then (when it fits) a blank separator
107
+ * and the notes-affordance row. `focused` and `notesVisible` together gate the affordance
108
+ * text (visible only when the focused option carries a preview AND notes mode is inactive);
109
+ * its row is emitted (empty when gated) whenever the block keeps it, so the row count only
110
+ * depends on the fit. Under a `maxRows` allowance the box always closes and its bottom border
111
+ * counts the lines left out; with no room for a box the block is empty.
92
112
  */
93
113
  renderBlock(
94
114
  width: number,
@@ -96,18 +116,19 @@ export class PreviewBlockRenderer {
96
116
  mode: PreviewLayoutMode,
97
117
  focused: boolean,
98
118
  notesVisible: boolean,
119
+ maxRows?: number,
99
120
  ): string[] {
100
- const contentBudget = contentBudgetFor(mode);
101
121
  const maxInnerWidth = innerWidthFor(width);
102
-
103
122
  const raw = this.cache.bodyFor(optionIndex, maxInnerWidth);
104
- const truncated = raw.length > contentBudget;
105
- const hidden = truncated ? raw.length - contentBudget : 0;
106
- const contentLines = truncated ? raw.slice(0, contentBudget) : raw;
123
+ const fit = fitBlock(raw.length, mode, maxRows);
124
+ if (!fit) return [];
125
+ const hidden = raw.length - fit.contentRows;
126
+ const contentLines = hidden > 0 ? raw.slice(0, fit.contentRows) : raw;
107
127
 
108
128
  const { boxWidth } = computeBoxDimensions(contentLines, maxInnerWidth);
109
129
  const colorFn = (s: string) => this.theme.fg("accent", s);
110
130
  const boxedLines = renderBorderedBox(contentLines, boxWidth, colorFn, hidden);
131
+ if (!fit.affordance) return boxedLines;
111
132
 
112
133
  const showAffordance = focused && !notesVisible && this.cache.has(optionIndex);
113
134
  const affordance = showAffordance
@@ -1,5 +1,7 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
1
2
  import { type Component, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
2
3
  import type { QuestionData } from "../../../tool/types.js";
4
+ import { windowLines } from "../../scroll-window.js";
3
5
  import type { StatefulView } from "../../stateful-view.js";
4
6
  import type { OptionListView } from "../option-list-view.js";
5
7
  import type { PreviewBlockRenderer } from "./preview-block-renderer.js";
@@ -12,6 +14,9 @@ import {
12
14
  STACKED_GAP_ROWS,
13
15
  } from "./preview-layout-decider.js";
14
16
 
17
+ /** Smallest preview box worth showing: top border, one content line, bottom border. */
18
+ const MIN_BOX_ROWS = 3;
19
+
15
20
  // ----- Re-exports for test imports — keep `./preview-pane.js` as the public surface -----
16
21
  export {
17
22
  MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE,
@@ -59,6 +64,7 @@ export interface PreviewPaneProps {
59
64
 
60
65
  export interface PreviewPaneConfig {
61
66
  question: QuestionData;
67
+ theme: Theme;
62
68
  getTerminalWidth: () => number;
63
69
  optionListView: OptionListView;
64
70
  previewBlock: PreviewBlockRenderer;
@@ -81,7 +87,15 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
81
87
  private readonly getTerminalWidth: () => number;
82
88
  private readonly optionListView: OptionListView;
83
89
  private readonly previewBlock: PreviewBlockRenderer;
90
+ private readonly theme: Theme;
84
91
  private props: PreviewPaneProps;
92
+ /**
93
+ * Rows the dialog can give this body this frame, set by `DialogView` before each render;
94
+ * undefined when unconstrained. A body taller than this fits itself instead of being cut
95
+ * by the dialog's scroll window: the options scroll within their column around the focused
96
+ * option, and the preview box shrinks but always closes (#210).
97
+ */
98
+ private rowAllowance: number | undefined;
85
99
  /**
86
100
  * Cross-tab max left-width getter. Set exactly once by `buildQuestionnaire.injectGlobalLeftWidth`
87
101
  * before any render. Initialized to a throwing sentinel so missing injection is a hard fail
@@ -96,6 +110,7 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
96
110
  this.getTerminalWidth = config.getTerminalWidth;
97
111
  this.optionListView = config.optionListView;
98
112
  this.previewBlock = config.previewBlock;
113
+ this.theme = config.theme;
99
114
  this.props = { notesVisible: false, selectedIndex: 0, focused: false, inputMode: false };
100
115
  }
101
116
 
@@ -111,6 +126,11 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
111
126
  this.props = props;
112
127
  }
113
128
 
129
+ /** Render-time geometry from `DialogView` (not canonical state, so not a prop). */
130
+ setRowAllowance(rows: number | undefined): void {
131
+ this.rowAllowance = rows;
132
+ }
133
+
114
134
  handleInput(_data: string): void {}
115
135
 
116
136
  invalidate(): void {
@@ -128,21 +148,98 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
128
148
  // Side-by-side + preview block resume verbatim on nav-away (inputMode clears).
129
149
  if (this.props.inputMode) return this.optionListView.render(width);
130
150
 
151
+ return this.layout(width).lines;
152
+ }
153
+
154
+ /**
155
+ * Options + preview for the current layout mode, fitted to `rowAllowance` when set. The
156
+ * returned focus range is in the same row coordinates as `lines`, so `render`,
157
+ * `focusedItemRowRange` and `naturalHeight` agree by construction.
158
+ */
159
+ private layout(width: number): { lines: string[]; focus: [number, number] | undefined } {
131
160
  const mode = decideLayout(this.getTerminalWidth(), width);
132
- if (mode === "side-by-side") return this.renderSideBySide(width, mode);
133
-
134
- // Stacked: options + blank gap + preview block.
135
- return [
136
- ...this.optionListView.render(width),
137
- ...Array(STACKED_GAP_ROWS).fill(""),
138
- ...this.previewBlock.renderBlock(
139
- width,
140
- this.props.selectedIndex,
141
- mode,
142
- this.props.focused,
143
- this.props.notesVisible,
144
- ),
145
- ];
161
+ return mode === "side-by-side" ? this.layoutSideBySide(width, mode) : this.layoutStacked(width, mode);
162
+ }
163
+
164
+ /**
165
+ * Stacked: options, a blank gap, then the preview block. When that exceeds the allowance,
166
+ * keep the focused option (plus a row either side for the overflow arrows) and give the
167
+ * preview the rest, so the preview of the option being considered stays in view instead
168
+ * of falling below the dialog's scroll window.
169
+ */
170
+ private layoutStacked(
171
+ width: number,
172
+ mode: PreviewLayoutMode,
173
+ ): { lines: string[]; focus: [number, number] | undefined } {
174
+ const { selectedIndex, focused, notesVisible } = this.props;
175
+ const options = this.optionListView.render(width);
176
+ const focus = this.optionListView.focusedItemRowRange(width);
177
+ const gap = Array<string>(STACKED_GAP_ROWS).fill("");
178
+ const allowance = this.rowAllowance;
179
+ const fullBlock = this.previewBlock.blockHeight(width, selectedIndex, mode);
180
+ if (allowance === undefined || options.length + STACKED_GAP_ROWS + fullBlock <= allowance) {
181
+ return {
182
+ lines: [
183
+ ...options,
184
+ ...gap,
185
+ ...this.previewBlock.renderBlock(width, selectedIndex, mode, focused, notesVisible),
186
+ ],
187
+ focus,
188
+ };
189
+ }
190
+ // Prefer keeping a row either side of the focused option for the overflow arrows; give
191
+ // those up only when they are what keeps the preview box (border + 1 line) from fitting.
192
+ const focusRows = focus ? focus[1] - focus[0] : 1;
193
+ const previewRowsWith = (optionsFloor: number) =>
194
+ Math.min(fullBlock, allowance - STACKED_GAP_ROWS - Math.min(options.length, optionsFloor));
195
+ const roomy = previewRowsWith(focusRows + 2);
196
+ const previewRows = roomy >= MIN_BOX_ROWS ? roomy : previewRowsWith(focusRows);
197
+ const block = this.previewBlock.renderBlock(width, selectedIndex, mode, focused, notesVisible, previewRows);
198
+ const optionRows = allowance - (block.length > 0 ? STACKED_GAP_ROWS + block.length : 0);
199
+ const windowed = windowLines(options, Math.max(1, optionRows), focus, this.theme);
200
+ return {
201
+ lines: block.length > 0 ? [...windowed.lines, ...gap, ...block] : windowed.lines,
202
+ focus: windowed.focus,
203
+ };
204
+ }
205
+
206
+ /**
207
+ * Side-by-side: options column left, preview block right. Under an allowance the options
208
+ * column scrolls around the focused option and the preview block shrinks, so neither
209
+ * column exceeds it and the dialog never slices through the preview box.
210
+ */
211
+ private layoutSideBySide(
212
+ width: number,
213
+ mode: PreviewLayoutMode,
214
+ ): { lines: string[]; focus: [number, number] | undefined } {
215
+ const adaptiveLeft = this.getAdaptiveLeft(width);
216
+ const { leftWidth, rightWidth, gap } = columnWidths(width, adaptiveLeft);
217
+ const allowance = this.rowAllowance;
218
+ const left =
219
+ allowance === undefined
220
+ ? {
221
+ lines: this.optionListView.render(leftWidth),
222
+ focus: this.optionListView.focusedItemRowRange(leftWidth),
223
+ }
224
+ : windowLines(
225
+ this.optionListView.render(leftWidth),
226
+ allowance,
227
+ this.optionListView.focusedItemRowRange(leftWidth),
228
+ this.theme,
229
+ );
230
+ const rightLines = this.renderPaddedPreviewLines(rightWidth, mode, allowance);
231
+ const rows = Math.max(left.lines.length, rightLines.length);
232
+ const gapStr = " ".repeat(gap);
233
+ const out: string[] = [];
234
+ for (let i = 0; i < rows; i++) {
235
+ const leftRaw = left.lines[i] ?? "";
236
+ const rightRaw = rightLines[i] ?? "";
237
+ const leftClamped = truncateToWidth(leftRaw, leftWidth, "");
238
+ const leftPad = " ".repeat(Math.max(0, leftWidth - visibleWidth(leftClamped)));
239
+ const joined = `${leftClamped}${leftPad}${gapStr}${rightRaw}`;
240
+ out.push(truncateToWidth(joined, width, ""));
241
+ }
242
+ return { lines: out, focus: left.focus };
146
243
  }
147
244
 
148
245
  focusedItemRowRange(width: number): [number, number] {
@@ -151,11 +248,7 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
151
248
  // `inputMode`: compute the focused row range against the FULL pane width (not the
152
249
  // side-by-side leftWidth), mirroring `render`'s full-width option list.
153
250
  if (this.props.inputMode) return this.optionListView.focusedItemRowRange(width);
154
- const mode = decideLayout(this.getTerminalWidth(), width);
155
- if (mode === "stacked") return this.optionListView.focusedItemRowRange(width);
156
- const adaptiveLeft = this.getAdaptiveLeft(width);
157
- const { leftWidth } = columnWidths(width, adaptiveLeft);
158
- return this.optionListView.focusedItemRowRange(leftWidth);
251
+ return this.layout(width).focus ?? [0, 1];
159
252
  }
160
253
 
161
254
  naturalHeight(width: number): number {
@@ -164,6 +257,7 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
164
257
  // `inputMode`: height is the full-width option list only (no preview block) — preserves
165
258
  // the `naturalHeight === render.length` parity invariant.
166
259
  if (this.props.inputMode) return this.optionListView.render(width).length;
260
+ if (this.rowAllowance !== undefined) return this.layout(width).lines.length;
167
261
  const mode = decideLayout(this.getTerminalWidth(), width);
168
262
  const adaptiveLeft = this.getAdaptiveLeft(width);
169
263
  const { optionsWidth, previewWidth } = bodyWidths(width, mode, adaptiveLeft);
@@ -188,30 +282,15 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
188
282
  const h = this.previewBlock.blockHeight(previewWidth, i, mode);
189
283
  if (h > maxPreviewBlock) maxPreviewBlock = h;
190
284
  }
191
- if (mode === "side-by-side") return Math.max(optionsHeight, maxPreviewBlock);
192
- return optionsHeight + STACKED_GAP_ROWS + maxPreviewBlock;
193
- }
194
-
195
- private renderSideBySide(width: number, mode: PreviewLayoutMode): string[] {
196
- const adaptiveLeft = this.getAdaptiveLeft(width);
197
- const { leftWidth, rightWidth, gap } = columnWidths(width, adaptiveLeft);
198
- const leftLines = this.optionListView.render(leftWidth);
199
- const rightLines = this.renderPaddedPreviewLines(rightWidth, mode);
200
- const rows = Math.max(leftLines.length, rightLines.length);
201
- const gapStr = " ".repeat(gap);
202
- const out: string[] = [];
203
- for (let i = 0; i < rows; i++) {
204
- const leftRaw = leftLines[i] ?? "";
205
- const rightRaw = rightLines[i] ?? "";
206
- const leftClamped = truncateToWidth(leftRaw, leftWidth, "");
207
- const leftPad = " ".repeat(Math.max(0, leftWidth - visibleWidth(leftClamped)));
208
- const joined = `${leftClamped}${leftPad}${gapStr}${rightRaw}`;
209
- out.push(truncateToWidth(joined, width, ""));
210
- }
211
- return out;
285
+ const max =
286
+ mode === "side-by-side"
287
+ ? Math.max(optionsHeight, maxPreviewBlock)
288
+ : optionsHeight + STACKED_GAP_ROWS + maxPreviewBlock;
289
+ // A fitted body never exceeds the allowance, whichever option is focused.
290
+ return this.rowAllowance === undefined ? max : Math.min(max, this.rowAllowance);
212
291
  }
213
292
 
214
- private renderPaddedPreviewLines(colWidth: number, mode: PreviewLayoutMode): string[] {
293
+ private renderPaddedPreviewLines(colWidth: number, mode: PreviewLayoutMode, maxRows: number | undefined): string[] {
215
294
  const inner = Math.max(1, colWidth - PREVIEW_PADDING_LEFT);
216
295
  const contentLines = this.previewBlock.renderBlock(
217
296
  inner,
@@ -219,6 +298,7 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
219
298
  mode,
220
299
  this.props.focused,
221
300
  this.props.notesVisible,
301
+ maxRows,
222
302
  );
223
303
  const boxWidth = Math.max(1, visibleWidth(contentLines[0] ?? ""));
224
304
  const boxAlignedPad = Math.max(PREVIEW_PADDING_LEFT, colWidth - boxWidth);
@@ -5,6 +5,7 @@ import type { QuestionnaireState } from "../state/state.js";
5
5
  import type { QuestionData } from "../tool/types.js";
6
6
  import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
7
7
  import type { TabBar } from "./components/tab-bar.js";
8
+ import { computeScrollStart, decorateOverflow } from "./scroll-window.js";
8
9
  import type { StatefulView } from "./stateful-view.js";
9
10
  import type { TabComponents } from "./tab-components.js";
10
11
  import { QuestionTabStrategy, SubmitTabStrategy, type TabContentStrategy } from "./tab-content-strategy.js";
@@ -64,10 +65,6 @@ export const INCOMPLETE_WARNING_PREFIX = "⚠ Answer remaining questions before
64
65
  */
65
66
  const MIN_SCROLL_ROWS_WITH_PINNED_HEADING = 3;
66
67
 
67
- const OVERFLOW_UP = "↑";
68
- const OVERFLOW_DOWN = "↓";
69
- const OVERFLOW_BOTH = "↕";
70
-
71
68
  /** No-overflow path: append the residual spacer rows after the footer. */
72
69
  function renderFitsTerminal(natural: string[], spacerRows: number): string[] {
73
70
  return spacerRows > 0 ? [...natural, ...Array<string>(spacerRows).fill("")] : natural;
@@ -79,49 +76,6 @@ function renderChromeOnly(natural: string[], topFixed: number, bottomFixed: numb
79
76
  return chromeOnly.length > termRows ? chromeOnly.slice(0, termRows) : chromeOnly;
80
77
  }
81
78
 
82
- /** Scroll window start, centered on the focused option; top-anchored when there is no interactive focus. */
83
- function computeScrollStart(
84
- bodyRange: [number, number] | undefined,
85
- rowsBeforeBody: number,
86
- availableMiddle: number,
87
- middleRows: number,
88
- ): number {
89
- if (!bodyRange) return 0;
90
- const focusedRowInMiddle = rowsBeforeBody + bodyRange[0];
91
- const focusedHeight = bodyRange[1] - bodyRange[0];
92
- // Center the focused item vertically in the available middle space.
93
- const idealStart = focusedRowInMiddle - Math.floor(Math.max(0, availableMiddle - focusedHeight) / 2);
94
- return Math.max(0, Math.min(idealStart, middleRows - availableMiddle));
95
- }
96
-
97
- /**
98
- * Mark the scroll window edges with overflow arrows; combined ↕ on a single-row middle.
99
- * An arrow replaces the row it lands on, so rows inside `protectedRows` (the focused item,
100
- * in window coordinates, end-exclusive) are never drawn over — losing the affordance beats
101
- * losing the row the user is on (#261).
102
- */
103
- function decorateOverflow(
104
- scrollableMiddle: string[],
105
- hasUp: boolean,
106
- hasDown: boolean,
107
- theme: Theme,
108
- protectedRows: readonly [number, number] | undefined,
109
- ): void {
110
- const drawable = (row: number) => !protectedRows || row < protectedRows[0] || row >= protectedRows[1];
111
- if (hasUp && hasDown && scrollableMiddle.length === 1) {
112
- // Single-row middle: combined ↕ avoids the prior collision where ↓ overwrote ↑.
113
- if (drawable(0)) scrollableMiddle[0] = theme.fg("dim", OVERFLOW_BOTH);
114
- return;
115
- }
116
- if (hasUp && scrollableMiddle.length > 0 && drawable(0)) {
117
- scrollableMiddle[0] = theme.fg("dim", OVERFLOW_UP);
118
- }
119
- const last = scrollableMiddle.length - 1;
120
- if (hasDown && last > 0 && drawable(last)) {
121
- scrollableMiddle[last] = theme.fg("dim", OVERFLOW_DOWN);
122
- }
123
- }
124
-
125
79
  export type DialogState = QuestionnaireState;
126
80
 
127
81
  /** Per-tick projection of dialog state. Written by the adapter; read by the strategy thunk. */
@@ -215,14 +169,22 @@ export class DialogView implements StatefulView<DialogProps> {
215
169
 
216
170
  // Cache heading rows (avoid double construction in render and container build).
217
171
  const headingRowCache = strategy.headingRows(state);
218
-
219
- // Build container WITHOUT residual spacer — spacer handled below based on overflow.
220
- const natural = this.buildContainerFromStrategy(strategy, headingRowCache).render(width);
172
+ // Rendered rows, not components: a long question wraps.
173
+ const headingHeight = headingRowCache.reduce((rows, c) => rows + c.render(width).length, 0);
221
174
 
222
175
  // Fixed region sizes (deterministic from structure).
223
176
  // TabBar.render() returns [tabLine, ""] — always 2 rows.
224
177
  const topFixed = 1 + (this.config.isMulti && this.config.tabBar ? 2 : 0) + 1;
225
178
  const bottomFixed = 1 + strategy.footerRowCount;
179
+ const termRows = this.config.getTerminalRows();
180
+
181
+ this.allotBodyRows(
182
+ onSubmit,
183
+ termRows - topFixed - headingHeight - bottomFixed - this.rowsAroundBody(strategy, width),
184
+ );
185
+
186
+ // Build container WITHOUT residual spacer — spacer handled below based on overflow.
187
+ const natural = this.buildContainerFromStrategy(strategy, headingRowCache).render(width);
226
188
 
227
189
  // Residual spacer: equalizes total height across tabs (only needed when no overflow).
228
190
  const spacerRows = Math.max(
@@ -233,8 +195,6 @@ export class DialogView implements StatefulView<DialogProps> {
233
195
  strategy.footerRowCount,
234
196
  );
235
197
 
236
- const termRows = this.config.getTerminalRows();
237
-
238
198
  // The spacer is padding for a stable cross-tab footprint, not content: when only the
239
199
  // padding overflows, clamp it rather than entering the scroll partition, which assumes
240
200
  // the middle region itself is longer than the window (#260).
@@ -246,7 +206,6 @@ export class DialogView implements StatefulView<DialogProps> {
246
206
  // fixed top whenever the body keeps a usable window, so the question being answered
247
207
  // never scrolls out of view; heading height is measured in rendered rows, since a
248
208
  // long question wraps.
249
- const headingHeight = headingRowCache.reduce((rows, c) => rows + c.render(width).length, 0);
250
209
  const pinHeading = termRows - topFixed - headingHeight - bottomFixed >= MIN_SCROLL_ROWS_WITH_PINNED_HEADING;
251
210
  const top = pinHeading ? topFixed + headingHeight : topFixed;
252
211
  const middleRows = natural.length - top - bottomFixed;
@@ -280,6 +239,26 @@ export class DialogView implements StatefulView<DialogProps> {
280
239
  return result.length > termRows ? result.slice(0, termRows) : result;
281
240
  }
282
241
 
242
+ /** Rows between the body and the bottom border: the body's trailing Spacer(1) plus any mid rows (e.g. the notes editor). */
243
+ private rowsAroundBody(strategy: TabContentStrategy, width: number): number {
244
+ return 1 + strategy.midRows(this.liveProps.state).reduce((rows, c) => rows + c.render(width).length, 0);
245
+ }
246
+
247
+ /**
248
+ * Hand the active question's preview pane the rows its body may use this frame, so a body
249
+ * taller than the space fits itself — options scroll in their column, the preview box
250
+ * shrinks but closes — instead of the scroll window below slicing through it (#210).
251
+ * Other tabs' panes are cleared so a stale allowance never skews the cross-tab height.
252
+ * Below `MIN_SCROLL_ROWS_WITH_PINNED_HEADING` the dialog's own scroll window takes over.
253
+ */
254
+ private allotBodyRows(onSubmit: boolean, bodyRows: number): void {
255
+ const active =
256
+ onSubmit || bodyRows < MIN_SCROLL_ROWS_WITH_PINNED_HEADING ? undefined : this.liveProps.activePreviewPane;
257
+ for (const tab of this.config.tabsByIndex) {
258
+ tab.preview?.setRowAllowance?.(tab.preview === active ? bodyRows : undefined);
259
+ }
260
+ }
261
+
283
262
  private buildContainerFromStrategy(strategy: TabContentStrategy, headingRowCache: Component[]): Container {
284
263
  const { theme, isMulti, tabBar } = this.config;
285
264
  const state = this.liveProps.state;
@@ -0,0 +1,73 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+
3
+ /**
4
+ * Scroll-window row math shared by `DialogView` (the dialog's middle region) and
5
+ * `PreviewPane` (an options column fitted to the row allowance the dialog gives it) —
6
+ * one implementation, so both windows centre, clamp and mark overflow identically.
7
+ */
8
+
9
+ const OVERFLOW_UP = "↑";
10
+ const OVERFLOW_DOWN = "↓";
11
+ const OVERFLOW_BOTH = "↕";
12
+
13
+ /** Scroll window start, centered on the focused option; top-anchored when there is no interactive focus. */
14
+ export function computeScrollStart(
15
+ bodyRange: [number, number] | undefined,
16
+ rowsBeforeBody: number,
17
+ availableMiddle: number,
18
+ middleRows: number,
19
+ ): number {
20
+ if (!bodyRange) return 0;
21
+ const focusedRowInMiddle = rowsBeforeBody + bodyRange[0];
22
+ const focusedHeight = bodyRange[1] - bodyRange[0];
23
+ // Center the focused item vertically in the available middle space.
24
+ const idealStart = focusedRowInMiddle - Math.floor(Math.max(0, availableMiddle - focusedHeight) / 2);
25
+ return Math.max(0, Math.min(idealStart, middleRows - availableMiddle));
26
+ }
27
+
28
+ /**
29
+ * Mark the scroll window edges with overflow arrows; combined ↕ on a single-row middle.
30
+ * An arrow replaces the row it lands on, so rows inside `protectedRows` (the focused item,
31
+ * in window coordinates, end-exclusive) are never drawn over — losing the affordance beats
32
+ * losing the row the user is on (#261).
33
+ */
34
+ export function decorateOverflow(
35
+ scrollableMiddle: string[],
36
+ hasUp: boolean,
37
+ hasDown: boolean,
38
+ theme: Theme,
39
+ protectedRows: readonly [number, number] | undefined,
40
+ ): void {
41
+ const drawable = (row: number) => !protectedRows || row < protectedRows[0] || row >= protectedRows[1];
42
+ if (hasUp && hasDown && scrollableMiddle.length === 1) {
43
+ // Single-row middle: combined ↕ avoids the prior collision where ↓ overwrote ↑.
44
+ if (drawable(0)) scrollableMiddle[0] = theme.fg("dim", OVERFLOW_BOTH);
45
+ return;
46
+ }
47
+ if (hasUp && scrollableMiddle.length > 0 && drawable(0)) {
48
+ scrollableMiddle[0] = theme.fg("dim", OVERFLOW_UP);
49
+ }
50
+ const last = scrollableMiddle.length - 1;
51
+ if (hasDown && last > 0 && drawable(last)) {
52
+ scrollableMiddle[last] = theme.fg("dim", OVERFLOW_DOWN);
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Window `lines` to at most `rows` rows, centred on `focus` (end-exclusive row range),
58
+ * with overflow arrows that never cover the focused rows. Returns the visible lines and
59
+ * the focus range in window coordinates. Lines that already fit are returned unchanged.
60
+ */
61
+ export function windowLines(
62
+ lines: readonly string[],
63
+ rows: number,
64
+ focus: [number, number] | undefined,
65
+ theme: Theme,
66
+ ): { lines: string[]; focus: [number, number] | undefined } {
67
+ if (lines.length <= rows) return { lines: [...lines], focus };
68
+ const start = computeScrollStart(focus, 0, rows, lines.length);
69
+ const visible = lines.slice(start, start + rows);
70
+ const inWindow: [number, number] | undefined = focus && [focus[0] - start, focus[1] - start];
71
+ decorateOverflow(visible, start > 0, start + rows < lines.length, theme, inWindow);
72
+ return { lines: visible, focus: inWindow };
73
+ }