@ftrdotdev/pi-inquiry 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +268 -0
- package/README.md +24 -4
- package/package.json +18 -16
- package/src/core/domain.test.ts +198 -9
- package/src/core/domain.ts +75 -24
- package/src/core/editor.test.ts +87 -42
- package/src/core/flow.test.ts +22 -3
- package/src/core/flow.ts +7 -3
- package/src/core/keyboard.test.ts +3 -3
- package/src/core/keyboard.ts +1 -1
- package/src/core/machine.test.ts +25 -2
- package/src/core/machine.ts +30 -22
- package/src/core/purity.test.ts +118 -0
- package/src/core/scene.test.ts +75 -20
- package/src/core/scene.ts +78 -42
- package/src/core/services.ts +6 -5
- package/src/core/text.test.ts +44 -15
- package/src/core/text.ts +7 -0
- package/src/harness-policy.test.ts +27 -0
- package/src/sdk/box-parity.test.ts +2 -7
- package/src/sdk/keys.test.ts +24 -1
- package/src/sdk/painter.ts +3 -3
- package/src/sdk/pi-ui.test.ts +56 -12
- package/src/sdk/pi-ui.ts +118 -93
- package/src/sdk/schema.test.ts +31 -1
- package/src/tool/question.test.ts +136 -6
- package/src/tool/question.ts +11 -12
package/src/core/scene.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
multiEntries,
|
|
17
17
|
type SuggestionState,
|
|
18
18
|
} from "./machine.ts";
|
|
19
|
-
import { truncateToWidth, visibleWidth } from "./text.ts";
|
|
19
|
+
import { isNonEmptyString, truncateToWidth, visibleWidth } from "./text.ts";
|
|
20
20
|
import type { EditorState } from "./editor.ts";
|
|
21
21
|
|
|
22
22
|
export type Style =
|
|
@@ -42,7 +42,7 @@ export interface Scene {
|
|
|
42
42
|
readonly lines: readonly SceneLine[];
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
export const span = (text: string, style?: Style, bold = false): Span => ({
|
|
45
|
+
export const span = (text: string, style?: Style, bold: boolean = false): Span => ({
|
|
46
46
|
text,
|
|
47
47
|
...(style === undefined ? {} : { style }),
|
|
48
48
|
...(bold ? { bold: true } : {}),
|
|
@@ -71,7 +71,11 @@ export const wrapLine = (content: SceneLine, width: number): readonly SceneLine[
|
|
|
71
71
|
continue;
|
|
72
72
|
}
|
|
73
73
|
if (part !== "")
|
|
74
|
-
segments[segments.length - 1]!.push({
|
|
74
|
+
segments[segments.length - 1]!.push({
|
|
75
|
+
text: part,
|
|
76
|
+
...(s.style === undefined ? {} : { style: s.style }),
|
|
77
|
+
...(s.bold === undefined ? {} : { bold: s.bold }),
|
|
78
|
+
});
|
|
75
79
|
}
|
|
76
80
|
}
|
|
77
81
|
|
|
@@ -91,7 +95,12 @@ const wrapSegment = (segment: SceneLine, width: number): readonly SceneLine[] =>
|
|
|
91
95
|
const tokens: Span[] = [];
|
|
92
96
|
for (const s of segment) {
|
|
93
97
|
for (const part of s.text.split(/(\s+)/)) {
|
|
94
|
-
if (part !== "")
|
|
98
|
+
if (part !== "")
|
|
99
|
+
tokens.push({
|
|
100
|
+
text: part,
|
|
101
|
+
...(s.style === undefined ? {} : { style: s.style }),
|
|
102
|
+
...(s.bold === undefined ? {} : { bold: s.bold }),
|
|
103
|
+
});
|
|
95
104
|
}
|
|
96
105
|
}
|
|
97
106
|
|
|
@@ -152,9 +161,30 @@ const breakLongSpan = (token: Span, width: number): readonly Span[][] => {
|
|
|
152
161
|
}
|
|
153
162
|
if (current !== "") chunks.push(current);
|
|
154
163
|
if (chunks.length === 0) chunks.push("");
|
|
155
|
-
return chunks.map((text) => [
|
|
164
|
+
return chunks.map((text) => [
|
|
165
|
+
{
|
|
166
|
+
text,
|
|
167
|
+
...(token.style === undefined ? {} : { style: token.style }),
|
|
168
|
+
...(token.bold === undefined ? {} : { bold: token.bold }),
|
|
169
|
+
},
|
|
170
|
+
]);
|
|
156
171
|
};
|
|
157
172
|
|
|
173
|
+
/** Description and subtitle indent for the option block: seven cells, two more than before the marker column. */
|
|
174
|
+
const OPTION_TEXT_INDENT = " ";
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The option block's left gutter: the cursor cell, then the reserved marker
|
|
178
|
+
* column. Every row reserves the column, so the block has one geometry
|
|
179
|
+
* regardless of question data. The `✓` is the tab bar's success glyph. The
|
|
180
|
+
* cell after `✓` stays blank, so labels start at the same column with or
|
|
181
|
+
* without a recommendation.
|
|
182
|
+
*/
|
|
183
|
+
const optionRowPrefix = (selected: boolean, recommended: boolean): SceneLine => [
|
|
184
|
+
selected ? span("→ ", "accent") : span(" "),
|
|
185
|
+
recommended ? span("✓ ", "success") : span(" "),
|
|
186
|
+
];
|
|
187
|
+
|
|
158
188
|
/**
|
|
159
189
|
* Build the scene for the current machine state.
|
|
160
190
|
*
|
|
@@ -168,28 +198,28 @@ const breakLongSpan = (token: Span, width: number): readonly Span[][] => {
|
|
|
168
198
|
export const buildScene = (state: MachineState, width: number): Scene => {
|
|
169
199
|
const renderWidth = Math.max(1, width);
|
|
170
200
|
const frame = buildBoxFrame(renderWidth, { paddingX: 0 });
|
|
171
|
-
if (
|
|
201
|
+
if (frame === null) return { lines: [] };
|
|
172
202
|
const contentWidth = frame.contentWidth;
|
|
173
203
|
const lines: SceneLine[] = [];
|
|
174
204
|
const question = state.questions[state.currentTab];
|
|
175
205
|
const options = currentOptions(state);
|
|
176
206
|
|
|
177
|
-
const pushWrapped = (prefix:
|
|
178
|
-
const prefixWidth = visibleWidth(prefix
|
|
207
|
+
const pushWrapped = (prefix: SceneLine, content: SceneLine): void => {
|
|
208
|
+
const prefixWidth = visibleWidth(plain(prefix));
|
|
179
209
|
if (prefixWidth >= contentWidth) {
|
|
180
|
-
lines.push([prefix, ...content]);
|
|
210
|
+
lines.push([...prefix, ...content]);
|
|
181
211
|
return;
|
|
182
212
|
}
|
|
183
213
|
const wrapped = wrapLine(content, contentWidth - prefixWidth);
|
|
184
214
|
for (let i = 0; i < wrapped.length; i++) {
|
|
185
215
|
lines.push(
|
|
186
|
-
i === 0 ? [prefix, ...wrapped[i]!] : [span(" ".repeat(prefixWidth)), ...wrapped[i]!],
|
|
216
|
+
i === 0 ? [...prefix, ...wrapped[i]!] : [span(" ".repeat(prefixWidth)), ...wrapped[i]!],
|
|
187
217
|
);
|
|
188
218
|
}
|
|
189
219
|
};
|
|
190
220
|
|
|
191
221
|
const renderOptions = (): void => {
|
|
192
|
-
if (question?.multiple) {
|
|
222
|
+
if (question?.multiple ?? false) {
|
|
193
223
|
renderMultiOptions();
|
|
194
224
|
return;
|
|
195
225
|
}
|
|
@@ -199,14 +229,19 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
199
229
|
const isOther = option.isOther;
|
|
200
230
|
const labelText = `${i + 1}. ${option.label}${isOther && state.inputMode ? " ✎" : ""}`;
|
|
201
231
|
const color = selected || (isOther && state.inputMode) ? "accent" : "text";
|
|
202
|
-
pushWrapped(selected
|
|
203
|
-
if (option.description) {
|
|
204
|
-
pushWrapped(span(
|
|
205
|
-
} else if (option.isOther && !state.inputMode && question) {
|
|
232
|
+
pushWrapped(optionRowPrefix(selected, option.recommended), [span(labelText, color)]);
|
|
233
|
+
if (isNonEmptyString(option.description)) {
|
|
234
|
+
pushWrapped([span(OPTION_TEXT_INDENT)], [span(option.description, "muted")]);
|
|
235
|
+
} else if (option.isOther && !state.inputMode && question !== undefined) {
|
|
206
236
|
// Show an existing custom answer as a subtitle under "Type something".
|
|
237
|
+
// The strict comparison, rather than `?? false`, is what narrows
|
|
238
|
+
// `answer` for the label read below.
|
|
207
239
|
const answer = state.answers.get(question.id)?.[0];
|
|
208
|
-
if (answer?.wasCustom) {
|
|
209
|
-
pushWrapped(
|
|
240
|
+
if (answer?.wasCustom === true) {
|
|
241
|
+
pushWrapped(
|
|
242
|
+
[span(OPTION_TEXT_INDENT)],
|
|
243
|
+
[span(`Current answer: ${answer.label}`, "muted")],
|
|
244
|
+
);
|
|
210
245
|
}
|
|
211
246
|
}
|
|
212
247
|
}
|
|
@@ -234,11 +269,11 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
234
269
|
} else {
|
|
235
270
|
labelText = "+ Add your own answer";
|
|
236
271
|
}
|
|
237
|
-
pushWrapped(selected
|
|
272
|
+
pushWrapped(optionRowPrefix(selected, entry.kind === "option" && entry.recommended), [
|
|
238
273
|
span(labelText, selected ? "accent" : "text"),
|
|
239
274
|
]);
|
|
240
|
-
if (description) {
|
|
241
|
-
pushWrapped(span(
|
|
275
|
+
if (isNonEmptyString(description)) {
|
|
276
|
+
pushWrapped([span(OPTION_TEXT_INDENT)], [span(description, "muted")]);
|
|
242
277
|
}
|
|
243
278
|
}
|
|
244
279
|
};
|
|
@@ -265,23 +300,23 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
265
300
|
isSubmitTab ? span(submitText, "accent") : span(submitText, canSubmit ? "success" : "dim"),
|
|
266
301
|
span(" →"),
|
|
267
302
|
);
|
|
268
|
-
pushWrapped(span(" "), tabs);
|
|
303
|
+
pushWrapped([span(" ")], tabs);
|
|
269
304
|
lines.push([]);
|
|
270
305
|
}
|
|
271
306
|
|
|
272
307
|
// Content
|
|
273
|
-
if (state.inputMode && question) {
|
|
274
|
-
pushWrapped(span(" "), [span(question.prompt, "text")]);
|
|
308
|
+
if (state.inputMode && question !== undefined) {
|
|
309
|
+
pushWrapped([span(" ")], [span(question.prompt, "text")]);
|
|
275
310
|
lines.push([]);
|
|
276
311
|
renderOptions();
|
|
277
312
|
lines.push([]);
|
|
278
|
-
pushWrapped(span(" "), [span("Your answer:", "muted")]);
|
|
313
|
+
pushWrapped([span(" ")], [span("Your answer:", "muted")]);
|
|
279
314
|
// The editor box spans the content between the rails minus its 1-space
|
|
280
315
|
// gutter; the popup floats two spaces deeper under the input.
|
|
281
316
|
for (const editorLine of buildEditorBox(state.editor, Math.max(1, contentWidth - 1))) {
|
|
282
317
|
lines.push([span(" "), ...editorLine]);
|
|
283
318
|
}
|
|
284
|
-
if (state.suggestions) {
|
|
319
|
+
if (state.suggestions !== null) {
|
|
285
320
|
for (const popupLine of buildSuggestionPopup(
|
|
286
321
|
state.suggestions,
|
|
287
322
|
Math.max(1, contentWidth - 4),
|
|
@@ -290,19 +325,20 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
290
325
|
}
|
|
291
326
|
}
|
|
292
327
|
lines.push([]);
|
|
293
|
-
if (state.suggestions) {
|
|
294
|
-
pushWrapped(span(" "), [span("↑↓ pick • Enter/Tab complete • Esc close", "dim")]);
|
|
328
|
+
if (state.suggestions !== null) {
|
|
329
|
+
pushWrapped([span(" ")], [span("↑↓ pick • Enter/Tab complete • Esc close", "dim")]);
|
|
295
330
|
} else {
|
|
296
|
-
pushWrapped(
|
|
297
|
-
span("
|
|
298
|
-
|
|
331
|
+
pushWrapped(
|
|
332
|
+
[span(" ")],
|
|
333
|
+
[span("Enter to submit • Shift+Enter for newline • Esc to go back", "dim")],
|
|
334
|
+
);
|
|
299
335
|
}
|
|
300
336
|
} else if (state.currentTab === state.questions.length) {
|
|
301
|
-
pushWrapped(span(" "), [span("Ready to submit", "accent", true)]);
|
|
337
|
+
pushWrapped([span(" ")], [span("Ready to submit", "accent", true)]);
|
|
302
338
|
lines.push([]);
|
|
303
339
|
for (const q of state.questions) {
|
|
304
340
|
const answers = state.answers.get(q.id);
|
|
305
|
-
if (answers && answers.length > 0) {
|
|
341
|
+
if (answers !== undefined && answers.length > 0) {
|
|
306
342
|
// A single answer keeps the legacy bare label; a multi-select answer
|
|
307
343
|
// lists each chosen option/alternative with its index.
|
|
308
344
|
const rendered =
|
|
@@ -313,28 +349,28 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
313
349
|
: answers
|
|
314
350
|
.map((a) => (a.wasCustom ? `(wrote) ${a.label}` : `${a.index}. ${a.label}`))
|
|
315
351
|
.join(", ");
|
|
316
|
-
pushWrapped(span(" "), [span(`${q.label}: `, "muted"), span(rendered, "text")]);
|
|
352
|
+
pushWrapped([span(" ")], [span(`${q.label}: `, "muted"), span(rendered, "text")]);
|
|
317
353
|
}
|
|
318
354
|
}
|
|
319
355
|
lines.push([]);
|
|
320
356
|
if (isAllAnswered(state)) {
|
|
321
|
-
pushWrapped(span(" "), [span("Press Enter to submit", "success")]);
|
|
357
|
+
pushWrapped([span(" ")], [span("Press Enter to submit", "success")]);
|
|
322
358
|
} else {
|
|
323
359
|
const missing = state.questions
|
|
324
360
|
.filter((q) => !state.answers.has(q.id))
|
|
325
361
|
.map((q) => q.label)
|
|
326
362
|
.join(", ");
|
|
327
|
-
pushWrapped(span(" "), [span(`Unanswered: ${missing}`, "warning")]);
|
|
363
|
+
pushWrapped([span(" ")], [span(`Unanswered: ${missing}`, "warning")]);
|
|
328
364
|
}
|
|
329
|
-
} else if (question) {
|
|
330
|
-
pushWrapped(span(" "), [span(question.prompt, "text")]);
|
|
365
|
+
} else if (question !== undefined) {
|
|
366
|
+
pushWrapped([span(" ")], [span(question.prompt, "text")]);
|
|
331
367
|
lines.push([]);
|
|
332
368
|
renderOptions();
|
|
333
369
|
}
|
|
334
370
|
|
|
335
371
|
lines.push([]);
|
|
336
372
|
if (!state.inputMode) {
|
|
337
|
-
const isMultiSelect = question?.multiple
|
|
373
|
+
const isMultiSelect = question?.multiple ?? false;
|
|
338
374
|
const help =
|
|
339
375
|
state.mode === "multi"
|
|
340
376
|
? isMultiSelect
|
|
@@ -343,7 +379,7 @@ export const buildScene = (state: MachineState, width: number): Scene => {
|
|
|
343
379
|
: isMultiSelect
|
|
344
380
|
? "↑↓ move • Space toggles • Enter confirm • Esc cancel"
|
|
345
381
|
: "↑↓ navigate • Enter to select • Esc to cancel";
|
|
346
|
-
pushWrapped(span(" "), [span(help, "dim")]);
|
|
382
|
+
pushWrapped([span(" ")], [span(help, "dim")]);
|
|
347
383
|
}
|
|
348
384
|
|
|
349
385
|
// Frame the interior: accent border with rails, body padded to the content
|
|
@@ -401,7 +437,7 @@ export const clampLine = (content: SceneLine, width: number): SceneLine => {
|
|
|
401
437
|
export const buildEditorBox = (editor: EditorState, boxWidth: number): readonly SceneLine[] => {
|
|
402
438
|
const box = Math.max(4, boxWidth);
|
|
403
439
|
const frame = buildBoxFrame(box, { paddingX: 1 });
|
|
404
|
-
if (
|
|
440
|
+
if (frame === null) return [];
|
|
405
441
|
const border = (text: string): Span => span(text, "border");
|
|
406
442
|
const rows = buildEditorRows(editor, frame.contentWidth);
|
|
407
443
|
return [
|
|
@@ -485,7 +521,7 @@ export const buildEditorRows = (
|
|
|
485
521
|
// cell counts toward the width).
|
|
486
522
|
const renderedWidth = visibleWidth(plain(spans));
|
|
487
523
|
const padding = " ".repeat(Math.max(0, contentWidth - renderedWidth));
|
|
488
|
-
if (padding) spans.push(span(padding));
|
|
524
|
+
if (padding !== "") spans.push(span(padding));
|
|
489
525
|
out.push(spans);
|
|
490
526
|
}
|
|
491
527
|
return out;
|
|
@@ -526,7 +562,7 @@ export const buildSuggestionPopup = (
|
|
|
526
562
|
const marker = isSelected ? "→ " : " ";
|
|
527
563
|
const label = item.label;
|
|
528
564
|
const description = item.description?.replace(/\r\n|\r|\n/g, " ");
|
|
529
|
-
if (description && renderWidth > 40) {
|
|
565
|
+
if (isNonEmptyString(description) && renderWidth > 40) {
|
|
530
566
|
const primary = Math.max(1, Math.min(32, renderWidth - 6));
|
|
531
567
|
const maxPrimary = Math.max(1, primary - 2);
|
|
532
568
|
const truncatedLabel = truncateToWidth(label, maxPrimary);
|
package/src/core/services.ts
CHANGED
|
@@ -29,10 +29,11 @@ export interface CompletionState {
|
|
|
29
29
|
export class Keyboard extends Context.Service<
|
|
30
30
|
Keyboard,
|
|
31
31
|
{
|
|
32
|
-
/** Wait for the next key event from the user.
|
|
33
|
-
|
|
32
|
+
/** Wait for the next key event from the user. Effect is already lazy, so the
|
|
33
|
+
* member is the Effect itself, not a factory returning one. */
|
|
34
|
+
readonly nextKey: Effect.Effect<KeyEvent>;
|
|
34
35
|
}
|
|
35
|
-
>()("inquiry/core/services/Keyboard") {}
|
|
36
|
+
>()("@ftrdotdev/pi-inquiry/core/services/Keyboard") {}
|
|
36
37
|
|
|
37
38
|
export class Renderer extends Context.Service<
|
|
38
39
|
Renderer,
|
|
@@ -42,7 +43,7 @@ export class Renderer extends Context.Service<
|
|
|
42
43
|
/** Resolve the interaction with a final result. */
|
|
43
44
|
complete(result: QuestionResult): Effect.Effect<void>;
|
|
44
45
|
}
|
|
45
|
-
>()("inquiry/core/services/Renderer") {}
|
|
46
|
+
>()("@ftrdotdev/pi-inquiry/core/services/Renderer") {}
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
49
|
* File-reference completion for the inline editor (the `@`-token popup).
|
|
@@ -78,4 +79,4 @@ export class FileSuggestions extends Context.Service<
|
|
|
78
79
|
prefix: string,
|
|
79
80
|
): Effect.Effect<{ readonly text: string; readonly cursor: number }>;
|
|
80
81
|
}
|
|
81
|
-
>()("inquiry/core/services/FileSuggestions") {}
|
|
82
|
+
>()("@ftrdotdev/pi-inquiry/core/services/FileSuggestions") {}
|
package/src/core/text.test.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
1
|
+
import { assert, describe, expect, it } from "@effect/vitest";
|
|
2
|
+
import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
|
|
3
|
+
import { permittedTextArbitrary, widthArbitrary } from "../../test-support/generators.ts";
|
|
2
4
|
import { charWidth, stripLeadingEmoji, truncateToWidth, visibleWidth, wrapText } from "./text.ts";
|
|
3
5
|
|
|
4
6
|
describe("visibleWidth", () => {
|
|
@@ -90,16 +92,6 @@ describe("stripLeadingEmoji", () => {
|
|
|
90
92
|
expect(stripLeadingEmoji("⚠️ careful")).toBe("careful");
|
|
91
93
|
});
|
|
92
94
|
|
|
93
|
-
it("strips a run of emoji, not just one", () => {
|
|
94
|
-
expect(stripLeadingEmoji("➡️ ➡️ twice")).toBe("twice");
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("strips from each line independently", () => {
|
|
98
|
-
expect(stripLeadingEmoji("❓ Question?\n\n➡️ Recommended: A.")).toBe(
|
|
99
|
-
"Question?\n\nRecommended: A.",
|
|
100
|
-
);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
95
|
it("leaves non-emoji prefixes alone", () => {
|
|
104
96
|
expect(stripLeadingEmoji("→ text")).toBe("→ text");
|
|
105
97
|
expect(stripLeadingEmoji("• bullet")).toBe("• bullet");
|
|
@@ -125,10 +117,6 @@ describe("wrapText", () => {
|
|
|
125
117
|
expect(wrapText("one two three", 7)).toEqual(["one two", "three"]);
|
|
126
118
|
});
|
|
127
119
|
|
|
128
|
-
it("breaks long words character by character", () => {
|
|
129
|
-
expect(wrapText("abcdefghij", 4)).toEqual(["abcd", "efgh", "ij"]);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
120
|
it("trims trailing whitespace", () => {
|
|
133
121
|
expect(wrapText("one two ", 7)).toEqual(["one two"]);
|
|
134
122
|
});
|
|
@@ -153,3 +141,44 @@ describe("wrapText", () => {
|
|
|
153
141
|
expect(wrapText("あああ", 4)).toEqual(["ああ", "あ"]);
|
|
154
142
|
});
|
|
155
143
|
});
|
|
144
|
+
|
|
145
|
+
it.prop(
|
|
146
|
+
"truncation stays within the target width",
|
|
147
|
+
{ text: permittedTextArbitrary, width: widthArbitrary },
|
|
148
|
+
({ text, width }) => {
|
|
149
|
+
assert.isAtMost(visibleWidth(truncateToWidth(text, width)), width);
|
|
150
|
+
},
|
|
151
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
it.prop(
|
|
155
|
+
"truncation is idempotent",
|
|
156
|
+
{ text: permittedTextArbitrary, width: widthArbitrary },
|
|
157
|
+
({ text, width }) => {
|
|
158
|
+
assert.strictEqual(
|
|
159
|
+
truncateToWidth(truncateToWidth(text, width), width),
|
|
160
|
+
truncateToWidth(text, width),
|
|
161
|
+
);
|
|
162
|
+
},
|
|
163
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
it.prop(
|
|
167
|
+
"every wrapped line is within the width",
|
|
168
|
+
{ text: permittedTextArbitrary, width: widthArbitrary },
|
|
169
|
+
({ text, width }) => {
|
|
170
|
+
for (const line of wrapText(text, width)) assert.isAtMost(visibleWidth(line), width);
|
|
171
|
+
},
|
|
172
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
it.prop(
|
|
176
|
+
"stripping leading emoji is idempotent and preserves newlines",
|
|
177
|
+
{ text: permittedTextArbitrary },
|
|
178
|
+
({ text }) => {
|
|
179
|
+
const once = stripLeadingEmoji(text);
|
|
180
|
+
assert.strictEqual(stripLeadingEmoji(once), once);
|
|
181
|
+
assert.strictEqual(once.split("\n").length, text.split(/\r\n|\r|\n/).length);
|
|
182
|
+
},
|
|
183
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
184
|
+
);
|
package/src/core/text.ts
CHANGED
|
@@ -45,6 +45,13 @@ const graphemeWidth = (segment: string): number => {
|
|
|
45
45
|
const isEmoji = (segment: string): boolean => couldBeEmoji(segment) && rgiEmojiRegex.test(segment);
|
|
46
46
|
|
|
47
47
|
/** Width in terminal columns of a single character (code point). */
|
|
48
|
+
/**
|
|
49
|
+
* True when a string carries text. A decoded optional field is
|
|
50
|
+
* `string | undefined`, and the empty string means the same thing: absent.
|
|
51
|
+
*/
|
|
52
|
+
export const isNonEmptyString = (value: string | undefined): value is string =>
|
|
53
|
+
value !== undefined && value !== "";
|
|
54
|
+
|
|
48
55
|
export const charWidth = (ch: string): number => {
|
|
49
56
|
const cp = ch.codePointAt(0) ?? 0;
|
|
50
57
|
if (cp === 0x09) return 3; // tab
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { expect, it, vi } from "@effect/vitest";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { createVitest } from "vitest/node";
|
|
4
|
+
|
|
5
|
+
const PACKAGE_ROOT = fileURLToPath(new URL("..", import.meta.url));
|
|
6
|
+
const PROBE = "PI_HARNESS_PROBE";
|
|
7
|
+
|
|
8
|
+
it("this package resolves the shared policy although a second consumer states no key", async () => {
|
|
9
|
+
const vitest = await createVitest({ root: PACKAGE_ROOT, watch: false });
|
|
10
|
+
try {
|
|
11
|
+
const project = vitest.projects.at(0);
|
|
12
|
+
expect(project?.config.name).toBe("inquiry");
|
|
13
|
+
expect(project?.config.restoreMocks).toBe(true);
|
|
14
|
+
expect(project?.config.unstubEnvs).toBe(true);
|
|
15
|
+
expect(project?.config.unstubGlobals).toBe(true);
|
|
16
|
+
} finally {
|
|
17
|
+
await vitest.close();
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("a stubbed environment variable is set for the test that stubs it", () => {
|
|
22
|
+
vi.stubEnv(PROBE, "1");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("a stubbed environment variable is absent in the next test", () => {
|
|
26
|
+
expect(process.env[PROBE]).toBeUndefined();
|
|
27
|
+
});
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* editor box and the tui/tracker/preview boxes diverge visually.
|
|
11
11
|
*/
|
|
12
12
|
import { assert, it } from "@effect/vitest";
|
|
13
|
-
import
|
|
13
|
+
import { themeDouble } from "@ftrdotdev/pi-test-kit";
|
|
14
14
|
import type { Component } from "@earendil-works/pi-tui";
|
|
15
15
|
import { makeBorderedBox } from "@ftrdotdev/pi-tui";
|
|
16
16
|
import { buildBoxFrame } from "@ftrdotdev/pi-tui/box-frame";
|
|
@@ -18,12 +18,7 @@ import type { EditorState } from "../core/editor.ts";
|
|
|
18
18
|
import { buildEditorBox, buildEditorRows } from "../core/scene.ts";
|
|
19
19
|
import { paintScene } from "./painter.ts";
|
|
20
20
|
|
|
21
|
-
const identityTheme = {
|
|
22
|
-
fg: (_color: string, text: string): string => text,
|
|
23
|
-
inverse: (text: string): string => text,
|
|
24
|
-
bold: (text: string): string => text,
|
|
25
|
-
style: (text: string): string => text,
|
|
26
|
-
} as unknown as Theme;
|
|
21
|
+
const identityTheme = themeDouble({ inverse: (text) => text });
|
|
27
22
|
|
|
28
23
|
const THEMED = [
|
|
29
24
|
{ text: "", cursor: 0 },
|
package/src/sdk/keys.test.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
1
|
+
import { assert, describe, expect, it } from "@effect/vitest";
|
|
2
|
+
import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
|
|
3
|
+
import {
|
|
4
|
+
permittedTextArbitrary,
|
|
5
|
+
refusedControlByteArbitrary,
|
|
6
|
+
} from "../../test-support/generators.ts";
|
|
2
7
|
import { parseKey } from "./keys.ts";
|
|
3
8
|
import { KeyEvent } from "../core/keyboard.ts";
|
|
4
9
|
|
|
@@ -57,3 +62,21 @@ describe("parseKey", () => {
|
|
|
57
62
|
expect(parseKey("\x1b[1;5C")).toBeNull(); // ctrl+right, unsupported
|
|
58
63
|
});
|
|
59
64
|
});
|
|
65
|
+
|
|
66
|
+
it.prop(
|
|
67
|
+
"a bracketed paste of any text parses back to that text",
|
|
68
|
+
{ text: permittedTextArbitrary },
|
|
69
|
+
({ text }) => {
|
|
70
|
+
assert.deepStrictEqual(parseKey(`\x1b[200~${text}\x1b[201~`), KeyEvent.paste(text));
|
|
71
|
+
},
|
|
72
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
it.prop(
|
|
76
|
+
"a lone control byte yields no event",
|
|
77
|
+
{ byte: refusedControlByteArbitrary },
|
|
78
|
+
({ byte }) => {
|
|
79
|
+
assert.strictEqual(parseKey(String.fromCharCode(byte)), null);
|
|
80
|
+
},
|
|
81
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
82
|
+
);
|
package/src/sdk/painter.ts
CHANGED
|
@@ -25,10 +25,10 @@ const paintSpan = (span: Span, theme: Theme): string => {
|
|
|
25
25
|
// One `theme.style()` call per span: fg (or the cursor's inverse) and bold
|
|
26
26
|
// combine into a single SGR wrapper instead of nested fg(bold(text)).
|
|
27
27
|
if (span.style === "cursor") {
|
|
28
|
-
return theme.style(span.text, { inverse: true, bold: span.bold });
|
|
28
|
+
return theme.style(span.text, { inverse: true, bold: span.bold ?? false });
|
|
29
29
|
}
|
|
30
30
|
if (span.style !== undefined) {
|
|
31
|
-
return theme.style(span.text, { fg: span.style, bold: span.bold });
|
|
31
|
+
return theme.style(span.text, { fg: span.style, bold: span.bold ?? false });
|
|
32
32
|
}
|
|
33
|
-
return span.bold ? theme.bold(span.text) : span.text;
|
|
33
|
+
return (span.bold ?? false) ? theme.bold(span.text) : span.text;
|
|
34
34
|
};
|
package/src/sdk/pi-ui.test.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { themeDouble } from "@ftrdotdev/pi-test-kit";
|
|
2
3
|
import type { ExtensionUIContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
3
4
|
import { KeybindingsManager, TUI_KEYBINDINGS } from "@earendil-works/pi-tui";
|
|
4
5
|
import { initialMachineState, step, type MachineState } from "../core/machine.ts";
|
|
@@ -15,27 +16,26 @@ import {
|
|
|
15
16
|
describe("makeSceneRenderer cache", () => {
|
|
16
17
|
const fixture = (): { state: MachineState; theme: Theme; paints: () => number } => {
|
|
17
18
|
let count = 0;
|
|
18
|
-
const theme = {
|
|
19
|
-
fg: (_color
|
|
19
|
+
const theme = themeDouble({
|
|
20
|
+
fg: (_color, text) => {
|
|
20
21
|
count++;
|
|
21
22
|
return text;
|
|
22
23
|
},
|
|
23
|
-
inverse: (text
|
|
24
|
+
inverse: (text) => {
|
|
24
25
|
count++;
|
|
25
26
|
return text;
|
|
26
27
|
},
|
|
27
|
-
|
|
28
|
-
style: (text: string): string => {
|
|
28
|
+
style: (text) => {
|
|
29
29
|
count++;
|
|
30
30
|
return text;
|
|
31
31
|
},
|
|
32
|
-
}
|
|
32
|
+
});
|
|
33
33
|
const state = initialMachineState([
|
|
34
|
-
|
|
34
|
+
Question.make({
|
|
35
35
|
id: "q1",
|
|
36
36
|
label: "Q1",
|
|
37
37
|
prompt: "Pick one?",
|
|
38
|
-
options: [
|
|
38
|
+
options: [Option.make({ label: "Yes" }), Option.make({ label: "No" })],
|
|
39
39
|
allowOther: true,
|
|
40
40
|
multiple: false,
|
|
41
41
|
}),
|
|
@@ -96,10 +96,12 @@ describe("runQuestionUi input routing", () => {
|
|
|
96
96
|
const makeFakeUi = (): {
|
|
97
97
|
ui: ExtensionUIContext;
|
|
98
98
|
component: () => { handleInput: (data: string) => void };
|
|
99
|
+
customOptions: () => unknown;
|
|
99
100
|
setToolsExpanded: ReturnType<typeof vi.fn>;
|
|
100
101
|
} => {
|
|
101
102
|
let expanded = false;
|
|
102
103
|
let component: { handleInput: (data: string) => void } | undefined;
|
|
104
|
+
let customOptions: unknown;
|
|
103
105
|
// Mirror the app's KEYBINDINGS: TUI defaults plus the app.tools.expand
|
|
104
106
|
// action (default ctrl+o), so `matches` has real semantics.
|
|
105
107
|
const keybindings = new KeybindingsManager({
|
|
@@ -114,7 +116,9 @@ describe("runQuestionUi input routing", () => {
|
|
|
114
116
|
injectedKeybindings: unknown,
|
|
115
117
|
done: () => void,
|
|
116
118
|
) => unknown,
|
|
119
|
+
options: unknown,
|
|
117
120
|
) => {
|
|
121
|
+
customOptions = options;
|
|
118
122
|
component = factory({ requestRender: () => {} }, {}, keybindings, () => {}) as {
|
|
119
123
|
handleInput: (data: string) => void;
|
|
120
124
|
};
|
|
@@ -128,21 +132,46 @@ describe("runQuestionUi input routing", () => {
|
|
|
128
132
|
return {
|
|
129
133
|
ui: ui as unknown as ExtensionUIContext,
|
|
130
134
|
component: () => component!,
|
|
135
|
+
customOptions: () => customOptions,
|
|
131
136
|
setToolsExpanded: ui.setToolsExpanded as unknown as ReturnType<typeof vi.fn>,
|
|
132
137
|
};
|
|
133
138
|
};
|
|
134
139
|
|
|
135
140
|
const questions = [
|
|
136
|
-
|
|
141
|
+
Question.make({
|
|
137
142
|
id: "q1",
|
|
138
143
|
label: "Q1",
|
|
139
144
|
prompt: "Pick one?",
|
|
140
|
-
options: [
|
|
145
|
+
options: [Option.make({ label: "Yes" })],
|
|
141
146
|
allowOther: true,
|
|
142
147
|
multiple: false,
|
|
143
148
|
}),
|
|
144
149
|
];
|
|
145
150
|
|
|
151
|
+
it("reports blocked program status while a question waits", () => {
|
|
152
|
+
const { ui, customOptions } = makeFakeUi();
|
|
153
|
+
void runQuestionUi(ui, questions, "/tmp");
|
|
154
|
+
expect(customOptions()).toEqual({
|
|
155
|
+
blocked: { kind: "question", message: "Answer the question" },
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("uses a count for several questions", () => {
|
|
160
|
+
const { ui, customOptions } = makeFakeUi();
|
|
161
|
+
const second = Question.make({
|
|
162
|
+
id: "q2",
|
|
163
|
+
label: "Q2",
|
|
164
|
+
prompt: "Pick another?",
|
|
165
|
+
options: [Option.make({ label: "Yes" })],
|
|
166
|
+
allowOther: true,
|
|
167
|
+
multiple: false,
|
|
168
|
+
});
|
|
169
|
+
void runQuestionUi(ui, [...questions, second], "/tmp");
|
|
170
|
+
expect(customOptions()).toEqual({
|
|
171
|
+
blocked: { kind: "question", message: "Answer 2 questions" },
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
146
175
|
it("toggles tool output expansion on ctrl+o while the questionnaire is open", () => {
|
|
147
176
|
const { ui, component, setToolsExpanded } = makeFakeUi();
|
|
148
177
|
void runQuestionUi(ui, questions, "/tmp");
|
|
@@ -198,11 +227,11 @@ describe("cursor unit conversion", () => {
|
|
|
198
227
|
|
|
199
228
|
describe("questionClipboardText", () => {
|
|
200
229
|
const question = (allowOther: boolean): Question =>
|
|
201
|
-
|
|
230
|
+
Question.make({
|
|
202
231
|
id: "q1",
|
|
203
232
|
label: "Q1",
|
|
204
233
|
prompt: "Pick one?",
|
|
205
|
-
options: [
|
|
234
|
+
options: [Option.make({ label: "First" }), Option.make({ label: "Second" })],
|
|
206
235
|
allowOther,
|
|
207
236
|
multiple: false,
|
|
208
237
|
});
|
|
@@ -217,6 +246,21 @@ describe("questionClipboardText", () => {
|
|
|
217
246
|
expect(text.endsWith("3. Type something.")).toBe(true);
|
|
218
247
|
});
|
|
219
248
|
|
|
249
|
+
it("ignores the recommended flag", () => {
|
|
250
|
+
const q = Question.make({
|
|
251
|
+
id: "q1",
|
|
252
|
+
label: "Q1",
|
|
253
|
+
prompt: "Pick one?",
|
|
254
|
+
options: [
|
|
255
|
+
Option.make({ label: "First", recommended: true }),
|
|
256
|
+
Option.make({ label: "Second" }),
|
|
257
|
+
],
|
|
258
|
+
allowOther: false,
|
|
259
|
+
multiple: false,
|
|
260
|
+
});
|
|
261
|
+
expect(questionClipboardText(initialMachineState([q]))).toBe("Pick one?\n1. First\n2. Second");
|
|
262
|
+
});
|
|
263
|
+
|
|
220
264
|
it("is empty on the submit tab", () => {
|
|
221
265
|
const state = { ...initialMachineState([question(false)]), currentTab: 1 };
|
|
222
266
|
expect(questionClipboardText(state)).toBe("");
|