@ftrdotdev/pi-inquiry 0.2.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/LICENSE +21 -0
- package/README.md +161 -0
- package/package.json +68 -0
- package/src/core/domain.test.ts +154 -0
- package/src/core/domain.ts +149 -0
- package/src/core/editor.test.ts +124 -0
- package/src/core/editor.ts +78 -0
- package/src/core/flow.test.ts +445 -0
- package/src/core/flow.ts +98 -0
- package/src/core/keyboard.test.ts +44 -0
- package/src/core/keyboard.ts +59 -0
- package/src/core/machine.test.ts +438 -0
- package/src/core/machine.ts +378 -0
- package/src/core/scene.test.ts +385 -0
- package/src/core/scene.ts +555 -0
- package/src/core/services.ts +81 -0
- package/src/core/text.test.ts +77 -0
- package/src/core/text.ts +207 -0
- package/src/sdk/box-parity.test.ts +76 -0
- package/src/sdk/keys.test.ts +59 -0
- package/src/sdk/keys.ts +77 -0
- package/src/sdk/painter.ts +33 -0
- package/src/sdk/pi-ui.test.ts +185 -0
- package/src/sdk/pi-ui.ts +210 -0
- package/src/sdk/schema.test.ts +67 -0
- package/src/tool/question.test.ts +70 -0
- package/src/tool/question.ts +176 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { charWidth, visibleWidth, wrapText } from "./text.ts";
|
|
3
|
+
|
|
4
|
+
describe("visibleWidth", () => {
|
|
5
|
+
it("counts ASCII as one column per character", () => {
|
|
6
|
+
expect(visibleWidth("hello")).toBe(5);
|
|
7
|
+
expect(visibleWidth("")).toBe(0);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("counts wide CJK characters as two columns", () => {
|
|
11
|
+
expect(visibleWidth("日本語")).toBe(6);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("counts tabs as three columns", () => {
|
|
15
|
+
expect(visibleWidth("\t")).toBe(3);
|
|
16
|
+
expect(visibleWidth("a\tb")).toBe(5);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("ignores control characters", () => {
|
|
20
|
+
expect(visibleWidth("\x1b")).toBe(0);
|
|
21
|
+
// the core never strips ANSI sequences; only the escape byte itself is zero-width
|
|
22
|
+
expect(visibleWidth("\x1b[31m")).toBe(4);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("counts emoji as two columns", () => {
|
|
26
|
+
expect(visibleWidth("😀")).toBe(2);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("counts combining marks as zero", () => {
|
|
30
|
+
expect(visibleWidth("e\u0301")).toBe(1);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("charWidth", () => {
|
|
35
|
+
it("handles zero-width joiner and variation selectors", () => {
|
|
36
|
+
expect(charWidth("\u200d")).toBe(0);
|
|
37
|
+
expect(charWidth("\ufe0f")).toBe(0);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("wrapText", () => {
|
|
42
|
+
it("returns the text unchanged when it fits", () => {
|
|
43
|
+
expect(wrapText("hello world", 20)).toEqual(["hello world"]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("wraps at word boundaries", () => {
|
|
47
|
+
expect(wrapText("one two three", 7)).toEqual(["one two", "three"]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("breaks long words character by character", () => {
|
|
51
|
+
expect(wrapText("abcdefghij", 4)).toEqual(["abcd", "efgh", "ij"]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("trims trailing whitespace", () => {
|
|
55
|
+
expect(wrapText("one two ", 7)).toEqual(["one two"]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("does not start a line with whitespace", () => {
|
|
59
|
+
expect(wrapText("one two three", 6)).toEqual(["one", "two", "three"]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("splits on literal newlines", () => {
|
|
63
|
+
expect(wrapText("one\ntwo three", 20)).toEqual(["one", "two three"]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("returns an empty line for empty input", () => {
|
|
67
|
+
expect(wrapText("", 10)).toEqual([""]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("handles width one", () => {
|
|
71
|
+
expect(wrapText("ab", 1)).toEqual(["a", "b"]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("handles wide characters in wrapping", () => {
|
|
75
|
+
expect(wrapText("あああ", 4)).toEqual(["ああ", "あ"]);
|
|
76
|
+
});
|
|
77
|
+
});
|
package/src/core/text.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure text measurement and wrapping helpers.
|
|
3
|
+
*
|
|
4
|
+
* The core never deals with ANSI codes or terminal themes, only plain strings
|
|
5
|
+
* and their visible widths. Styling is applied later by the fringe painter.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Width in terminal columns of a single character (code point). */
|
|
9
|
+
export const charWidth = (ch: string): number => {
|
|
10
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
11
|
+
if (cp === 0x09) return 3; // tab
|
|
12
|
+
if (cp < 0x20 || (cp >= 0x7f && cp <= 0x9f)) return 0; // control
|
|
13
|
+
if (cp === 0x200d) return 0; // zero-width joiner
|
|
14
|
+
if ((cp >= 0xfe00 && cp <= 0xfe0f) || (cp >= 0xe0100 && cp <= 0xe01ef)) return 0; // variation selectors
|
|
15
|
+
if (/\p{Mark}/u.test(ch)) return 0; // combining marks
|
|
16
|
+
if (isWide(cp)) return 2;
|
|
17
|
+
return 1;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** Visible width of a string in terminal columns. */
|
|
21
|
+
export const visibleWidth = (text: string): number => {
|
|
22
|
+
let width = 0;
|
|
23
|
+
for (const ch of text) {
|
|
24
|
+
width += charWidth(ch);
|
|
25
|
+
}
|
|
26
|
+
return width;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Truncate `text` to at most `width` visible columns, cutting whole characters. */
|
|
30
|
+
export const truncateToWidth = (text: string, width: number): string => {
|
|
31
|
+
if (width <= 0) return "";
|
|
32
|
+
if (visibleWidth(text) <= width) return text;
|
|
33
|
+
let out = "";
|
|
34
|
+
let used = 0;
|
|
35
|
+
for (const ch of text) {
|
|
36
|
+
const w = charWidth(ch);
|
|
37
|
+
if (used + w > width) break;
|
|
38
|
+
out += ch;
|
|
39
|
+
used += w;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Wrap plain text to a maximum visible width per line.
|
|
46
|
+
*
|
|
47
|
+
* Word-aware: whitespace runs separate tokens; tokens longer than the width
|
|
48
|
+
* are broken character by character. Trailing whitespace is trimmed. Literal
|
|
49
|
+
* newlines split lines. Mirrors the wrapping semantics of pi-tui's
|
|
50
|
+
* `wrapTextWithAnsi` for unstyled text.
|
|
51
|
+
*/
|
|
52
|
+
export const wrapText = (text: string, width: number): readonly string[] => {
|
|
53
|
+
if (width <= 0) return [""];
|
|
54
|
+
const out: string[] = [];
|
|
55
|
+
for (const segment of text.split(/\r\n|\r|\n/)) {
|
|
56
|
+
out.push(...wrapSegment(segment, width));
|
|
57
|
+
}
|
|
58
|
+
return out.length > 0 ? out : [""];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const wrapSegment = (segment: string, width: number): readonly string[] => {
|
|
62
|
+
if (segment === "") return [""];
|
|
63
|
+
if (visibleWidth(segment) <= width) return [segment];
|
|
64
|
+
const tokens = segment.split(/(\s+)/);
|
|
65
|
+
const lines: string[] = [];
|
|
66
|
+
let current = "";
|
|
67
|
+
let currentWidth = 0;
|
|
68
|
+
for (const token of tokens) {
|
|
69
|
+
const tokenWidth = visibleWidth(token);
|
|
70
|
+
const isWhitespace = token.trim() === "";
|
|
71
|
+
if (tokenWidth > width && !isWhitespace) {
|
|
72
|
+
if (current !== "") {
|
|
73
|
+
lines.push(current.trimEnd());
|
|
74
|
+
current = "";
|
|
75
|
+
currentWidth = 0;
|
|
76
|
+
}
|
|
77
|
+
const broken = breakLongToken(token, width);
|
|
78
|
+
for (let i = 0; i < broken.length - 1; i++) lines.push(broken[i]!);
|
|
79
|
+
current = broken[broken.length - 1]!;
|
|
80
|
+
currentWidth = visibleWidth(current);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (currentWidth + tokenWidth > width && current !== "") {
|
|
84
|
+
lines.push(current.trimEnd());
|
|
85
|
+
current = isWhitespace ? "" : token;
|
|
86
|
+
currentWidth = isWhitespace ? 0 : tokenWidth;
|
|
87
|
+
} else {
|
|
88
|
+
if (current === "" && isWhitespace) continue;
|
|
89
|
+
current += token;
|
|
90
|
+
currentWidth += tokenWidth;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (current !== "") lines.push(current.trimEnd());
|
|
94
|
+
return lines.length > 0 ? lines : [""];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const breakLongToken = (token: string, width: number): readonly string[] => {
|
|
98
|
+
const lines: string[] = [];
|
|
99
|
+
let current = "";
|
|
100
|
+
let currentWidth = 0;
|
|
101
|
+
for (const ch of token) {
|
|
102
|
+
const w = visibleWidth(ch);
|
|
103
|
+
if (currentWidth + w > width && current !== "") {
|
|
104
|
+
lines.push(current);
|
|
105
|
+
current = "";
|
|
106
|
+
currentWidth = 0;
|
|
107
|
+
}
|
|
108
|
+
current += ch;
|
|
109
|
+
currentWidth += w;
|
|
110
|
+
}
|
|
111
|
+
if (current !== "") lines.push(current);
|
|
112
|
+
return lines.length > 0 ? lines : [""];
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** East Asian wide/fullwidth and emoji ranges (wcwidth-style). */
|
|
116
|
+
const isWide = (cp: number): boolean =>
|
|
117
|
+
(cp >= 0x1100 && cp <= 0x115f) || // Hangul Jamo
|
|
118
|
+
(cp >= 0x231a && cp <= 0x231b) ||
|
|
119
|
+
(cp >= 0x2329 && cp <= 0x232a) ||
|
|
120
|
+
(cp >= 0x23e9 && cp <= 0x23ec) ||
|
|
121
|
+
cp === 0x23f0 ||
|
|
122
|
+
cp === 0x23f3 ||
|
|
123
|
+
(cp >= 0x25fd && cp <= 0x25fe) ||
|
|
124
|
+
(cp >= 0x2614 && cp <= 0x2615) ||
|
|
125
|
+
(cp >= 0x2648 && cp <= 0x2653) ||
|
|
126
|
+
cp === 0x267f ||
|
|
127
|
+
cp === 0x2693 ||
|
|
128
|
+
cp === 0x26a1 ||
|
|
129
|
+
(cp >= 0x26aa && cp <= 0x26ab) ||
|
|
130
|
+
(cp >= 0x26bd && cp <= 0x26be) ||
|
|
131
|
+
(cp >= 0x26c4 && cp <= 0x26c5) ||
|
|
132
|
+
cp === 0x26ce ||
|
|
133
|
+
cp === 0x26d4 ||
|
|
134
|
+
cp === 0x26ea ||
|
|
135
|
+
(cp >= 0x26f2 && cp <= 0x26f3) ||
|
|
136
|
+
cp === 0x26f5 ||
|
|
137
|
+
cp === 0x26fa ||
|
|
138
|
+
cp === 0x26fd ||
|
|
139
|
+
cp === 0x2705 ||
|
|
140
|
+
(cp >= 0x270a && cp <= 0x270b) ||
|
|
141
|
+
cp === 0x2728 ||
|
|
142
|
+
cp === 0x274c ||
|
|
143
|
+
cp === 0x274e ||
|
|
144
|
+
(cp >= 0x2753 && cp <= 0x2755) ||
|
|
145
|
+
cp === 0x2757 ||
|
|
146
|
+
(cp >= 0x2795 && cp <= 0x2797) ||
|
|
147
|
+
cp === 0x27b0 ||
|
|
148
|
+
cp === 0x27bf ||
|
|
149
|
+
(cp >= 0x2b1b && cp <= 0x2b1c) ||
|
|
150
|
+
cp === 0x2b50 ||
|
|
151
|
+
cp === 0x2b55 ||
|
|
152
|
+
(cp >= 0x2e80 && cp <= 0x303e) || // CJK Radicals .. CJK Symbols and Punctuation
|
|
153
|
+
(cp >= 0x3041 && cp <= 0x33ff) || // Hiragana .. CJK Compatibility
|
|
154
|
+
(cp >= 0x3400 && cp <= 0x4dbf) || // CJK Extension A
|
|
155
|
+
(cp >= 0x4e00 && cp <= 0x9fff) || // CJK Unified Ideographs
|
|
156
|
+
(cp >= 0xa000 && cp <= 0xa4cf) || // Yi
|
|
157
|
+
(cp >= 0xa960 && cp <= 0xa97f) ||
|
|
158
|
+
(cp >= 0xac00 && cp <= 0xd7a3) || // Hangul syllables
|
|
159
|
+
(cp >= 0xf900 && cp <= 0xfaff) ||
|
|
160
|
+
(cp >= 0xfe10 && cp <= 0xfe19) ||
|
|
161
|
+
(cp >= 0xfe30 && cp <= 0xfe6f) ||
|
|
162
|
+
(cp >= 0xff00 && cp <= 0xff60) || // Fullwidth forms
|
|
163
|
+
(cp >= 0xffe0 && cp <= 0xffe6) ||
|
|
164
|
+
cp === 0x1f004 ||
|
|
165
|
+
cp === 0x1f0cf ||
|
|
166
|
+
cp === 0x1f18e ||
|
|
167
|
+
(cp >= 0x1f191 && cp <= 0x1f19a) ||
|
|
168
|
+
(cp >= 0x1f1e6 && cp <= 0x1f1ff) || // regional indicators
|
|
169
|
+
(cp >= 0x1f200 && cp <= 0x1f320) ||
|
|
170
|
+
(cp >= 0x1f32d && cp <= 0x1f335) ||
|
|
171
|
+
(cp >= 0x1f337 && cp <= 0x1f37c) ||
|
|
172
|
+
(cp >= 0x1f37e && cp <= 0x1f393) ||
|
|
173
|
+
(cp >= 0x1f3a0 && cp <= 0x1f3ca) ||
|
|
174
|
+
(cp >= 0x1f3cf && cp <= 0x1f3d3) ||
|
|
175
|
+
(cp >= 0x1f3e0 && cp <= 0x1f3f0) ||
|
|
176
|
+
cp === 0x1f3f4 ||
|
|
177
|
+
(cp >= 0x1f3f8 && cp <= 0x1f43e) ||
|
|
178
|
+
cp === 0x1f440 ||
|
|
179
|
+
(cp >= 0x1f442 && cp <= 0x1f4fc) ||
|
|
180
|
+
(cp >= 0x1f4ff && cp <= 0x1f53d) ||
|
|
181
|
+
(cp >= 0x1f54b && cp <= 0x1f54e) ||
|
|
182
|
+
(cp >= 0x1f550 && cp <= 0x1f567) ||
|
|
183
|
+
cp === 0x1f57a ||
|
|
184
|
+
(cp >= 0x1f595 && cp <= 0x1f596) ||
|
|
185
|
+
cp === 0x1f5a4 ||
|
|
186
|
+
(cp >= 0x1f5fb && cp <= 0x1f64f) ||
|
|
187
|
+
(cp >= 0x1f680 && cp <= 0x1f6c5) ||
|
|
188
|
+
cp === 0x1f6cc ||
|
|
189
|
+
(cp >= 0x1f6d0 && cp <= 0x1f6d2) ||
|
|
190
|
+
(cp >= 0x1f6d5 && cp <= 0x1f6d7) ||
|
|
191
|
+
(cp >= 0x1f6dc && cp <= 0x1f6df) ||
|
|
192
|
+
(cp >= 0x1f6eb && cp <= 0x1f6ec) ||
|
|
193
|
+
(cp >= 0x1f6f4 && cp <= 0x1f6fc) ||
|
|
194
|
+
(cp >= 0x1f7e0 && cp <= 0x1f7eb) ||
|
|
195
|
+
cp === 0x1f7f0 ||
|
|
196
|
+
(cp >= 0x1f90c && cp <= 0x1f93a) ||
|
|
197
|
+
(cp >= 0x1f93c && cp <= 0x1f945) ||
|
|
198
|
+
(cp >= 0x1f947 && cp <= 0x1f9ff) ||
|
|
199
|
+
(cp >= 0x1fa70 && cp <= 0x1fa7c) ||
|
|
200
|
+
(cp >= 0x1fa80 && cp <= 0x1fa88) ||
|
|
201
|
+
(cp >= 0x1fa90 && cp <= 0x1fabd) ||
|
|
202
|
+
(cp >= 0x1fabf && cp <= 0x1fac5) ||
|
|
203
|
+
cp === 0x1face ||
|
|
204
|
+
(cp >= 0x1fae0 && cp <= 0x1fae8) ||
|
|
205
|
+
(cp >= 0x1faf0 && cp <= 0x1faf8) ||
|
|
206
|
+
(cp >= 0x20000 && cp <= 0x2fffd) || // CJK Extensions B..
|
|
207
|
+
(cp >= 0x30000 && cp <= 0x3fffd);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locks the house box look between the pi-free core scene and the themed
|
|
3
|
+
* `BorderedBox` component.
|
|
4
|
+
*
|
|
5
|
+
* The core's `buildEditorBox` draws its frame from the same pure
|
|
6
|
+
* `@ftrdotdev/pi-tui/box-frame` model that `BorderedBox` wraps, so the two
|
|
7
|
+
* must render byte-identically once painted. This test assembles both paths
|
|
8
|
+
* for representative editors and asserts the equal output — if either the
|
|
9
|
+
* core wrapper or the component wrapper drifts from the shared frame, the
|
|
10
|
+
* editor box and the tui/tracker/preview boxes diverge visually.
|
|
11
|
+
*/
|
|
12
|
+
import { assert, it } from "@effect/vitest";
|
|
13
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
15
|
+
import { makeBorderedBox } from "@ftrdotdev/pi-tui";
|
|
16
|
+
import { buildBoxFrame } from "@ftrdotdev/pi-tui/box-frame";
|
|
17
|
+
import type { EditorState } from "../core/editor.ts";
|
|
18
|
+
import { buildEditorBox, buildEditorRows } from "../core/scene.ts";
|
|
19
|
+
import { paintScene } from "./painter.ts";
|
|
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
|
+
} as unknown as Theme;
|
|
26
|
+
|
|
27
|
+
const THEMED = [
|
|
28
|
+
{ text: "", cursor: 0 },
|
|
29
|
+
{ text: "ab", cursor: 1 },
|
|
30
|
+
{ text: "hello world", cursor: 5 },
|
|
31
|
+
{ text: "a\nb", cursor: 1 },
|
|
32
|
+
{ text: "one\ntwo", cursor: 7 },
|
|
33
|
+
{ text: "x".repeat(20), cursor: 20 },
|
|
34
|
+
] as const satisfies readonly EditorState[];
|
|
35
|
+
|
|
36
|
+
it("painted core editor box is byte-identical to BorderedBox for representative editors", () => {
|
|
37
|
+
for (const editor of THEMED) {
|
|
38
|
+
for (const width of [10, 24, 40]) {
|
|
39
|
+
const boxWidth = Math.max(4, width);
|
|
40
|
+
const painted = paintScene({ lines: buildEditorBox(editor, boxWidth) }, identityTheme);
|
|
41
|
+
|
|
42
|
+
const frame = buildBoxFrame(boxWidth, { paddingX: 1 });
|
|
43
|
+
assert.ok(frame, `frame should exist at width ${width}`);
|
|
44
|
+
const rows = buildEditorRows(editor, frame!.contentWidth);
|
|
45
|
+
const body: Component = {
|
|
46
|
+
render: () => paintScene({ lines: rows }, identityTheme),
|
|
47
|
+
invalidate: () => {},
|
|
48
|
+
};
|
|
49
|
+
const boxed = makeBorderedBox(body, identityTheme, { color: "border" }).render(boxWidth);
|
|
50
|
+
|
|
51
|
+
assert.deepStrictEqual(painted, boxed, `editor ${JSON.stringify(editor)} at width ${width}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("painted editor box matches BorderedBox output even when a label would fit", () => {
|
|
57
|
+
const editor: EditorState = { text: "hi", cursor: 2 };
|
|
58
|
+
const width = 40;
|
|
59
|
+
const painted = paintScene({ lines: buildEditorBox(editor, width) }, identityTheme);
|
|
60
|
+
|
|
61
|
+
const frame = buildBoxFrame(width, { paddingX: 1 });
|
|
62
|
+
assert.ok(frame);
|
|
63
|
+
const rows = buildEditorRows(editor, frame!.contentWidth);
|
|
64
|
+
const body: Component = {
|
|
65
|
+
render: () => paintScene({ lines: rows }, identityTheme),
|
|
66
|
+
invalidate: () => {},
|
|
67
|
+
};
|
|
68
|
+
// A label must not leak into the editor box: BorderedBox with a label that
|
|
69
|
+
// does not fit renders the same plain frame as the core.
|
|
70
|
+
const boxed = makeBorderedBox(body, identityTheme, {
|
|
71
|
+
color: "border",
|
|
72
|
+
label: "x".repeat(100),
|
|
73
|
+
}).render(width);
|
|
74
|
+
|
|
75
|
+
assert.deepStrictEqual(painted, boxed);
|
|
76
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { parseKey } from "./keys.ts";
|
|
3
|
+
import { KeyEvent } from "../core/keyboard.ts";
|
|
4
|
+
|
|
5
|
+
describe("parseKey", () => {
|
|
6
|
+
it("maps legacy arrow and navigation sequences", () => {
|
|
7
|
+
expect(parseKey("\x1b[A")).toEqual(KeyEvent.up);
|
|
8
|
+
expect(parseKey("\x1b[B")).toEqual(KeyEvent.down);
|
|
9
|
+
expect(parseKey("\x1b[C")).toEqual(KeyEvent.right);
|
|
10
|
+
expect(parseKey("\x1b[D")).toEqual(KeyEvent.left);
|
|
11
|
+
expect(parseKey("\x1b[H")).toEqual(KeyEvent.home);
|
|
12
|
+
expect(parseKey("\x1b[F")).toEqual(KeyEvent.end);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("maps tab, shift-tab, enter and escape", () => {
|
|
16
|
+
expect(parseKey("\t")).toEqual(KeyEvent.tab);
|
|
17
|
+
expect(parseKey("\x1b[Z")).toEqual(KeyEvent.shiftTab);
|
|
18
|
+
expect(parseKey("\r")).toEqual(KeyEvent.enter);
|
|
19
|
+
expect(parseKey("\x1b")).toEqual(KeyEvent.escape);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("maps shift+enter sequences to shiftEnter", () => {
|
|
23
|
+
expect(parseKey("\n")).toEqual(KeyEvent.shiftEnter);
|
|
24
|
+
expect(parseKey("\x1b\r")).toEqual(KeyEvent.shiftEnter);
|
|
25
|
+
expect(parseKey("\x1b[13;2~")).toEqual(KeyEvent.shiftEnter);
|
|
26
|
+
expect(parseKey("\x1b[13;2u")).toEqual(KeyEvent.shiftEnter);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("keeps plain enter as enter", () => {
|
|
30
|
+
expect(parseKey("\r")).toEqual(KeyEvent.enter);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("extracts bracketed paste content", () => {
|
|
34
|
+
expect(parseKey("\x1b[200~hello world\x1b[201~")).toEqual(KeyEvent.paste("hello world"));
|
|
35
|
+
expect(parseKey("\x1b[200~line1\nline2\x1b[201~")).toEqual(KeyEvent.paste("line1\nline2"));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("decodes CSI-u re-encoded control bytes inside pastes", () => {
|
|
39
|
+
// tmux re-encodes a newline (ctrl+j) inside bracketed paste as \x1b[106;5u
|
|
40
|
+
expect(parseKey("\x1b[200~a\x1b[106;5ub\x1b[201~")).toEqual(KeyEvent.paste("a\nb"));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("maps backspace and delete", () => {
|
|
44
|
+
expect(parseKey("\x7f")).toEqual(KeyEvent.backspace);
|
|
45
|
+
expect(parseKey("\x1b[3~")).toEqual(KeyEvent.delete);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("passes printable characters through as char events", () => {
|
|
49
|
+
expect(parseKey("a")).toEqual(KeyEvent.char("a"));
|
|
50
|
+
expect(parseKey("😀")).toEqual(KeyEvent.char("😀"));
|
|
51
|
+
expect(parseKey("paste")).toEqual(KeyEvent.char("paste"));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("rejects control input", () => {
|
|
55
|
+
expect(parseKey("\x03")).toBeNull(); // ctrl+c
|
|
56
|
+
expect(parseKey("\x00")).toBeNull();
|
|
57
|
+
expect(parseKey("\x1b[1;5C")).toBeNull(); // ctrl+right, unsupported
|
|
58
|
+
});
|
|
59
|
+
});
|
package/src/sdk/keys.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map raw pi-tui key data to core key events.
|
|
3
|
+
*
|
|
4
|
+
* The core never sees terminal escape sequences; this module translates them.
|
|
5
|
+
* Unparseable or control input returns `null` and is ignored by the adapter.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Key, matchesKey } from "@earendil-works/pi-tui";
|
|
9
|
+
import { KeyEvent } from "../core/keyboard.ts";
|
|
10
|
+
|
|
11
|
+
export const parseKey = (data: string): KeyEvent | null => {
|
|
12
|
+
// Bracketed paste: the terminal layer re-wraps pastes in \x1b[200~ ... \x1b[201~
|
|
13
|
+
// before they reach handleInput. Must be checked before key matching.
|
|
14
|
+
const paste = extractPaste(data);
|
|
15
|
+
if (paste !== null) return KeyEvent.paste(paste);
|
|
16
|
+
|
|
17
|
+
if (matchesKey(data, Key.up)) return KeyEvent.up;
|
|
18
|
+
if (matchesKey(data, Key.down)) return KeyEvent.down;
|
|
19
|
+
if (matchesKey(data, Key.left)) return KeyEvent.left;
|
|
20
|
+
if (matchesKey(data, Key.right)) return KeyEvent.right;
|
|
21
|
+
|
|
22
|
+
// Shift+Enter must be matched before Enter: with the Kitty keyboard protocol
|
|
23
|
+
// active, a bare \n is Ghostty's shift+enter mapping, and the pi editor treats
|
|
24
|
+
// \x1b\r / \x1b[13;2~ / bare \n as newline input.
|
|
25
|
+
if (
|
|
26
|
+
matchesKey(data, Key.shift("enter")) ||
|
|
27
|
+
data === "\x1b\r" ||
|
|
28
|
+
data === "\x1b[13;2~" ||
|
|
29
|
+
(data.length > 1 && data.includes("\x1b") && data.includes("\r")) ||
|
|
30
|
+
data === "\n"
|
|
31
|
+
) {
|
|
32
|
+
return KeyEvent.shiftEnter;
|
|
33
|
+
}
|
|
34
|
+
if (matchesKey(data, Key.enter)) return KeyEvent.enter;
|
|
35
|
+
if (matchesKey(data, Key.shift("tab"))) return KeyEvent.shiftTab;
|
|
36
|
+
if (matchesKey(data, Key.tab)) return KeyEvent.tab;
|
|
37
|
+
if (matchesKey(data, Key.escape)) return KeyEvent.escape;
|
|
38
|
+
if (matchesKey(data, Key.backspace)) return KeyEvent.backspace;
|
|
39
|
+
if (matchesKey(data, Key.delete)) return KeyEvent.delete;
|
|
40
|
+
if (matchesKey(data, Key.home)) return KeyEvent.home;
|
|
41
|
+
if (matchesKey(data, Key.end)) return KeyEvent.end;
|
|
42
|
+
if (data.length > 0 && !containsControl(data)) return KeyEvent.char(data);
|
|
43
|
+
return null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const BRACKETED_PASTE_START = "\x1b[200~";
|
|
47
|
+
const BRACKETED_PASTE_END = "\x1b[201~";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Extract the content of a bracketed paste, or null when `data` is not one.
|
|
51
|
+
*
|
|
52
|
+
* Control bytes inside paste content may arrive re-encoded as CSI-u Ctrl+<letter>
|
|
53
|
+
* sequences (e.g. tmux with extended-keys-format=csi-u); decode those back to
|
|
54
|
+
* their literal byte so newlines survive (the core sanitizer strips the rest).
|
|
55
|
+
*/
|
|
56
|
+
const extractPaste = (data: string): string | null => {
|
|
57
|
+
const start = data.indexOf(BRACKETED_PASTE_START);
|
|
58
|
+
if (start === -1) return null;
|
|
59
|
+
const end = data.indexOf(BRACKETED_PASTE_END, start);
|
|
60
|
+
if (end === -1) return null;
|
|
61
|
+
const content = data.slice(start + BRACKETED_PASTE_START.length, end);
|
|
62
|
+
// eslint-disable-next-line no-control-regex -- intentional: CSI-u ctrl re-encoding (matches pi-tui)
|
|
63
|
+
return content.replace(/\x1b\[(\d+);5u/g, (match, code: string) => {
|
|
64
|
+
const cp = Number(code);
|
|
65
|
+
if (cp >= 97 && cp <= 122) return String.fromCharCode(cp - 96);
|
|
66
|
+
if (cp >= 65 && cp <= 90) return String.fromCharCode(cp - 64);
|
|
67
|
+
return match;
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const containsControl = (data: string): boolean => {
|
|
72
|
+
for (const ch of data) {
|
|
73
|
+
const code = ch.codePointAt(0) ?? 0;
|
|
74
|
+
if (code < 0x20 || code === 0x7f) return true;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paint core scenes to ANSI lines using the pi theme.
|
|
3
|
+
*
|
|
4
|
+
* The only place the core's style palette becomes terminal colors. The core
|
|
5
|
+
* scene model guarantees lines are already wrapped; this module applies the
|
|
6
|
+
* theme and emits ready-to-render strings.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import type { Scene, Span } from "../core/scene.ts";
|
|
11
|
+
|
|
12
|
+
export const paintScene = (scene: Scene, theme: Theme): string[] => {
|
|
13
|
+
const lines: string[] = [];
|
|
14
|
+
for (const sceneLine of scene.lines) {
|
|
15
|
+
let out = "";
|
|
16
|
+
for (const span of sceneLine) {
|
|
17
|
+
out += paintSpan(span, theme);
|
|
18
|
+
}
|
|
19
|
+
lines.push(out);
|
|
20
|
+
}
|
|
21
|
+
return lines;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const paintSpan = (span: Span, theme: Theme): string => {
|
|
25
|
+
if (span.style === "cursor") {
|
|
26
|
+
const inner = span.bold ? theme.bold(span.text) : span.text;
|
|
27
|
+
return theme.inverse(inner);
|
|
28
|
+
}
|
|
29
|
+
let text = span.text;
|
|
30
|
+
if (span.bold) text = theme.bold(text);
|
|
31
|
+
if (span.style) return theme.fg(span.style, text);
|
|
32
|
+
return text;
|
|
33
|
+
};
|