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,225 +0,0 @@
1
- import { DynamicBorder, type Theme } from "@earendil-works/pi-coding-agent";
2
- import { type Component, Container, type Input, Spacer } from "@earendil-works/pi-tui";
3
- import type { QuestionnaireState } from "../state/state.js";
4
- import type { QuestionData } from "../tool/types.js";
5
- import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
6
- import type { TabBar } from "./components/tab-bar.js";
7
- import type { StatefulView } from "./stateful-view.js";
8
- import type { TabComponents } from "./tab-components.js";
9
- import {
10
- QuestionTabStrategy,
11
- SubmitTabStrategy,
12
- type TabContentStrategy,
13
- } from "./tab-content-strategy.js";
14
-
15
- export const HINT_PART_ENTER = "Enter to select";
16
- export const HINT_PART_NAV = "↑/↓ to navigate";
17
- export const HINT_PART_TOGGLE = "Space to toggle";
18
- export const HINT_PART_NOTES = "n to add notes";
19
- export const HINT_PART_TAB = "Tab to switch questions";
20
- export const HINT_PART_CANCEL = "Esc to cancel";
21
- export const HINT_PART_COLLAPSE = "Ctrl+] to collapse";
22
- export const HINT_PART_EXPAND = "Ctrl+] to expand";
23
- /**
24
- * `HINT_SINGLE` / `HINT_MULTI` are the resting core hint for NON-multiSelect
25
- * question tabs only: `buildHintText` drops `NOTES` while the notes editor is
26
- * open (`state.notesVisible`) or the "Type something." row is capturing text
27
- * (`state.inputMode`), and on multiSelect tabs it interleaves `TOGGLE` between
28
- * `NAV` and `NOTES`, so neither composite is a substring there — assert on
29
- * `HINT_PART_*` constants in those states instead. The collapse affordance is
30
- * appended AFTER cancel by `buildHintText` so the resting core stays a contiguous
31
- * prefix substring of the rendered line. On narrow terminals the collapse tail
32
- * clips with `…` (`OneLineClippedText`); the core is preserved.
33
- */
34
- export const HINT_SINGLE = [HINT_PART_ENTER, HINT_PART_NAV, HINT_PART_NOTES, HINT_PART_CANCEL].join(
35
- " · ",
36
- );
37
- export const HINT_MULTI = [
38
- HINT_PART_ENTER,
39
- HINT_PART_NAV,
40
- HINT_PART_NOTES,
41
- HINT_PART_TAB,
42
- HINT_PART_CANCEL,
43
- ].join(" · ");
44
- /** Single-line footer shown by `QuestionnaireSession` when `state.collapsed === true`. Bypasses `buildHintText`. */
45
- export const COLLAPSED_HINT = [HINT_PART_EXPAND, HINT_PART_CANCEL].join(" · ");
46
- export const REVIEW_HEADING = "Review your answers";
47
- export const READY_PROMPT = "Ready to submit your answers?";
48
- export const INCOMPLETE_WARNING_PREFIX = "⚠ Answer remaining questions before submitting:";
49
-
50
- const OVERFLOW_UP = "↑";
51
- const OVERFLOW_DOWN = "↓";
52
- const OVERFLOW_BOTH = "↕";
53
-
54
- export type DialogState = QuestionnaireState;
55
-
56
- /** Per-tick projection of dialog state. Written by the adapter; read by the strategy thunk. */
57
- export interface DialogProps {
58
- state: DialogState;
59
- activePreviewPane: StatefulView<PreviewPaneProps>;
60
- }
61
-
62
- /** Construction-time config for `DialogView`. Frozen after construction. */
63
- export interface DialogConfig {
64
- theme: Theme;
65
- questions: readonly QuestionData[];
66
- tabBar: TabBar | undefined;
67
- notesInput: Input;
68
- isMulti: boolean;
69
- tabsByIndex: ReadonlyArray<TabComponents>;
70
- /** Optional so single-question mode and non-submit tests can omit it; SubmitTabStrategy falls back to Spacer rows. */
71
- submitPicker?: Component;
72
- /** Body height of the currently active tab/option. */
73
- getCurrentBodyHeight: (width: number) => number;
74
- /** Terminal height getter. Mirrors `getTerminalWidth` — reads `tui.terminal.rows` at render time. */
75
- getTerminalRows: () => number;
76
- }
77
-
78
- /**
79
- * The 7th renderable, promoted from a structural literal to a named class so
80
- * all view-layer components share one explicit `implements StatefulView<P>`
81
- * contract. `setProps(DialogProps)` writes the live cell read by the
82
- * strategy thunk during `render()`. `liveProps.activePreviewPane` is a
83
- * resolved pane reference threaded by the adapter per tick — the dialog
84
- * itself does not derive it.
85
- */
86
- export class DialogView implements StatefulView<DialogProps> {
87
- private liveProps: DialogProps;
88
- private readonly config: DialogConfig;
89
- private readonly questionStrategy: TabContentStrategy;
90
- private readonly submitStrategy: TabContentStrategy | undefined;
91
-
92
- constructor(config: DialogConfig, initialProps: DialogProps) {
93
- this.config = config;
94
- this.liveProps = initialProps;
95
- this.questionStrategy = new QuestionTabStrategy({
96
- theme: config.theme,
97
- questions: config.questions,
98
- getPreviewPane: () => this.liveProps.activePreviewPane,
99
- tabsByIndex: config.tabsByIndex,
100
- notesInput: config.notesInput,
101
- isMulti: config.isMulti,
102
- getCurrentBodyHeight: config.getCurrentBodyHeight,
103
- });
104
- this.submitStrategy = config.isMulti
105
- ? new SubmitTabStrategy({
106
- theme: config.theme,
107
- questions: config.questions,
108
- submitPicker: config.submitPicker,
109
- })
110
- : undefined;
111
- }
112
-
113
- setProps(props: DialogProps): void {
114
- this.liveProps = props;
115
- }
116
-
117
- handleInput(_data: string): void {}
118
-
119
- // Invalidation is driven by `QuestionnairePropsAdapter.invalidate()`, which
120
- // owns the full set of renderables (binding registries + extras like
121
- // `notesInput`). DialogView has no cached layout of its own.
122
- invalidate(): void {}
123
-
124
- render(width: number): string[] {
125
- const state = this.liveProps.state;
126
- const onSubmit = this.config.isMulti && state.currentTab === this.config.questions.length;
127
- const strategy = onSubmit && this.submitStrategy ? this.submitStrategy : this.questionStrategy;
128
-
129
- // Cache heading rows (avoid double construction in render and container build).
130
- const headingRowCache = strategy.headingRows(state);
131
- const headingCount = headingRowCache.length;
132
-
133
- // Render only the active tab's natural content; blank height equalization would
134
- // unnecessarily consume transcript rows above this full-width editor component.
135
- const natural = this.buildContainerFromStrategy(strategy, headingRowCache).render(width);
136
-
137
- // Fixed region sizes (deterministic from structure).
138
- // TabBar.render() returns [tabLine, ""] — always 2 rows.
139
- const topFixed = 1 + (this.config.isMulti && this.config.tabBar ? 2 : 0) + 1;
140
- const bottomFixed = 1 + strategy.footerRowCount;
141
- const middleRows = natural.length - topFixed - bottomFixed;
142
-
143
- const termRows = this.config.getTerminalRows();
144
- if (natural.length <= termRows) return natural;
145
-
146
- // OVERFLOW — apply 3-region partition with scroll-to-focus.
147
- const availableMiddle = Math.max(0, termRows - topFixed - bottomFixed);
148
- if (availableMiddle === 0) {
149
- // Terminal too small for any middle content — show just chrome.
150
- const chromeOnly = [
151
- ...natural.slice(0, topFixed),
152
- ...natural.slice(natural.length - bottomFixed),
153
- ];
154
- return chromeOnly.length > termRows ? chromeOnly.slice(0, termRows) : chromeOnly;
155
- }
156
-
157
- const bodyRange = strategy.focusedItemRowRange(width, state);
158
-
159
- // Compute scroll window centered on the focused option.
160
- let scrollStart: number;
161
- if (bodyRange) {
162
- const focusedRowInMiddle = headingCount + bodyRange[0];
163
- const focusedHeight = bodyRange[1] - bodyRange[0];
164
- // Center the focused item vertically in the available middle space.
165
- const idealStart =
166
- focusedRowInMiddle - Math.floor(Math.max(0, availableMiddle - focusedHeight) / 2);
167
- scrollStart = Math.max(0, Math.min(idealStart, middleRows - availableMiddle));
168
- } else {
169
- // No interactive focus (submit tab) — top-anchor the middle.
170
- scrollStart = 0;
171
- }
172
-
173
- const scrollableMiddle = natural.slice(
174
- topFixed + scrollStart,
175
- topFixed + scrollStart + availableMiddle,
176
- );
177
-
178
- const hasUp = scrollStart > 0;
179
- const hasDown = scrollStart + availableMiddle < middleRows;
180
- if (hasUp && hasDown && scrollableMiddle.length === 1) {
181
- // Single-row middle: combined ↕ avoids the prior collision where ↓ overwrote ↑.
182
- scrollableMiddle[0] = this.config.theme.fg("dim", OVERFLOW_BOTH);
183
- } else {
184
- if (hasUp && scrollableMiddle.length > 0) {
185
- scrollableMiddle[0] = this.config.theme.fg("dim", OVERFLOW_UP);
186
- }
187
- if (hasDown && scrollableMiddle.length > 0) {
188
- scrollableMiddle[scrollableMiddle.length - 1] = this.config.theme.fg("dim", OVERFLOW_DOWN);
189
- }
190
- }
191
-
192
- const result = [
193
- ...natural.slice(0, topFixed),
194
- ...scrollableMiddle,
195
- ...natural.slice(natural.length - bottomFixed),
196
- ];
197
- // Safety: never exceed terminal rows (covers the availableMiddle === 0 case
198
- // where topFixed + bottomFixed > termRows).
199
- return result.length > termRows ? result.slice(0, termRows) : result;
200
- }
201
-
202
- private buildContainerFromStrategy(
203
- strategy: TabContentStrategy,
204
- headingRowCache: Component[],
205
- ): Container {
206
- const { theme, isMulti, tabBar } = this.config;
207
- const state = this.liveProps.state;
208
- const container = new Container();
209
- const border = () => new DynamicBorder((s) => theme.fg("accent", s));
210
-
211
- container.addChild(border());
212
- if (isMulti && tabBar) container.addChild(tabBar);
213
- container.addChild(new Spacer(1));
214
-
215
- for (const c of headingRowCache) container.addChild(c);
216
- container.addChild(strategy.bodyComponent(state));
217
- container.addChild(new Spacer(1));
218
- for (const c of strategy.midRows(state)) container.addChild(c);
219
-
220
- container.addChild(border());
221
- for (const c of strategy.footerRows(state)) container.addChild(c);
222
-
223
- return container;
224
- }
225
- }
@@ -1,123 +0,0 @@
1
- import type { Input } from "@earendil-works/pi-tui";
2
- import type { BindingContext, PerTabBindingContext } from "../state/selectors/contract.js";
3
- import { selectActivePreviewPaneIndex } from "../state/selectors/derivations.js";
4
- import { selectActiveView } from "../state/selectors/focus.js";
5
- import type { QuestionnaireState } from "../state/state.js";
6
- import type { QuestionData } from "../tool/types.js";
7
- import type { BoundGlobalBinding, BoundPerTabBinding } from "./component-binding.js";
8
- import type { WrappingSelectItem } from "./components/wrapping-select.js";
9
- import type { TabComponents } from "./tab-components.js";
10
-
11
- /** Cache-invalidation contract used by the adapter. `pi-tui` `Component` already satisfies it. */
12
- interface Invalidatable {
13
- invalidate(): void;
14
- }
15
-
16
- /**
17
- * Reads pi-tui Input's private `cursor` field via type escape with full runtime validation.
18
- * Returns `undefined` on any failure → graceful degradation to end-of-buffer cursor.
19
- * Follow-up: replace with Input.getCursorOffset() when pi-tui exposes a public API.
20
- */
21
- function getInputCursorOffset(input: Input): number | undefined {
22
- const raw = (input as unknown as { cursor?: unknown }).cursor;
23
- if (typeof raw !== "number") return undefined;
24
- if (!Number.isSafeInteger(raw)) return undefined;
25
- const value = input.getValue();
26
- if (raw < 0 || raw > value.length) return undefined;
27
- return raw;
28
- }
29
-
30
- export interface QuestionnairePropsAdapterConfig {
31
- tui: { requestRender(): void };
32
- questions: readonly QuestionData[];
33
- itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
34
- tabsByIndex: ReadonlyArray<TabComponents>;
35
- inlineInput: Input;
36
- globalBindings: ReadonlyArray<BoundGlobalBinding>;
37
- perTabBindings: ReadonlyArray<BoundPerTabBinding>;
38
- /**
39
- * Renderables not reached by the binding registries (e.g. the notes
40
- * `Input`, which is typed into directly and has no props). Walked by
41
- * `invalidate()` after the binding-driven components.
42
- */
43
- extraInvalidatables?: ReadonlyArray<Invalidatable>;
44
- }
45
-
46
- /**
47
- * View fan-out: drives every component setter from the canonical state via
48
- * two binding registries. `globalBindings` covers the cross-tab components
49
- * (dialog, submitPicker?, tabBar?); `perTabBindings` covers the
50
- * per-tab kinds (optionList, preview, multiSelect?). The hand-coded fan-out
51
- * collapses to one global loop + one nested per-tab loop. The inline-Other
52
- * value is read from the headless `inlineInput` instance per tick into ctx so
53
- * `selectOptionListProps` sees the live value.
54
- */
55
- export class QuestionnairePropsAdapter {
56
- private readonly tui: QuestionnairePropsAdapterConfig["tui"];
57
- private readonly questions: readonly QuestionData[];
58
- private readonly itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
59
- private readonly tabsByIndex: ReadonlyArray<TabComponents>;
60
- private readonly inlineInput: Input;
61
- private readonly globalBindings: ReadonlyArray<BoundGlobalBinding>;
62
- private readonly perTabBindings: ReadonlyArray<BoundPerTabBinding>;
63
- private readonly extraInvalidatables: ReadonlyArray<Invalidatable>;
64
-
65
- constructor(config: QuestionnairePropsAdapterConfig) {
66
- this.tui = config.tui;
67
- this.questions = config.questions;
68
- this.itemsByTab = config.itemsByTab;
69
- this.tabsByIndex = config.tabsByIndex;
70
- this.inlineInput = config.inlineInput;
71
- this.globalBindings = config.globalBindings;
72
- this.perTabBindings = config.perTabBindings;
73
- this.extraInvalidatables = config.extraInvalidatables ?? [];
74
- }
75
-
76
- apply(state: QuestionnaireState): void {
77
- const totalQuestions = this.questions.length;
78
- const activeView = selectActiveView(state, totalQuestions);
79
- const paneIndex = selectActivePreviewPaneIndex(state.currentTab, totalQuestions);
80
- const activePreviewPane = this.tabsByIndex[paneIndex]?.preview ?? this.tabsByIndex[0]!.preview;
81
-
82
- const ctx: BindingContext = {
83
- questions: this.questions,
84
- itemsByTab: this.itemsByTab,
85
- totalQuestions,
86
- activeView,
87
- inputBuffer: this.inlineInput.getValue(),
88
- inputCursorOffset: getInputCursorOffset(this.inlineInput),
89
- activePreviewPane,
90
- };
91
-
92
- for (const binding of this.globalBindings) {
93
- binding.apply(state, ctx);
94
- }
95
-
96
- for (let i = 0; i < this.tabsByIndex.length; i++) {
97
- const tab = this.tabsByIndex[i]!;
98
- const tabCtx: PerTabBindingContext = { ...ctx, tab, i };
99
- for (const binding of this.perTabBindings) {
100
- binding.apply(state, tabCtx);
101
- }
102
- }
103
-
104
- this.tui.requestRender();
105
- }
106
-
107
- /**
108
- * Invalidates every owned renderable. Called by the session in place of
109
- * the old `dialog.invalidate()` forwarding chain — DialogView no longer
110
- * reaches into siblings (tabBar, notesInput, activePreviewPane).
111
- * Iterates the same registries used by `apply()` plus
112
- * `extraInvalidatables` for components outside the binding system.
113
- */
114
- invalidate(): void {
115
- for (const b of this.globalBindings) b.invalidate();
116
- for (const tab of this.tabsByIndex) {
117
- tab.optionList.invalidate();
118
- tab.preview.invalidate();
119
- tab.multiSelect?.invalidate();
120
- }
121
- for (const x of this.extraInvalidatables) x.invalidate();
122
- }
123
- }
@@ -1,24 +0,0 @@
1
- import type { Component } from "@earendil-works/pi-tui";
2
-
3
- /**
4
- * Generic prop-driven component contract. Every renderable owns its own `P` shape;
5
- * the adapter computes `P` from canonical state via per-component selectors and
6
- * pushes it via `setProps`. `focused: boolean` is a field on `P` only where the
7
- * component needs it.
8
- */
9
- export interface StatefulView<P> extends Component {
10
- setProps(props: P): void;
11
- }
12
-
13
- /**
14
- * Discriminated focus union — encodes the three-cell focus invariant
15
- * (`notesVisible`, submit-tab, options). Dispatcher cascade (`key-router.ts`)
16
- * and reducer's defensive clears (`state-reducer.ts`) enforce mutual exclusion;
17
- * this type makes it explicit so per-component `focused: boolean` flags
18
- * derive from one equality check against this discriminant rather than
19
- * parallel boolean reads.
20
- *
21
- * Priority order: notes > submit > options. Matches the dispatcher
22
- * cascade exactly so the union is observably equivalent to today's reads.
23
- */
24
- export type ActiveView = "notes" | "options" | "submit";
@@ -1,16 +0,0 @@
1
- import type { MultiSelectView } from "./components/multi-select-view.js";
2
- import type { OptionListViewProps } from "./components/option-list-view.js";
3
- import type { PreviewPane } from "./components/preview/preview-pane.js";
4
- import type { StatefulView } from "./stateful-view.js";
5
-
6
- export interface TabBodyHeights {
7
- current: number;
8
- max: number;
9
- }
10
-
11
- export interface TabComponents {
12
- optionList: StatefulView<OptionListViewProps>;
13
- preview: PreviewPane;
14
- multiSelect?: MultiSelectView;
15
- bodyHeights: (width: number) => TabBodyHeights;
16
- }
@@ -1,264 +0,0 @@
1
- import type { Theme } from "@earendil-works/pi-coding-agent";
2
- import {
3
- type Component,
4
- Container,
5
- type Input,
6
- Spacer,
7
- Text,
8
- truncateToWidth,
9
- } from "@earendil-works/pi-tui";
10
- import { formatAnswerScalar } from "../tool/format-answer.js";
11
- import type { QuestionData } from "../tool/types.js";
12
- import type { PreviewPane, PreviewPaneProps } from "./components/preview/preview-pane.js";
13
- import {
14
- type DialogState,
15
- HINT_PART_CANCEL,
16
- HINT_PART_COLLAPSE,
17
- HINT_PART_ENTER,
18
- HINT_PART_NAV,
19
- HINT_PART_NOTES,
20
- HINT_PART_TAB,
21
- HINT_PART_TOGGLE,
22
- INCOMPLETE_WARNING_PREFIX,
23
- READY_PROMPT,
24
- REVIEW_HEADING,
25
- } from "./dialog-builder.js";
26
- import type { StatefulView } from "./stateful-view.js";
27
- import type { TabComponents } from "./tab-components.js";
28
-
29
- const NOTES_HEADER = "Notes:";
30
-
31
- /**
32
- * Single-row, width-clipped chrome cell. The footer row count is invariant
33
- * (`QuestionTabStrategy.footerRowCount = 2`) — pi-tui's `Text` word-wraps when
34
- * the styled hint exceeds `width`, inflating that row count and desyncing the
35
- * `bodyHeight + footerRowCount` math in `DialogView.render`. Clipping with
36
- * `truncateToWidth` (ANSI-aware, matches `multi-select-view.ts` usage) keeps
37
- * the hint on one line; the collapse affordance falls off the right edge with
38
- * `…` on terminals too narrow to advertise it.
39
- */
40
- class OneLineClippedText implements Component {
41
- constructor(
42
- private readonly text: string,
43
- private readonly paddingLeft: number = 0,
44
- ) {}
45
-
46
- render(width: number): string[] {
47
- const pad = " ".repeat(this.paddingLeft);
48
- const avail = Math.max(0, width - this.paddingLeft);
49
- return [pad + truncateToWidth(this.text, avail, "…", false)];
50
- }
51
-
52
- invalidate(): void {}
53
-
54
- handleInput(_data: string): void {}
55
- }
56
-
57
- /**
58
- * Per-tab content provider. Pure functional — closes over construction-time
59
- * config; per-tick state threads through method args. The chrome wrapper
60
- * enforces height equality across tabs via `bodyHeight + footerRowCount`.
61
- */
62
- export interface TabContentStrategy {
63
- /** Total RENDERED footer rows — MUST equal what `footerRows()` actually emits. Drives residual math. */
64
- readonly footerRowCount: number;
65
-
66
- /** Variable rows above the body, after top chrome (border + tabBar + Spacer). */
67
- headingRows(state: DialogState): Component[];
68
-
69
- /** Body Component placed at the body slot. */
70
- bodyComponent(state: DialogState): Component;
71
-
72
- /** Natural rendered height of `bodyComponent(state)` at given width. */
73
- bodyHeight(width: number, state: DialogState): number;
74
-
75
- /** Optional rows between body's trailing Spacer and the bottom border. */
76
- midRows(state: DialogState): Component[];
77
-
78
- /** Footer rows below the bottom border. Rendered row count MUST equal `footerRowCount`. */
79
- footerRows(state: DialogState): Component[];
80
-
81
- /** Row range of the focused item within the body's rendered output, or undefined if no interactive focus. */
82
- focusedItemRowRange(width: number, state: DialogState): [number, number] | undefined;
83
- }
84
-
85
- export interface QuestionTabStrategyConfig {
86
- theme: Theme;
87
- questions: readonly QuestionData[];
88
- getPreviewPane: () => StatefulView<PreviewPaneProps>;
89
- tabsByIndex: ReadonlyArray<TabComponents>;
90
- notesInput: Input;
91
- isMulti: boolean;
92
- getCurrentBodyHeight: (width: number) => number;
93
- }
94
-
95
- export class QuestionTabStrategy implements TabContentStrategy {
96
- /** Spacer(1) + OneLineClippedText(hint, 1) = 2 rendered rows. */
97
- readonly footerRowCount = 2;
98
-
99
- constructor(private readonly config: QuestionTabStrategyConfig) {}
100
-
101
- headingRows(state: DialogState): Component[] {
102
- const out: Component[] = [];
103
- const question = this.config.questions[state.currentTab];
104
- // In multi-question mode the tab bar already shows the header; suppress the inline badge.
105
- if (!this.config.isMulti && question?.header && question.header.length > 0) {
106
- out.push(new Text(this.config.theme.bg("selectedBg", ` ${question.header} `), 1, 0));
107
- out.push(new Spacer(1));
108
- }
109
- if (question) {
110
- out.push(new Text(this.config.theme.bold(question.question), 1, 0));
111
- out.push(new Spacer(1));
112
- }
113
- return out;
114
- }
115
-
116
- bodyComponent(state: DialogState): Component {
117
- const question = this.config.questions[state.currentTab];
118
- const mso = this.config.tabsByIndex[state.currentTab]?.multiSelect;
119
- if (question?.multiSelect === true && mso) return mso;
120
- return this.config.getPreviewPane();
121
- }
122
-
123
- bodyHeight(width: number, _state: DialogState): number {
124
- return this.config.getCurrentBodyHeight(width);
125
- }
126
-
127
- midRows(state: DialogState): Component[] {
128
- if (!state.notesVisible) return [];
129
- return [
130
- new Text(this.config.theme.fg("muted", NOTES_HEADER), 1, 0),
131
- this.config.notesInput,
132
- new Spacer(1),
133
- ];
134
- }
135
-
136
- footerRows(state: DialogState): Component[] {
137
- const question = this.config.questions[state.currentTab];
138
- // OneLineClippedText (not pi-tui `Text`) — `buildHintText` includes the collapse
139
- // affordance, pushing the rendered string past 80 columns; `Text` would wrap and
140
- // break the strategy's `footerRowCount = 2` invariant. Clipping on narrow terminals
141
- // drops the trailing parts (collapse hint first, then cancel) with `…`.
142
- return [
143
- new Spacer(1),
144
- new OneLineClippedText(
145
- this.config.theme.fg("dim", buildHintText(question, this.config.isMulti, state)),
146
- 1,
147
- ),
148
- ];
149
- }
150
-
151
- focusedItemRowRange(width: number, state: DialogState): [number, number] | undefined {
152
- const question = this.config.questions[state.currentTab];
153
- const mso = this.config.tabsByIndex[state.currentTab]?.multiSelect;
154
- if (question?.multiSelect === true && mso) return mso.focusedItemRowRange(width);
155
- return (this.config.getPreviewPane() as unknown as PreviewPane).focusedItemRowRange(width);
156
- }
157
- }
158
-
159
- export interface SubmitTabStrategyConfig {
160
- theme: Theme;
161
- questions: readonly QuestionData[];
162
- submitPicker: Component | undefined;
163
- }
164
-
165
- export class SubmitTabStrategy implements TabContentStrategy {
166
- /** Spacer(1) + Text(prompt, 1) + Spacer(1) + submitPicker(2) = 5 rendered rows. Fallback path lands at 5 via 2 trailing Spacer(1)s. */
167
- readonly footerRowCount = 5;
168
-
169
- constructor(private readonly config: SubmitTabStrategyConfig) {}
170
-
171
- headingRows(_state: DialogState): Component[] {
172
- return [
173
- new Text(this.config.theme.bold(this.config.theme.fg("accent", REVIEW_HEADING)), 1, 0),
174
- new Spacer(1),
175
- ];
176
- }
177
-
178
- bodyComponent(state: DialogState): Component {
179
- const c = new Container();
180
- for (let i = 0; i < this.config.questions.length; i++) {
181
- const q = this.config.questions[i];
182
- const a = state.answers.get(i);
183
- if (!a) continue;
184
- const label = q.header && q.header.length > 0 ? q.header : `Q${i + 1}`;
185
- const answerText = formatAnswerScalar(a, "summary");
186
- c.addChild(new Text(this.config.theme.fg("muted", ` ● ${label}`), 1, 0));
187
- c.addChild(
188
- new Text(
189
- ` ${this.config.theme.fg("muted", "→")} ${this.config.theme.fg("text", answerText)}`,
190
- 1,
191
- 0,
192
- ),
193
- );
194
- if (a.notes && a.notes.length > 0) {
195
- c.addChild(new Text(this.config.theme.fg("dim", ` notes: ${a.notes}`), 1, 0));
196
- }
197
- }
198
- return c;
199
- }
200
-
201
- bodyHeight(width: number, state: DialogState): number {
202
- return this.bodyComponent(state).render(width).length;
203
- }
204
-
205
- midRows(_state: DialogState): Component[] {
206
- return [];
207
- }
208
-
209
- footerRows(state: DialogState): Component[] {
210
- const missing: string[] = [];
211
- for (let i = 0; i < this.config.questions.length; i++) {
212
- const q = this.config.questions[i];
213
- if (!state.answers.has(i)) {
214
- missing.push(q.header && q.header.length > 0 ? q.header : `Q${i + 1}`);
215
- }
216
- }
217
- const promptText =
218
- missing.length === 0
219
- ? this.config.theme.fg("muted", READY_PROMPT)
220
- : this.config.theme.fg("warning", `${INCOMPLETE_WARNING_PREFIX} ${missing.join(", ")}`);
221
- const out: Component[] = [new Spacer(1), new Text(promptText, 1, 0), new Spacer(1)];
222
- if (this.config.submitPicker) {
223
- out.push(this.config.submitPicker);
224
- } else {
225
- // Padding when the picker isn't wired — keeps rendered row count at footerRowCount=5.
226
- out.push(new Spacer(1));
227
- out.push(new Spacer(1));
228
- }
229
- return out;
230
- }
231
-
232
- focusedItemRowRange(_width: number, _state: DialogState): [number, number] | undefined {
233
- return undefined;
234
- }
235
- }
236
-
237
- /**
238
- * Build the controls hint line. Order:
239
- * Enter · ↑/↓ [· Space toggle] [· n notes] [· Tab switch] · Esc · Ctrl+] collapse
240
- *
241
- * `NOTES` is now part of the resting (notes-closed) core — it renders on every
242
- * question tab and drops when `state.notesVisible` flips the editor open OR while
243
- * `state.inputMode` has the "Type something." row capturing text (`n` would insert
244
- * a literal character there, so advertising it would lie). The collapse affordance
245
- * is appended last so the resting core stays a contiguous substring even when the
246
- * trailing part is clipped by `OneLineClippedText` on terminals < ~95 cols. This
247
- * is the trade we picked over wrapping (which would inflate `footerRowCount` and
248
- * desync the height math in `DialogView.render`).
249
- */
250
- export function buildHintText(
251
- question: QuestionData | undefined,
252
- isMulti: boolean,
253
- state: DialogState,
254
- ): string {
255
- const parts: string[] = [HINT_PART_ENTER, HINT_PART_NAV];
256
- if (question?.multiSelect === true) parts.push(HINT_PART_TOGGLE);
257
- if (question && !state.notesVisible && !state.inputMode) {
258
- parts.push(HINT_PART_NOTES);
259
- }
260
- if (isMulti) parts.push(HINT_PART_TAB);
261
- parts.push(HINT_PART_CANCEL);
262
- parts.push(HINT_PART_COLLAPSE);
263
- return parts.join(" · ");
264
- }