vc-extras 0.1.0 → 0.1.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.
Files changed (249) hide show
  1. package/README.md +128 -71
  2. package/agent-install.mjs +159 -0
  3. package/agents/tester.md +23 -23
  4. package/install-extras.mjs +41 -109
  5. package/install-menu.mjs +22 -43
  6. package/install-skills.mjs +112 -112
  7. package/package.json +72 -28
  8. package/plugin-registry.mjs +73 -0
  9. package/plugins/config-ui/index.ts +159 -159
  10. package/plugins/config-ui/lib/arguments.mjs +24 -24
  11. package/plugins/config-ui/lib/documents.mjs +67 -67
  12. package/plugins/config-ui/lib/frontmatter.mjs +91 -91
  13. package/plugins/config-ui/lib/open.mjs +16 -16
  14. package/plugins/config-ui/lib/ownership.mjs +99 -99
  15. package/plugins/config-ui/lib/page.mjs +11 -11
  16. package/plugins/config-ui/lib/patch.mjs +35 -35
  17. package/plugins/config-ui/lib/paths.mjs +14 -14
  18. package/plugins/config-ui/lib/plugin-fields.mjs +86 -86
  19. package/plugins/config-ui/lib/plugin-validation.mjs +28 -28
  20. package/plugins/config-ui/lib/redaction.mjs +8 -8
  21. package/plugins/config-ui/lib/registry.mjs +282 -282
  22. package/plugins/config-ui/lib/secrets.mjs +44 -44
  23. package/plugins/config-ui/lib/server.mjs +250 -250
  24. package/plugins/config-ui/lib/sources.mjs +96 -96
  25. package/plugins/config-ui/lib/state.mjs +156 -156
  26. package/plugins/config-ui/lib/themes.mjs +113 -113
  27. package/plugins/config-ui/lib/writer.mjs +690 -690
  28. package/plugins/config-ui/project-local-owner.ts +106 -106
  29. package/plugins/config-ui/vc-extras.plugin.json +13 -0
  30. package/plugins/config-ui/web/app.mjs +997 -997
  31. package/plugins/config-ui/web/index.html +342 -342
  32. package/plugins/pi-blackhole/vc-extras.plugin.json +12 -0
  33. package/plugins/pi-btw/vc-extras.plugin.json +12 -0
  34. package/plugins/pi-claude-code/vc-extras.plugin.json +12 -0
  35. package/plugins/pi-fff/vc-extras.plugin.json +12 -0
  36. package/plugins/pi-jev-auto-mode/vc-extras.plugin.json +12 -0
  37. package/plugins/pi-mcp-adapter/vc-extras.plugin.json +12 -0
  38. package/plugins/pi-web-access/vc-extras.plugin.json +12 -0
  39. package/plugins/vc-ask/claude-hook.mjs +37 -0
  40. package/plugins/vc-ask/codex-native.mjs +28 -0
  41. package/plugins/vc-ask/host-channel.mjs +75 -0
  42. package/plugins/vc-ask/index.ts +57 -0
  43. package/plugins/vc-ask/native-questions.mjs +33 -0
  44. package/plugins/vc-ask/package.json +10 -0
  45. package/plugins/vc-ask/payload.ts +170 -0
  46. package/plugins/vc-ask/questionnaire/LICENSE.ask-user-question +21 -0
  47. package/plugins/vc-ask/questionnaire/UPSTREAM.md +104 -0
  48. package/plugins/vc-ask/questionnaire/answer-channel.ts +135 -0
  49. package/plugins/vc-ask/questionnaire/config.ts +180 -0
  50. package/plugins/vc-ask/questionnaire/events.ts +154 -0
  51. package/plugins/vc-ask/questionnaire/execute.ts +688 -0
  52. package/plugins/vc-ask/questionnaire/i18n.ts +73 -0
  53. package/plugins/vc-ask/questionnaire/inline-entry.ts +312 -0
  54. package/plugins/vc-ask/questionnaire/locales/de.json +43 -0
  55. package/plugins/vc-ask/questionnaire/locales/en.json +42 -0
  56. package/plugins/vc-ask/questionnaire/locales/es.json +43 -0
  57. package/plugins/vc-ask/questionnaire/locales/fr.json +43 -0
  58. package/plugins/vc-ask/questionnaire/locales/pt-BR.json +43 -0
  59. package/plugins/vc-ask/questionnaire/locales/pt.json +43 -0
  60. package/plugins/vc-ask/questionnaire/locales/ru.json +43 -0
  61. package/plugins/vc-ask/questionnaire/locales/uk.json +43 -0
  62. package/plugins/vc-ask/questionnaire/locales/zh.json +46 -0
  63. package/plugins/vc-ask/questionnaire/presentation.ts +11 -0
  64. package/plugins/vc-ask/questionnaire/question-preview.ts +6 -0
  65. package/plugins/vc-ask/questionnaire/rpc.ts +221 -0
  66. package/plugins/vc-ask/questionnaire/session-timeout.ts +67 -0
  67. package/plugins/vc-ask/questionnaire/state/build-questionnaire.ts +144 -0
  68. package/plugins/vc-ask/questionnaire/state/external-editor.ts +62 -0
  69. package/plugins/vc-ask/questionnaire/state/geometry.ts +131 -0
  70. package/plugins/vc-ask/questionnaire/state/key-router.ts +382 -0
  71. package/plugins/vc-ask/questionnaire/state/panel-router.ts +32 -0
  72. package/plugins/vc-ask/questionnaire/state/questionnaire-session.ts +642 -0
  73. package/plugins/vc-ask/questionnaire/state/row-intent.ts +143 -0
  74. package/plugins/vc-ask/questionnaire/state/selectors/contract.ts +24 -0
  75. package/plugins/vc-ask/questionnaire/state/selectors/derivations.ts +40 -0
  76. package/plugins/vc-ask/questionnaire/state/selectors/focus.ts +17 -0
  77. package/plugins/vc-ask/questionnaire/state/selectors/projections.ts +99 -0
  78. package/plugins/vc-ask/questionnaire/state/state-reducer.ts +455 -0
  79. package/plugins/vc-ask/questionnaire/state/state.ts +90 -0
  80. package/plugins/vc-ask/questionnaire/state/vertical.ts +158 -0
  81. package/plugins/vc-ask/questionnaire/timeout.ts +59 -0
  82. package/plugins/vc-ask/questionnaire/tool/external-answer.ts +103 -0
  83. package/plugins/vc-ask/questionnaire/tool/format-answer.ts +24 -0
  84. package/plugins/vc-ask/questionnaire/tool/normalize-params.ts +37 -0
  85. package/plugins/vc-ask/questionnaire/tool/response-envelope.ts +75 -0
  86. package/plugins/vc-ask/questionnaire/tool/types.ts +74 -0
  87. package/plugins/vc-ask/questionnaire/tool/validate-questionnaire.ts +75 -0
  88. package/plugins/vc-ask/questionnaire/view/card-theme.ts +245 -0
  89. package/plugins/vc-ask/questionnaire/view/components/inline-input.ts +66 -0
  90. package/plugins/vc-ask/questionnaire/view/components/multi-select-view.ts +177 -0
  91. package/plugins/vc-ask/questionnaire/view/components/option-list-view.ts +65 -0
  92. package/plugins/vc-ask/questionnaire/view/components/preview/markdown-content-cache.ts +101 -0
  93. package/plugins/vc-ask/questionnaire/view/components/preview/preview-block-renderer.ts +130 -0
  94. package/plugins/vc-ask/questionnaire/view/components/preview/preview-box-renderer.ts +86 -0
  95. package/plugins/vc-ask/questionnaire/view/components/preview/preview-layout-decider.ts +204 -0
  96. package/plugins/vc-ask/questionnaire/view/components/preview/preview-pane.ts +257 -0
  97. package/plugins/vc-ask/questionnaire/view/components/submit-picker.ts +65 -0
  98. package/plugins/vc-ask/questionnaire/view/components/tab-bar.ts +57 -0
  99. package/plugins/vc-ask/questionnaire/view/components/wrapping-select.ts +269 -0
  100. package/plugins/vc-ask/questionnaire/view/dialog-builder.ts +319 -0
  101. package/plugins/vc-ask/questionnaire/view/document-theme.ts +53 -0
  102. package/plugins/vc-ask/questionnaire/view/document-view.ts +306 -0
  103. package/plugins/vc-ask/questionnaire/view/panel-chrome.ts +133 -0
  104. package/plugins/vc-ask/questionnaire/view/props-adapter.ts +77 -0
  105. package/plugins/vc-ask/questionnaire/view/stateful-view.ts +24 -0
  106. package/plugins/vc-ask/questionnaire/view/tab-components.ts +16 -0
  107. package/plugins/vc-ask/questionnaire/view/tab-content-strategy.ts +280 -0
  108. package/plugins/vc-ask/questionnaire/view/vertical-view.ts +218 -0
  109. package/plugins/vc-ask/user-input-events.ts +34 -0
  110. package/plugins/vc-ask/vc-extras.plugin.json +18 -0
  111. package/plugins/vc-footer/agents/claude-code/adapter.mjs +29 -0
  112. package/plugins/vc-footer/agents/claude-code/cli.mjs +23 -0
  113. package/plugins/vc-footer/agents/claude-code/renderer.mjs +1 -0
  114. package/plugins/vc-footer/agents/claude-code/types.d.ts +38 -0
  115. package/plugins/vc-footer/agents/codex/adapter.mjs +13 -0
  116. package/plugins/vc-footer/agents/codex/mcp.mjs +45 -0
  117. package/plugins/vc-footer/agents/codex/native.mjs +19 -0
  118. package/plugins/vc-footer/agents/codex/renderer.mjs +2 -0
  119. package/plugins/vc-footer/agents/pi/adapter.mjs +23 -0
  120. package/plugins/vc-footer/agents/pi/index.js +84 -0
  121. package/plugins/vc-footer/agents/pi/project-local-owner.mjs +97 -0
  122. package/plugins/vc-footer/agents/pi/renderer.mjs +5 -0
  123. package/plugins/vc-footer/core/data.mjs +218 -0
  124. package/plugins/vc-footer/core/render.mjs +291 -0
  125. package/plugins/vc-footer/core/types.d.ts +29 -0
  126. package/plugins/vc-footer/package.json +22 -0
  127. package/plugins/vc-footer/vc-extras.plugin.json +32 -0
  128. package/skills/animate/LICENSE +21 -21
  129. package/skills/animate/RECIPES.md +324 -324
  130. package/skills/animate/SKILL.md +208 -208
  131. package/skills/animate/UPSTREAM.md +19 -19
  132. package/skills/animation-performance/LICENSE +21 -21
  133. package/skills/animation-performance/SKILL.md +223 -223
  134. package/skills/animation-performance/UPSTREAM.md +19 -19
  135. package/skills/answer-me-with-html/SKILL.md +174 -174
  136. package/skills/answer-me-with-html/scripts/am.mjs +12896 -12896
  137. package/skills/apple-design/LICENSE +21 -21
  138. package/skills/apple-design/SKILL.md +291 -291
  139. package/skills/apple-design/UPSTREAM.md +19 -19
  140. package/skills/bad-smell/SKILL.md +44 -44
  141. package/skills/commit/SKILL.md +90 -90
  142. package/skills/emil-design-eng/LICENSE +21 -21
  143. package/skills/emil-design-eng/SKILL.md +677 -677
  144. package/skills/emil-design-eng/UPSTREAM.md +19 -19
  145. package/skills/find-the-truth/SKILL.md +38 -38
  146. package/skills/find-the-truth/references/trace-method.md +48 -48
  147. package/skills/find-the-truth/references/truth-method.md +39 -39
  148. package/skills/gestures/LICENSE +21 -21
  149. package/skills/gestures/SKILL.md +107 -107
  150. package/skills/gestures/UPSTREAM.md +19 -19
  151. package/skills/humanizer-zh/LICENSE +21 -21
  152. package/skills/humanizer-zh/SKILL.md +409 -409
  153. package/skills/humanizer-zh/UPSTREAM.md +7 -7
  154. package/skills/impeccable/LICENSE +191 -191
  155. package/skills/impeccable/NOTICE.md +11 -11
  156. package/skills/impeccable/SKILL.md +89 -89
  157. package/skills/impeccable/licenses/modern-screenshot-MIT.txt +8 -8
  158. package/skills/impeccable/licenses/platform-design-skills-MIT.txt +21 -21
  159. package/skills/impeccable/reference/adapt.md +318 -318
  160. package/skills/impeccable/reference/adapt.native.md +58 -58
  161. package/skills/impeccable/reference/android.md +46 -46
  162. package/skills/impeccable/reference/animate.md +89 -89
  163. package/skills/impeccable/reference/audit.md +139 -139
  164. package/skills/impeccable/reference/audit.native.md +139 -139
  165. package/skills/impeccable/reference/bolder.md +33 -33
  166. package/skills/impeccable/reference/clarify.md +94 -94
  167. package/skills/impeccable/reference/colorize.md +86 -86
  168. package/skills/impeccable/reference/component-review.md +63 -63
  169. package/skills/impeccable/reference/craft-floor.md +44 -44
  170. package/skills/impeccable/reference/craft.md +5 -5
  171. package/skills/impeccable/reference/critique.md +819 -819
  172. package/skills/impeccable/reference/degraded/asset-producer.md +41 -41
  173. package/skills/impeccable/reference/degraded/documenter.md +23 -23
  174. package/skills/impeccable/reference/degraded/finish-reviewer.md +37 -37
  175. package/skills/impeccable/reference/degraded/manual-edit-applier.md +92 -92
  176. package/skills/impeccable/reference/delight.md +70 -70
  177. package/skills/impeccable/reference/distill.md +111 -111
  178. package/skills/impeccable/reference/doctor.md +54 -54
  179. package/skills/impeccable/reference/document.md +416 -416
  180. package/skills/impeccable/reference/extract.md +69 -69
  181. package/skills/impeccable/reference/generate.md +101 -101
  182. package/skills/impeccable/reference/harden.md +362 -362
  183. package/skills/impeccable/reference/hooks.md +114 -114
  184. package/skills/impeccable/reference/init.md +131 -131
  185. package/skills/impeccable/reference/ios.md +51 -51
  186. package/skills/impeccable/reference/layout.md +84 -84
  187. package/skills/impeccable/reference/live-setup.md +104 -104
  188. package/skills/impeccable/reference/live.md +327 -327
  189. package/skills/impeccable/reference/mode-operate.md +21 -21
  190. package/skills/impeccable/reference/mode-persuade.md +19 -19
  191. package/skills/impeccable/reference/mode-read.md +21 -21
  192. package/skills/impeccable/reference/new-work.md +152 -152
  193. package/skills/impeccable/reference/onboard.md +234 -234
  194. package/skills/impeccable/reference/operate.md +61 -61
  195. package/skills/impeccable/reference/optimize.md +257 -257
  196. package/skills/impeccable/reference/overdrive.md +127 -127
  197. package/skills/impeccable/reference/polish.md +105 -105
  198. package/skills/impeccable/reference/quieter.md +99 -99
  199. package/skills/impeccable/reference/region-map.md +26 -26
  200. package/skills/impeccable/reference/routing.md +24 -24
  201. package/skills/impeccable/reference/shape.md +59 -59
  202. package/skills/impeccable/reference/typeset.md +90 -90
  203. package/skills/impeccable/reference/visualize.md +47 -47
  204. package/skills/impeccable/scripts/VERSION +1 -1
  205. package/skills/impeccable/scripts/command-metadata.json +98 -98
  206. package/skills/impeccable/scripts/data/font-index-failures.json +120 -120
  207. package/skills/impeccable/scripts/engine-checksums.txt +9 -9
  208. package/skills/impeccable/scripts/impeccable +249 -249
  209. package/skills/impeccable/scripts/impeccable.cmd +278 -278
  210. package/skills/impeccable/scripts/live-browser-dom.js +320 -320
  211. package/skills/impeccable/scripts/live-browser-ignores.js +246 -246
  212. package/skills/impeccable/scripts/live-browser-session.js +183 -183
  213. package/skills/impeccable/scripts/live-browser.js +13862 -13862
  214. package/skills/impeccable/scripts/modern-screenshot.umd.js +14 -14
  215. package/skills/improve-animations/AUDIT.md +115 -115
  216. package/skills/improve-animations/LICENSE +21 -21
  217. package/skills/improve-animations/PLAN-TEMPLATE.md +73 -73
  218. package/skills/improve-animations/SKILL.md +110 -110
  219. package/skills/improve-animations/UPSTREAM.md +19 -19
  220. package/skills/pick-ui-library/LICENSE +21 -21
  221. package/skills/pick-ui-library/SKILL.md +85 -85
  222. package/skills/pick-ui-library/UPSTREAM.md +19 -19
  223. package/skills/review-animations/LICENSE +21 -21
  224. package/skills/review-animations/SKILL.md +120 -120
  225. package/skills/review-animations/STANDARDS.md +187 -187
  226. package/skills/review-animations/UPSTREAM.md +19 -19
  227. package/skills/test/SKILL.md +29 -29
  228. package/skills/test/references/test-method.md +60 -60
  229. package/skills/write-doc/SKILL.md +115 -115
  230. package/skills/write-doc/examples/result-report.md +53 -53
  231. package/skills/write-doc/examples/task-summary.md +45 -45
  232. package/skills/write-doc/references/terminology.md +147 -147
  233. package/specs/ask-host-protocol-v1.md +58 -0
  234. package/specs/design-demo.md +1102 -1102
  235. package/specs/frontend-immersive.md +28 -28
  236. package/specs/plugin-install-v2.md +123 -0
  237. package/specs/plugin-install.md +38 -0
  238. package/specs/web-modes/explore.md +31 -31
  239. package/specs/web-modes/risk.md +15 -15
  240. package/specs/web-modes/verify.md +16 -16
  241. package/vc-extras.mjs +46 -62
  242. package/plugins/essrj/context.ts +0 -47
  243. package/plugins/essrj/data.ts +0 -190
  244. package/plugins/essrj/index.ts +0 -139
  245. package/plugins/essrj/package.json +0 -12
  246. package/plugins/essrj/project-local-owner.ts +0 -106
  247. package/plugins/essrj/render.ts +0 -217
  248. package/plugins/essrj/types.ts +0 -40
  249. package/plugins.json +0 -38
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Card-scoped theme accents.
3
+ *
4
+ * The ask card renders inside the host terminal and inherits every color from the pi
5
+ * `Theme` instance handed to `context.ui.custom((tui, theme, …))`. Users asked for the
6
+ * card to carry its own restrained accent (iOS/Notion-like) while the rest of the app
7
+ * keeps the configured theme — so instead of editing pi theme files, we wrap the theme
8
+ * in a proxy that rewrites exactly two tokens:
9
+ *
10
+ * - `accent` (fg) — box border, pointer `❯`, option symbols, selected text
11
+ * - `selectedBg` (bg) — the selected-row highlight and the header badge
12
+ *
13
+ * Everything else (`text`, `muted`, `dim`, `warning`, …) is passed through untouched,
14
+ * so the global look is preserved. When the accent is `off`, no proxy is installed at
15
+ * all and the card renders with the raw pi theme.
16
+ *
17
+ * Light/dark is detected from the resolved `text` color rather than a theme name: a
18
+ * dark theme draws bright text, a light theme draws dark text. `getColorMode()`
19
+ * decides whether the palette hex is emitted as truecolor or quantized into the
20
+ * xterm-216 cube — the quantizer is replicated from pi's `theme.js` so a 256-color
21
+ * terminal sees byte-identical output to what pi itself would have produced.
22
+ */
23
+ import type { Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
24
+ type ThemeBg = Parameters<Theme["bg"]>[0];
25
+
26
+ export type CardAccent = "off" | "slate" | "sand";
27
+
28
+ export interface CardPaletteVariant {
29
+ /** Foreground accent hex. */
30
+ readonly accent: string;
31
+ /**
32
+ * Selected-row tint. Terminals have no alpha channel, so the translucent overlays the
33
+ * design demo used are baked down to a solid color: the accent blended ~13% over a
34
+ * typical background of the matching brightness. This reads as a subtle highlight
35
+ * rather than the heavy inverse-video block pi's default `selectedBg` gives.
36
+ */
37
+ readonly selectedBg: string;
38
+ }
39
+
40
+ export interface CardPalette {
41
+ readonly dark: CardPaletteVariant;
42
+ readonly light: CardPaletteVariant;
43
+ }
44
+
45
+ /**
46
+ * Palettes confirmed in the visual demo (`design.md`), each with a dark and a light variant.
47
+ * `selectedBg` values are the accent blended over a representative background:
48
+ * slate dark `#9aa8c7`→`#262931`, slate light `#4f6087`→`#dfe3ea`,
49
+ * sand dark `#e0a066`→`#362a1f`, sand light `#a26629`→`#eee3d6`.
50
+ */
51
+ export const CARD_PALETTES: Record<Exclude<CardAccent, "off">, CardPalette> = {
52
+ slate: {
53
+ dark: { accent: "#9aa8c7", selectedBg: "#262931" },
54
+ light: { accent: "#4f6087", selectedBg: "#dfe3ea" },
55
+ },
56
+ sand: {
57
+ dark: { accent: "#e0a066", selectedBg: "#362a1f" },
58
+ light: { accent: "#a26629", selectedBg: "#eee3d6" },
59
+ },
60
+ };
61
+
62
+ // ============================================================================
63
+ // ANSI emission — mirrors pi's theme.js so 256-color output is identical.
64
+ // ============================================================================
65
+
66
+ const CUBE_VALUES = [0, 95, 135, 175, 215, 255];
67
+ const GRAY_VALUES = Array.from({ length: 24 }, (_, i) => 8 + i * 10);
68
+
69
+ function hexToRgb(hex: string): { r: number; g: number; b: number } {
70
+ const cleaned = hex.replace("#", "");
71
+ if (cleaned.length !== 6) throw new Error(`Invalid hex color: ${hex}`);
72
+ const r = parseInt(cleaned.substring(0, 2), 16);
73
+ const g = parseInt(cleaned.substring(2, 4), 16);
74
+ const b = parseInt(cleaned.substring(4, 6), 16);
75
+ if (Number.isNaN(r) || Number.isNaN(g) || Number.isNaN(b)) throw new Error(`Invalid hex color: ${hex}`);
76
+ return { r, g, b };
77
+ }
78
+
79
+ function colorDistance(r1: number, g1: number, b1: number, r2: number, g2: number, b2: number): number {
80
+ const dr = r1 - r2;
81
+ const dg = g1 - g2;
82
+ const db = b1 - b2;
83
+ return dr * dr * 0.299 + dg * dg * 0.587 + db * db * 0.114;
84
+ }
85
+
86
+ function findClosestCubeIndex(value: number): number {
87
+ let minDist = Infinity;
88
+ let minIdx = 0;
89
+ for (let i = 0; i < CUBE_VALUES.length; i++) {
90
+ const dist = Math.abs(value - CUBE_VALUES[i]);
91
+ if (dist < minDist) {
92
+ minDist = dist;
93
+ minIdx = i;
94
+ }
95
+ }
96
+ return minIdx;
97
+ }
98
+
99
+ function findClosestGrayIndex(gray: number): number {
100
+ let minDist = Infinity;
101
+ let minIdx = 0;
102
+ for (let i = 0; i < GRAY_VALUES.length; i++) {
103
+ const dist = Math.abs(gray - GRAY_VALUES[i]);
104
+ if (dist < minDist) {
105
+ minDist = dist;
106
+ minIdx = i;
107
+ }
108
+ }
109
+ return minIdx;
110
+ }
111
+
112
+ function rgbTo256(r: number, g: number, b: number): number {
113
+ const rIdx = findClosestCubeIndex(r);
114
+ const gIdx = findClosestCubeIndex(g);
115
+ const bIdx = findClosestCubeIndex(b);
116
+ const cubeIndex = 16 + 36 * rIdx + 6 * gIdx + bIdx;
117
+ const cubeDist = colorDistance(r, g, b, CUBE_VALUES[rIdx]!, CUBE_VALUES[gIdx]!, CUBE_VALUES[bIdx]!);
118
+ const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
119
+ const grayIdx = findClosestGrayIndex(gray);
120
+ const grayDist = colorDistance(r, g, b, GRAY_VALUES[grayIdx]!, GRAY_VALUES[grayIdx]!, GRAY_VALUES[grayIdx]!);
121
+ const spread = Math.max(r, g, b) - Math.min(r, g, b);
122
+ if (spread < 10 && grayDist < cubeDist) return 232 + grayIdx;
123
+ return cubeIndex;
124
+ }
125
+
126
+ function hexTo256(hex: string): number {
127
+ const { r, g, b } = hexToRgb(hex);
128
+ return rgbTo256(r, g, b);
129
+ }
130
+
131
+ type ColorMode = "truecolor" | "256color";
132
+
133
+ function fgAnsi(color: string, mode: ColorMode): string {
134
+ if (mode === "truecolor") {
135
+ const { r, g, b } = hexToRgb(color);
136
+ return `\x1b[38;2;${r};${g};${b}m`;
137
+ }
138
+ return `\x1b[38;5;${hexTo256(color)}m`;
139
+ }
140
+
141
+ function bgAnsi(color: string, mode: ColorMode): string {
142
+ if (mode === "truecolor") {
143
+ const { r, g, b } = hexToRgb(color);
144
+ return `\x1b[48;2;${r};${g};${b}m`;
145
+ }
146
+ return `\x1b[48;5;${hexTo256(color)}m`;
147
+ }
148
+
149
+ // ============================================================================
150
+ // Brightness detection
151
+ // ============================================================================
152
+
153
+ /** Approximate an xterm-256 palette index as RGB for brightness math. */
154
+ function indexToRgb(index: number): { r: number; g: number; b: number } {
155
+ if (index >= 16 && index <= 231) {
156
+ const i = index - 16;
157
+ return {
158
+ r: CUBE_VALUES[Math.floor(i / 36)]!,
159
+ g: CUBE_VALUES[Math.floor(i / 6) % 6]!,
160
+ b: CUBE_VALUES[i % 6]!,
161
+ };
162
+ }
163
+ if (index >= 232) {
164
+ const v = 8 + (index - 232) * 10;
165
+ return { r: v, g: v, b: v };
166
+ }
167
+ // Indices 0–15 are the standard ANSI colors; coarse but only used for the
168
+ // brightness estimate, never for emission.
169
+ const standard: Array<[number, number, number]> = [
170
+ [0, 0, 0], [170, 0, 0], [0, 170, 0], [170, 85, 0], [0, 0, 170], [170, 0, 170],
171
+ [0, 170, 170], [170, 170, 170], [85, 85, 85], [255, 85, 85], [85, 255, 85],
172
+ [255, 255, 85], [85, 85, 255], [255, 85, 255], [85, 255, 255], [255, 255, 255],
173
+ ];
174
+ const [r, g, b] = standard[index] ?? [170, 170, 170];
175
+ return { r, g, b };
176
+ }
177
+
178
+ /**
179
+ * Parse an ANSI foreground escape produced by `Theme.fg(color, "")` back into RGB.
180
+ * Returns `undefined` for anything unrecognized, in which case the dark palette is used.
181
+ */
182
+ export function parseFgAnsiRgb(ansi: string): { r: number; g: number; b: number } | undefined {
183
+ const truecolor = /^\x1b\[38;2;(\d+);(\d+);(\d+)m/.exec(ansi);
184
+ if (truecolor) {
185
+ const [, r, g, b] = truecolor;
186
+ return { r: Number(r), g: Number(g), b: Number(b) };
187
+ }
188
+ const indexed = /^\x1b\[38;5;(\d+)m/.exec(ansi);
189
+ if (indexed) return indexToRgb(Number(indexed[1]));
190
+ return undefined;
191
+ }
192
+
193
+ /**
194
+ * A dark theme paints bright text (pi dark `#d4d4d4`), a light theme paints dark text
195
+ * (pi light `#1f2328`). The grayscale midpoint cleanly separates the two.
196
+ */
197
+ function isLightText(base: Theme): boolean {
198
+ const probe = base.fg("text", "");
199
+ const rgb = parseFgAnsiRgb(probe);
200
+ if (!rgb) return false;
201
+ const gray = 0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b;
202
+ return gray < 120;
203
+ }
204
+
205
+ // ============================================================================
206
+ // The proxy
207
+ // ============================================================================
208
+
209
+ /** Tokens this layer rewrites; everything else is delegated to the base theme. */
210
+ const REMAPPED_FG = new Set<ThemeColor>(["accent"]);
211
+ const REMAPPED_BG = new Set<ThemeBg>(["selectedBg"]);
212
+
213
+ /**
214
+ * Wrap `base` so the two card tokens resolve against `palette`, chosen automatically
215
+ * for the terminal's brightness. Non-remapped tokens, `bold`, `getColorMode()`, and
216
+ * every other method pass straight through, so pi internals keep working unchanged.
217
+ */
218
+ export function wrapCardTheme(base: Theme, palette: CardPalette): Theme {
219
+ const variant = isLightText(base) ? palette.light : palette.dark;
220
+ const mode: ColorMode = base.getColorMode?.() ?? "truecolor";
221
+ const accent = fgAnsi(variant.accent, mode);
222
+ const selectedBg = bgAnsi(variant.selectedBg, mode);
223
+
224
+ const withFg = (text: string): string => `${accent}${text}\x1b[39m`;
225
+ const withBg = (text: string): string => `${selectedBg}${text}\x1b[49m`;
226
+
227
+ return new Proxy(base, {
228
+ get(target, prop, receiver) {
229
+ switch (prop) {
230
+ case "fg":
231
+ return (color: ThemeColor, text: string) =>
232
+ REMAPPED_FG.has(color) ? withFg(text) : target.fg(color, text);
233
+ case "bg":
234
+ return (color: ThemeBg, text: string) =>
235
+ REMAPPED_BG.has(color) ? withBg(text) : target.bg(color, text);
236
+ case "getFgAnsi":
237
+ return (color: ThemeColor) => (REMAPPED_FG.has(color) ? accent : target.getFgAnsi(color));
238
+ case "getBgAnsi":
239
+ return (color: ThemeBg) => (REMAPPED_BG.has(color) ? selectedBg : target.getBgAnsi(color));
240
+ default:
241
+ return Reflect.get(target, prop, receiver);
242
+ }
243
+ },
244
+ }) as Theme;
245
+ }
@@ -0,0 +1,66 @@
1
+ import { CURSOR_MARKER, wrapTextWithAnsi } from "@earendil-works/pi-tui";
2
+
3
+ // Grapheme-aware extraction at the cursor: pi-tui's Editor reports UTF-16
4
+ // line/column positions, so the cursor can land between code units of one cluster
5
+ // (emoji, ZWJ, combining marks). Single-code-unit slicing would split the cluster
6
+ // across the SGR 7/27 boundary. Both single- and multi-select views share this
7
+ // cursor-building core.
8
+ const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
9
+
10
+ export interface RenderInlineInputOptions {
11
+ /** Live inline-input buffer. */
12
+ buffer: string;
13
+ /** Cursor offset; `undefined` / out-of-range → end-of-buffer fallback. */
14
+ cursorOffset: number | undefined;
15
+ /** Prefix for the first emitted line (e.g. `❯ 4. `). */
16
+ rowPrefix: string;
17
+ /** Prefix for continuation lines (whitespace of equal visible width). */
18
+ continuationPrefix: string;
19
+ /** Visible columns available for the buffer content (width − prefix width). */
20
+ contentWidth: number;
21
+ /** Per-line styling (single-select: `theme.selectedText`; multi-select: accent+bold). */
22
+ selectedText: (text: string) => string;
23
+ }
24
+
25
+ /**
26
+ * Resolve the cursor offset, falling back to end-of-buffer for `undefined`/out-of-range.
27
+ * Mirrors the original wrapping-select.resolveOffset exactly.
28
+ */
29
+ function resolveCursorOffset(buffer: string, requested: number | undefined): number {
30
+ if (requested !== undefined && requested >= 0 && requested <= buffer.length) return requested;
31
+ return buffer.length;
32
+ }
33
+
34
+ /**
35
+ * Build the cursor-marked raw string for the whole buffer: `before | CURSOR_MARKER |
36
+ * SGR-7 reverse-video cell | SGR-27 | after`. The cell UNDER the cursor is the single
37
+ * grapheme at the offset (or U+00A0 NBSP at end-of-buffer / on a literal space — NBSP is
38
+ * wrap-safe where a literal space would tokenize as a wrap break). Zero characters shift;
39
+ * the column under the cursor inverts. `CURSOR_MARKER` is zero-width so wrap/truncate math
40
+ * is preserved.
41
+ */
42
+ function buildCursorRaw(buffer: string, offset: number): string {
43
+ const before = buffer.slice(0, offset);
44
+ const [firstGrapheme] = graphemeSegmenter.segment(buffer.slice(offset));
45
+ const rawAt = firstGrapheme ? firstGrapheme.segment : "";
46
+ // A logical newline has no visible cell. Draw the cursor on an NBSP immediately
47
+ // before it and leave the newline unconsumed so the next logical line still renders.
48
+ const cursorAtLineEnd = rawAt === "\n";
49
+ const atCursor = rawAt === "" || rawAt === " " || cursorAtLineEnd ? "\xa0" : rawAt;
50
+ const after = buffer.slice(offset + (cursorAtLineEnd ? 0 : rawAt.length));
51
+ return `${before}${CURSOR_MARKER}\x1b[7m${atCursor}\x1b[27m${after}`;
52
+ }
53
+
54
+ /**
55
+ * Render the inline editor across logical and visually wrapped lines.
56
+ * Cursor visualization follows Pi's editor pattern: reverse-video on the cell at
57
+ * the cursor, with a non-breaking-space cell at end-of-line/end-of-buffer.
58
+ */
59
+ export function renderInlineInputRow(opts: RenderInlineInputOptions): string[] {
60
+ const { buffer, cursorOffset, rowPrefix, continuationPrefix, contentWidth, selectedText } = opts;
61
+ const raw = buildCursorRaw(buffer, resolveCursorOffset(buffer, cursorOffset));
62
+ return wrapTextWithAnsi(raw, contentWidth).map((segment, index) => {
63
+ const prefix = index === 0 ? rowPrefix : continuationPrefix;
64
+ return selectedText(`${prefix}${segment}`);
65
+ });
66
+ }
@@ -0,0 +1,177 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
3
+ import { displayLabel } from "../../i18n.js";
4
+ import type { QuestionData } from "../../tool/types.js";
5
+ import type { StatefulView } from "../stateful-view.js";
6
+ import { renderInlineInputRow } from "./inline-input.js";
7
+
8
+ const ACTIVE_POINTER = "❯ ";
9
+ const INACTIVE_POINTER = " ";
10
+ const CHECKED = "[✔]";
11
+ const UNCHECKED = "[ ]";
12
+ const NUMBER_SEPARATOR = ". ";
13
+ const BOX_LABEL_GAP = " ";
14
+ // CC parity: description continuation indents to col 2 (past the pointer slot), NOT to the
15
+ // full prefix column. Wrap width still uses prefixVisibleWidth so naturalHeight matches render.
16
+ const CONTINUATION_INDENT = " ";
17
+
18
+ export const MULTI_SUBMIT_LABEL = "Submit";
19
+
20
+ export interface MultiSelectOtherRowProps {
21
+ /** The "Type something." row is the focused row (optionIndex === options.length). */
22
+ active: boolean;
23
+ /** `state.inputMode` — true once the row has focus and keystrokes append to the buffer. */
24
+ inputMode: boolean;
25
+ /** Live inline-input buffer (read from `runtime.inputBuffer` / `ctx.inputBuffer`). */
26
+ inputBuffer: string;
27
+ inputCursorOffset: number | undefined;
28
+ }
29
+
30
+ export interface MultiSelectViewProps {
31
+ rows: ReadonlyArray<{ checked: boolean; active: boolean }>;
32
+ other: MultiSelectOtherRowProps;
33
+ nextActive: boolean;
34
+ nextLabel: string;
35
+ }
36
+
37
+ /**
38
+ * Renders the multi-select option list (one row per option — pointer + checkbox + label —
39
+ * plus zero or more wrapped continuation lines per description).
40
+ *
41
+ * `naturalHeight(width)` is the rendered height for the current props. It grows when
42
+ * the custom-answer editor contains logical or visually wrapped lines, allowing the
43
+ * dialog to reserve exactly the space the active draft needs.
44
+ *
45
+ * One width-keyed layout supplies rendering, height, and focused-row measurement;
46
+ * `setProps` and `invalidate` discard that derived cache.
47
+ */
48
+ interface MultiSelectLayout {
49
+ lines: string[];
50
+ focusedRange: [number, number];
51
+ }
52
+
53
+ export class MultiSelectView implements StatefulView<MultiSelectViewProps> {
54
+ private props: MultiSelectViewProps;
55
+ private cachedLayout: { width: number; value: MultiSelectLayout } | undefined;
56
+
57
+ constructor(
58
+ private readonly theme: Theme,
59
+ private readonly question: QuestionData,
60
+ ) {
61
+ this.props = {
62
+ rows: [],
63
+ other: { active: false, inputMode: false, inputBuffer: "", inputCursorOffset: undefined },
64
+ nextActive: false,
65
+ nextLabel: displayLabel("next"),
66
+ };
67
+ }
68
+
69
+ setProps(props: MultiSelectViewProps): void {
70
+ this.props = props;
71
+ this.cachedLayout = undefined;
72
+ }
73
+
74
+ handleInput(_data: string): void {}
75
+
76
+ invalidate(): void {
77
+ this.cachedLayout = undefined;
78
+ }
79
+
80
+ render(width: number): string[] {
81
+ return this.layout(width).lines;
82
+ }
83
+
84
+ focusedItemRowRange(width: number): [number, number] {
85
+ return this.layout(width).focusedRange;
86
+ }
87
+
88
+ naturalHeight(width: number): number {
89
+ return this.layout(width).lines.length;
90
+ }
91
+
92
+ private layout(width: number): MultiSelectLayout {
93
+ if (this.cachedLayout?.width === width) return this.cachedLayout.value;
94
+
95
+ const lines: string[] = [];
96
+ let focusedRange: [number, number] = [0, 0];
97
+ const contentWidth = Math.max(1, width - this.prefixVisibleWidth());
98
+ const numberWidth = String(Math.max(1, this.question.options.length + 1)).length;
99
+ for (let i = 0; i < this.question.options.length; i++) {
100
+ const opt = this.question.options[i];
101
+ const row = this.props.rows[i];
102
+ if (!opt || !row) continue;
103
+ const start = lines.length;
104
+ const pointer = row.active ? this.theme.fg("accent", ACTIVE_POINTER) : INACTIVE_POINTER;
105
+ // Checked and active rows share the accent hue, matching the dialog's selection rhythm.
106
+ const box = row.checked ? this.theme.fg("accent", CHECKED) : this.theme.fg("muted", UNCHECKED);
107
+ const label = truncateToWidth(opt.label, contentWidth, "…");
108
+ const styledLabel = row.active ? this.theme.fg("accent", this.theme.bold(label)) : label;
109
+ const number = String(i + 1).padStart(numberWidth, " ");
110
+ lines.push(
111
+ truncateToWidth(`${pointer}${number}${NUMBER_SEPARATOR}${box}${BOX_LABEL_GAP}${styledLabel}`, width, ""),
112
+ );
113
+ if (opt.description) {
114
+ for (const segment of wrapTextWithAnsi(opt.description, contentWidth)) {
115
+ lines.push(CONTINUATION_INDENT + this.theme.fg("muted", segment));
116
+ }
117
+ }
118
+ if (row.active) focusedRange = [start, lines.length];
119
+ }
120
+
121
+ const otherStart = lines.length;
122
+ lines.push(...this.renderOtherRow(contentWidth, numberWidth));
123
+ if (this.props.other.active) focusedRange = [otherStart, lines.length];
124
+
125
+ const nextStart = lines.length;
126
+ const nextPointer = this.props.nextActive ? this.theme.fg("accent", ACTIVE_POINTER) : INACTIVE_POINTER;
127
+ const nextLabel = this.props.nextActive
128
+ ? this.theme.fg("accent", this.theme.bold(this.props.nextLabel))
129
+ : this.props.nextLabel;
130
+ lines.push(truncateToWidth(`${nextPointer}${nextLabel}`, width, ""));
131
+ if (this.props.nextActive) focusedRange = [nextStart, lines.length];
132
+
133
+ const value = { lines, focusedRange };
134
+ this.cachedLayout = { width, value };
135
+ return value;
136
+ }
137
+
138
+ private renderOtherRow(contentWidth: number, numberWidth: number): string[] {
139
+ const other = this.props.other;
140
+ const pointer = other.active ? this.theme.fg("accent", ACTIVE_POINTER) : INACTIVE_POINTER;
141
+ const box = this.theme.fg("muted", UNCHECKED);
142
+ const number = String(this.question.options.length + 1).padStart(numberWidth, " ");
143
+ const rowPrefix = `${pointer}${number}${NUMBER_SEPARATOR}${box}${BOX_LABEL_GAP}`;
144
+ const continuationPrefix = " ".repeat(visibleWidth(rowPrefix));
145
+ const selectedText = (text: string) => this.theme.fg("accent", this.theme.bold(text));
146
+
147
+ if (other.active && other.inputMode) {
148
+ return renderInlineInputRow({
149
+ buffer: other.inputBuffer,
150
+ cursorOffset: other.inputCursorOffset,
151
+ rowPrefix,
152
+ continuationPrefix,
153
+ contentWidth,
154
+ selectedText,
155
+ });
156
+ }
157
+
158
+ return wrapTextWithAnsi(other.inputBuffer || displayLabel("other"), contentWidth).map((segment, index) => {
159
+ const line = `${index === 0 ? rowPrefix : continuationPrefix}${segment}`;
160
+ return other.active ? selectedText(line) : line;
161
+ });
162
+ }
163
+
164
+ private prefixVisibleWidth(): number {
165
+ // Canonical prefix for OPTION rows: INACTIVE_POINTER + numberWidth digits + NUMBER_SEPARATOR
166
+ // + UNCHECKED + BOX_LABEL_GAP. State-independent because ACTIVE/INACTIVE pointer share
167
+ // visibleWidth, CHECKED/UNCHECKED share visibleWidth, and numberWidth is constant per question.
168
+ // The number column fits `options.length + 1` so the "Type something." row's N+1 number
169
+ // is never clipped. The Next sentinel uses a bare `pointer + "Next"` shape — its width
170
+ // never exceeds this prefix at any reasonable terminal width, so it's safe to leave it
171
+ // out of the canonical computation.
172
+ const numberWidth = String(Math.max(1, this.question.options.length + 1)).length;
173
+ return (
174
+ visibleWidth(INACTIVE_POINTER) + numberWidth + visibleWidth(`${NUMBER_SEPARATOR}${UNCHECKED}${BOX_LABEL_GAP}`)
175
+ );
176
+ }
177
+ }
@@ -0,0 +1,65 @@
1
+ import type { StatefulView } from "../stateful-view.js";
2
+ import { WrappingSelect, type WrappingSelectItem, type WrappingSelectTheme } from "./wrapping-select.js";
3
+
4
+ /**
5
+ * Maximum number of option rows visible in the WrappingSelect window. Lifted here from
6
+ * `preview-pane.ts` so the cap travels with the option-list owner.
7
+ */
8
+ export const MAX_VISIBLE_OPTIONS = 10;
9
+
10
+ export interface OptionListViewConfig {
11
+ items: readonly WrappingSelectItem[];
12
+ theme: WrappingSelectTheme;
13
+ }
14
+
15
+ /**
16
+ * Per-tick projection of OptionListView state. After Phase 11b, `inputBuffer`
17
+ * is part of the props bag — the session-owned `inlineInput` (a headless
18
+ * `pi-tui` Editor instance) supplies its current `getText()` here per tick.
19
+ * `OptionListView` is purely props-driven; the imperative buffer surface and
20
+ * read-back getters are gone.
21
+ */
22
+ export interface OptionListViewProps {
23
+ selectedIndex: number;
24
+ focused: boolean;
25
+ inputBuffer: string;
26
+ inputCursorOffset?: number;
27
+ /** Optional previously-confirmed indicator. Omit when no marker should be drawn. */
28
+ confirmed?: { index: number; labelOverride?: string };
29
+ }
30
+
31
+ /**
32
+ * Sole owner of the option list's interactive state. Wraps a single
33
+ * `WrappingSelect`. Implements `StatefulView<OptionListViewProps>`:
34
+ * `setProps` is the only mutator; render output reflects the last props
35
+ * received.
36
+ */
37
+ export class OptionListView implements StatefulView<OptionListViewProps> {
38
+ private readonly select: WrappingSelect;
39
+
40
+ constructor(config: OptionListViewConfig) {
41
+ this.select = new WrappingSelect(config.items, Math.min(config.items.length, MAX_VISIBLE_OPTIONS), config.theme);
42
+ }
43
+
44
+ setProps(props: OptionListViewProps): void {
45
+ this.select.setSelectedIndex(props.selectedIndex);
46
+ this.select.setFocused(props.focused);
47
+ this.select.setConfirmedIndex(props.confirmed?.index, props.confirmed?.labelOverride);
48
+ this.select.setInputBuffer(props.inputBuffer);
49
+ this.select.setInputCursorOffset(props.inputCursorOffset);
50
+ }
51
+
52
+ handleInput(_data: string): void {}
53
+
54
+ invalidate(): void {
55
+ this.select.invalidate();
56
+ }
57
+
58
+ render(width: number): string[] {
59
+ return this.select.render(width);
60
+ }
61
+
62
+ focusedItemRowRange(width: number): [number, number] {
63
+ return this.select.focusedItemRowRange(width);
64
+ }
65
+ }
@@ -0,0 +1,101 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import { Markdown, type MarkdownTheme, visibleWidth } from "@earendil-works/pi-tui";
3
+ import { t } from "../../../i18n.js";
4
+ import type { QuestionData } from "../../../tool/types.js";
5
+ import { stripFenceMarkers } from "./preview-box-renderer.js";
6
+
7
+ /** CC parity in side-by-side layout. */
8
+ export const MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE = 20;
9
+ /** Preserves narrow-terminal protection in stacked layout. */
10
+ export const MAX_PREVIEW_HEIGHT_STACKED = 15;
11
+ export const NO_PREVIEW_TEXT = "No preview available";
12
+ /** 1 blank separator + 1 affordance text row reserved when `hasAnyPreview` (height stability of the affordance row's offset relative to the box). */
13
+ export const NOTES_AFFORDANCE_OVERHEAD = 2;
14
+
15
+ /**
16
+ * Per-question cache for rendered markdown previews. Width-keyed: switching the
17
+ * inner width invalidates every cached `Markdown`'s render output (pi-tui's
18
+ * `Markdown.render(width)` re-wraps when width changes).
19
+ *
20
+ * Replaces the inline `previewTexts`, `markdownCache`, `cachedWidth` triple from
21
+ * the previous monolithic `preview-pane.ts`. One Markdown per option, lazy on
22
+ * first request, never re-constructed — count semantics frozen by tests.
23
+ */
24
+ export class MarkdownContentCache {
25
+ private readonly previewTexts: Map<number, string>;
26
+ private readonly markdownCache: Map<number, Markdown>;
27
+ private readonly questionPreview: string | undefined;
28
+ private questionMarkdown: Markdown | undefined;
29
+ private cachedWidth: number | undefined;
30
+ private readonly theme: Theme;
31
+ private readonly markdownTheme: MarkdownTheme;
32
+
33
+ constructor(question: QuestionData, theme: Theme, markdownTheme: MarkdownTheme, private readonly document = false) {
34
+ this.theme = theme;
35
+ this.markdownTheme = markdownTheme;
36
+ this.previewTexts = new Map();
37
+ for (let i = 0; i < question.options.length; i++) {
38
+ const raw = question.options[i]?.preview;
39
+ if (raw && raw.length > 0) this.previewTexts.set(i, raw);
40
+ }
41
+ this.questionPreview = question.preview && question.preview.length > 0 ? question.preview : undefined;
42
+ this.markdownCache = new Map();
43
+ this.questionMarkdown = undefined;
44
+ }
45
+
46
+ hasAnyPreview(): boolean {
47
+ return this.previewTexts.size > 0;
48
+ }
49
+
50
+ has(optionIndex: number): boolean {
51
+ return this.previewTexts.has(optionIndex);
52
+ }
53
+
54
+ /**
55
+ * Compute the body lines for a given option at a given inner width. Width changes
56
+ * invalidate the per-Markdown render cache.
57
+ */
58
+ bodyFor(optionIndex: number, innerWidth: number): string[] {
59
+ if (this.cachedWidth !== innerWidth) {
60
+ for (const md of this.markdownCache.values()) md.invalidate();
61
+ this.cachedWidth = innerWidth;
62
+ }
63
+ const text = this.previewTexts.get(optionIndex);
64
+ if (!text) {
65
+ const placeholder = this.theme.fg("dim", t("preview.no_preview", NO_PREVIEW_TEXT));
66
+ const pad = Math.max(0, innerWidth - visibleWidth(placeholder));
67
+ return [placeholder + " ".repeat(pad)];
68
+ }
69
+ let md = this.markdownCache.get(optionIndex);
70
+ if (!md) {
71
+ md = new Markdown(text, 0, 0, this.markdownTheme);
72
+ this.markdownCache.set(optionIndex, md);
73
+ }
74
+ return stripFenceMarkers(md.render(innerWidth));
75
+ }
76
+
77
+ /**
78
+ * Compute the body lines for the question-level preview at a given inner width. Renders
79
+ * `question.preview` directly (never an option preview), lazily caching one Markdown instance.
80
+ * Returns [] when the question carries no preview.
81
+ */
82
+ questionBodyFor(innerWidth: number): string[] {
83
+ if (this.cachedWidth !== innerWidth) {
84
+ for (const md of this.markdownCache.values()) md.invalidate();
85
+ this.questionMarkdown?.invalidate();
86
+ this.cachedWidth = innerWidth;
87
+ }
88
+ if (!this.questionPreview) return [];
89
+ if (!this.questionMarkdown) {
90
+ this.questionMarkdown = new Markdown(this.questionPreview, 0, 0, this.markdownTheme, undefined, { preserveOrderedListMarkers: this.document, renderLatex: !this.document });
91
+ }
92
+ const lines = this.questionMarkdown.render(innerWidth);
93
+ return this.document ? lines : stripFenceMarkers(lines);
94
+ }
95
+
96
+ invalidate(): void {
97
+ for (const md of this.markdownCache.values()) md.invalidate();
98
+ this.questionMarkdown?.invalidate();
99
+ this.cachedWidth = undefined;
100
+ }
101
+ }