@juicesharp/rpiv-ask-user-question 1.1.5 → 1.2.1

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
@@ -63,7 +63,7 @@ ask_user_question({
63
63
  questions: [
64
64
  {
65
65
  question: string, // full question text, ends with "?"
66
- header: string, // chip label, max 12 chars
66
+ header: string, // chip label, max 16 chars
67
67
  options: [
68
68
  {
69
69
  label: string, // 1-5 words, max 60 chars
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-ask-user-question",
3
- "version": "1.1.5",
3
+ "version": "1.2.1",
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",
@@ -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
  }