pal-code 0.0.14 → 0.0.15
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/package.json +2 -2
- package/packages/cli/src/components/assistant-body.ts +107 -8
- package/packages/cli/src/components/permission-prompt.ts +35 -19
- package/packages/cli/src/components/sidebar.ts +39 -22
- package/packages/cli/src/index.ts +423 -20
- package/packages/cli/src/overlays/model-picker.ts +6 -6
- package/packages/cli/src/overlays/session-picker.ts +6 -6
- package/packages/cli/src/theme.ts +237 -71
- package/packages/cli/src/themes/github-dark.ts +174 -0
- package/packages/cli/src/themes/index.ts +36 -0
- package/packages/cli/src/themes/types.ts +192 -0
- package/packages/cli/src/themes/vanilla-pi.ts +175 -0
- package/packages/cli/src/tui/paint-panel.ts +20 -0
- package/packages/cli/src/tui/tty-width.ts +973 -0
- package/packages/cli/src/turn.ts +8 -0
- package/packages/cli/src/utils/code-block.ts +90 -13
- package/packages/cli/src/utils/format.ts +11 -7
- package/packages/cli/src/utils/highlight.ts +15 -15
- package/packages/cli/src/utils/osc-canvas.ts +102 -0
- package/packages/cli/src/utils/osc52.ts +32 -3
package/packages/cli/src/turn.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
7
|
getAllTools,
|
|
8
|
+
validateRequiredArgs,
|
|
8
9
|
registerExtensionTools,
|
|
9
10
|
executeTool,
|
|
10
11
|
registerExtensionHandlers,
|
|
@@ -200,6 +201,13 @@ async function gatedExecuteTool(
|
|
|
200
201
|
// If args can't be parsed, let executeTool handle the error
|
|
201
202
|
}
|
|
202
203
|
|
|
204
|
+
// Reject schema-incomplete calls before the permission gate, so a call
|
|
205
|
+
// missing required arguments can't trigger an approval prompt and then fail.
|
|
206
|
+
const validationError = validateRequiredArgs(name, args);
|
|
207
|
+
if (validationError) {
|
|
208
|
+
return {toolCallId, content: validationError, error: true};
|
|
209
|
+
}
|
|
210
|
+
|
|
203
211
|
const decision = await checkToolPermission(name, args);
|
|
204
212
|
|
|
205
213
|
if (!decision.allowed) {
|
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
* Fenced code blocks in the transcript: segmentation + boxed rendering.
|
|
3
3
|
*
|
|
4
4
|
* Both exports are pure and deterministic — same input bytes → same output
|
|
5
|
-
* bytes, no clock or randomness.
|
|
6
|
-
*
|
|
5
|
+
* bytes, no clock or randomness. Hover/success paint arrives as explicit
|
|
6
|
+
* `opts` parameters, never as ambient state, so the TUI can repaint
|
|
7
|
+
* differentially and the components can cache by width without desyncing
|
|
7
8
|
* the screen from the cache.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
|
-
import {truncateToWidth} from '
|
|
11
|
+
import {truncateToWidth} from '../tui/tty-width.js';
|
|
11
12
|
|
|
12
|
-
import {c} from '../theme.js';
|
|
13
|
+
import {c, codebox} from '../theme.js';
|
|
13
14
|
import {buildHeader, displayWidth, wrapText} from './format.js';
|
|
14
15
|
import {highlightCode} from './highlight.js';
|
|
15
16
|
|
|
@@ -18,6 +19,47 @@ export type CodeSegment =
|
|
|
18
19
|
| {readonly kind: 'text'; readonly text: string}
|
|
19
20
|
| {readonly kind: 'code'; readonly lang: string; readonly lines: readonly string[]};
|
|
20
21
|
|
|
22
|
+
/** Right-edge copy affordance drawn in rendered code-block headers. */
|
|
23
|
+
export const COPY_LABEL = '⧉ copy';
|
|
24
|
+
|
|
25
|
+
/** Header label once the copy succeeded — wider than COPY_LABEL, so the
|
|
26
|
+
* dash run and copy zone must recompute from it. */
|
|
27
|
+
export const SUCCESS_LABEL = '✓ copied';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Screen geometry of the header copy zone: the rightmost columns of the
|
|
31
|
+
* header row — the label plus the closing corner (`⧉ copy╮`). Returns null
|
|
32
|
+
* when disabled or when the box is too narrow for the label plus at least
|
|
33
|
+
* one dash. Measured with `displayWidth` (=== `visibleWidth`), so the
|
|
34
|
+
* renderer and the click hit-test always agree on the same columns even if
|
|
35
|
+
* the label's ambiguous-width glyph is measured differently than expected.
|
|
36
|
+
* `label` defaults to COPY_LABEL; pass SUCCESS_LABEL while the success
|
|
37
|
+
* chip is painted so the hit-test follows the reflowed zone.
|
|
38
|
+
*/
|
|
39
|
+
export function copyZoneRect(
|
|
40
|
+
W: number,
|
|
41
|
+
tag: string,
|
|
42
|
+
enabled: boolean,
|
|
43
|
+
label = COPY_LABEL,
|
|
44
|
+
): {startCol: number; width: number} | null {
|
|
45
|
+
const tagW = displayWidth(tag.trim());
|
|
46
|
+
const tagged = tag.trim() !== '' && tagW + 6 <= W;
|
|
47
|
+
const dashes = tagged ? W - tagW - 5 : W - 2;
|
|
48
|
+
const labelW = displayWidth(label);
|
|
49
|
+
if (!enabled || dashes - labelW < 1) return null;
|
|
50
|
+
return {startCol: W - labelW - 1, width: labelW + 1};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Re-open the surface fill after every full reset inside a row, so a
|
|
55
|
+
* mid-line `\x1b[0m` (highlightCode emits them around every token) never
|
|
56
|
+
* leaves a cell on the terminal default background. Callers append the
|
|
57
|
+
* row's single final reset after this pass.
|
|
58
|
+
*/
|
|
59
|
+
function keepSurface(s: string): string {
|
|
60
|
+
return s.split(c.reset).join(c.reset + codebox.surfaceBg);
|
|
61
|
+
}
|
|
62
|
+
|
|
21
63
|
// Opening fence: up to 3 leading spaces, 3+ backticks, then an info string
|
|
22
64
|
// that must not itself contain backticks (CommonMark) — so a line like
|
|
23
65
|
// "```code```" mid-paragraph stays prose.
|
|
@@ -95,29 +137,64 @@ export function parseCodeSegments(text: string): CodeSegment[] {
|
|
|
95
137
|
* up. Code is syntax-highlighted via the existing `highlightCode` helper,
|
|
96
138
|
* then wrapped and hard-truncated to the box — no row ever exceeds `width`.
|
|
97
139
|
*/
|
|
98
|
-
export function codeBlockBox(
|
|
140
|
+
export function codeBlockBox(
|
|
141
|
+
code: string,
|
|
142
|
+
width: number,
|
|
143
|
+
lang = '',
|
|
144
|
+
opts?: {copy?: boolean; hover?: boolean; success?: boolean},
|
|
145
|
+
): string {
|
|
99
146
|
// One clamp before anything is derived, so header/body/footer agree.
|
|
100
147
|
const W = Math.max(6, Math.floor(width));
|
|
101
148
|
const inner = W - 4; // columns between "│ " and " │"
|
|
102
149
|
const textWidth = Math.max(1, inner - 1); // reserve the last inner column
|
|
103
150
|
|
|
104
151
|
const tag = lang.trim();
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
152
|
+
const hover = opts?.hover === true;
|
|
153
|
+
const success = opts?.success === true;
|
|
154
|
+
// Every row opens the surface fill once; `paint` switches only the
|
|
155
|
+
// foreground role, and `finish` re-opens the fill after any embedded
|
|
156
|
+
// reset before closing the row with a single trailing reset.
|
|
157
|
+
const bg = codebox.surfaceBg;
|
|
158
|
+
const paint = (s: string): string => `${bg}${codebox.borderFg}${s}`;
|
|
159
|
+
const finish = (line: string): string => `${keepSurface(line)}${c.reset}`;
|
|
160
|
+
|
|
161
|
+
// Copy affordance: pin the label at the right edge of the header (zone
|
|
162
|
+
// includes the closing ╮) while the visible width stays exactly W.
|
|
163
|
+
// Chip precedence: success > hover > idle — success swaps both the
|
|
164
|
+
// label and the fg, and the zone reflows from the wider label.
|
|
165
|
+
const labelText = success ? SUCCESS_LABEL : COPY_LABEL;
|
|
166
|
+
const zone = copyZoneRect(W, tag, opts?.copy === true, labelText);
|
|
167
|
+
const tagW = displayWidth(tag);
|
|
168
|
+
const labelW = displayWidth(labelText);
|
|
169
|
+
const chipBg = hover && !success ? codebox.chipBgHover : codebox.chipBgIdle;
|
|
170
|
+
const chipFg = success ? codebox.successFg : hover ? codebox.chipFgHover : codebox.chipFgIdle;
|
|
171
|
+
const chip = `${chipBg}${chipFg}${labelText}`;
|
|
172
|
+
|
|
173
|
+
let header: string;
|
|
174
|
+
if (!zone) {
|
|
175
|
+
header = tag
|
|
176
|
+
? finish(buildHeader(W, tag, tagW, codebox.langFg, paint, ''))
|
|
177
|
+
: finish(paint(`╭${'─'.repeat(W - 2)}╮`));
|
|
178
|
+
} else if (tag !== '' && tagW + 6 <= W) {
|
|
179
|
+
const dashes = W - tagW - 5 - labelW;
|
|
180
|
+
header = finish(
|
|
181
|
+
`${paint('╭─')} ${codebox.langFg}${tag}${c.reset} ${paint('─'.repeat(dashes))}` +
|
|
182
|
+
`${chip}${paint('╮')}`,
|
|
183
|
+
);
|
|
184
|
+
} else {
|
|
185
|
+
const dashes = W - 2 - labelW;
|
|
186
|
+
header = finish(`${paint(`╭${'─'.repeat(dashes)}`)}${chip}${paint('╮')}`);
|
|
187
|
+
}
|
|
111
188
|
|
|
112
189
|
const body = wrapText(highlightCode(code, tag || undefined).join('\n'), textWidth)
|
|
113
190
|
.map((line) => {
|
|
114
191
|
const safe = truncateToWidth(line.trimEnd(), textWidth, '');
|
|
115
192
|
const pad = Math.max(0, inner - displayWidth(safe));
|
|
116
|
-
return `${paint('│')} ${safe}${c.reset}${' '.repeat(pad)} ${paint('│')}
|
|
193
|
+
return finish(`${paint('│')} ${safe}${c.reset}${' '.repeat(pad)} ${paint('│')}`);
|
|
117
194
|
})
|
|
118
195
|
.join('\n');
|
|
119
196
|
|
|
120
|
-
const footer = paint(`╰${'─'.repeat(W - 2)}╯`);
|
|
197
|
+
const footer = finish(paint(`╰${'─'.repeat(W - 2)}╯`));
|
|
121
198
|
|
|
122
199
|
return `${header}\n${body}\n${footer}`;
|
|
123
200
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {visibleWidth, wrapTextWithAnsi, truncateToWidth} from '
|
|
1
|
+
import {visibleWidth, wrapTextWithAnsi, truncateToWidth} from '../tui/tty-width.js';
|
|
2
2
|
|
|
3
|
-
import {c, agentColors, agentLabels} from '../theme.js';
|
|
3
|
+
import {c, agentColors, agentLabels, surface} from '../theme.js';
|
|
4
4
|
import type {AgentRole} from '../state.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Small text helpers shared by the renderers.
|
|
8
8
|
*
|
|
9
|
-
* Width is always measured in *terminal columns* via
|
|
9
|
+
* Width is always measured in *terminal columns* via `visibleWidth`
|
|
10
10
|
* (ANSI-aware, treats CJK/emoji as two cells, combining marks as zero), never
|
|
11
11
|
* in JavaScript string length — otherwise coloured or wide text misaligns.
|
|
12
12
|
*/
|
|
@@ -91,11 +91,11 @@ export function userMessageBox(
|
|
|
91
91
|
const label = String(agentLabels[role] ?? role ?? '');
|
|
92
92
|
const tagText = `[${label}]`;
|
|
93
93
|
const tagWidth = visibleWidth(tagText);
|
|
94
|
-
const roleColor = agentColors[role] ??
|
|
94
|
+
const roleColor = agentColors[role] ?? surface.messageBox.roleFallback;
|
|
95
95
|
|
|
96
96
|
// Border glyphs are role-coloured but dimmed, so the box frames content
|
|
97
97
|
// without competing with it; the tag itself stays bright.
|
|
98
|
-
const paint = (s: string) => `${
|
|
98
|
+
const paint = (s: string) => `${surface.messageBox.border}${roleColor}${s}${c.reset}`;
|
|
99
99
|
|
|
100
100
|
const header = buildHeader(W, tagText, tagWidth, roleColor, paint, timestamp);
|
|
101
101
|
|
|
@@ -148,7 +148,7 @@ export function buildHeader(
|
|
|
148
148
|
return (
|
|
149
149
|
`${paint('╭─')} ${roleColor}${tagText}${c.reset} ` +
|
|
150
150
|
`${paint('─'.repeat(dashes))} ` +
|
|
151
|
-
`${
|
|
151
|
+
`${surface.messageBox.dim}${ts}${c.reset} ` +
|
|
152
152
|
`${paint('─╮')}`
|
|
153
153
|
);
|
|
154
154
|
}
|
|
@@ -178,7 +178,11 @@ export function buildHeader(
|
|
|
178
178
|
if (tsWidth && tsWidth + 4 <= W) {
|
|
179
179
|
const dashes = W - tsWidth - 4;
|
|
180
180
|
if (dashes >= 1) {
|
|
181
|
-
return
|
|
181
|
+
return (
|
|
182
|
+
paint(`╭${'─'.repeat(dashes)} `) +
|
|
183
|
+
`${surface.messageBox.dim}${tsText}${c.reset} ` +
|
|
184
|
+
paint('─╮')
|
|
185
|
+
);
|
|
182
186
|
}
|
|
183
187
|
}
|
|
184
188
|
return paint(`╭${'─'.repeat(W - 2)}╮`);
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* languages well enough for agent output (TS, JS, Python, JSON, Shell, Go, Rust).
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {c} from '../theme.js';
|
|
8
|
+
import {c, syntax} from '../theme.js';
|
|
9
9
|
|
|
10
10
|
// Per-language keyword sets
|
|
11
11
|
const KEYWORDS: Record<string, Set<string> | null> = {
|
|
@@ -233,7 +233,7 @@ function getKeywords(lang: string): Set<string> | null {
|
|
|
233
233
|
export function highlightCode(code: string, lang?: string): string[] {
|
|
234
234
|
const lines = code.split('\n');
|
|
235
235
|
|
|
236
|
-
if (!lang) return lines.map((l) => `${
|
|
236
|
+
if (!lang) return lines.map((l) => `${syntax.plain}${l}${c.reset}`);
|
|
237
237
|
|
|
238
238
|
const keywords = getKeywords(lang);
|
|
239
239
|
|
|
@@ -245,7 +245,7 @@ export function highlightCode(code: string, lang?: string): string[] {
|
|
|
245
245
|
return highlightLine(line, keywords);
|
|
246
246
|
}
|
|
247
247
|
// Unknown language — just dim the code
|
|
248
|
-
return `${
|
|
248
|
+
return `${syntax.plain}${line}${c.reset}`;
|
|
249
249
|
});
|
|
250
250
|
}
|
|
251
251
|
|
|
@@ -265,13 +265,13 @@ function highlightLine(line: string, keywords: Set<string>): string {
|
|
|
265
265
|
|
|
266
266
|
// Line comment — rest of line is comment
|
|
267
267
|
if (ch === '/' && line[i + 1] === '/') {
|
|
268
|
-
result += `${
|
|
268
|
+
result += `${syntax.comment}${line.slice(i)}${c.reset}`;
|
|
269
269
|
return result;
|
|
270
270
|
}
|
|
271
271
|
|
|
272
272
|
// Hash comment (Python, Shell)
|
|
273
273
|
if (ch === '#') {
|
|
274
|
-
result += `${
|
|
274
|
+
result += `${syntax.comment}${line.slice(i)}${c.reset}`;
|
|
275
275
|
return result;
|
|
276
276
|
}
|
|
277
277
|
|
|
@@ -279,11 +279,11 @@ function highlightLine(line: string, keywords: Set<string>): string {
|
|
|
279
279
|
if (ch === '/' && line[i + 1] === '*') {
|
|
280
280
|
const end = line.indexOf('*/', i + 2);
|
|
281
281
|
if (end !== -1) {
|
|
282
|
-
result += `${
|
|
282
|
+
result += `${syntax.comment}${line.slice(i, end + 2)}${c.reset}`;
|
|
283
283
|
i = end + 2;
|
|
284
284
|
continue;
|
|
285
285
|
}
|
|
286
|
-
result += `${
|
|
286
|
+
result += `${syntax.comment}${line.slice(i)}${c.reset}`;
|
|
287
287
|
return result;
|
|
288
288
|
}
|
|
289
289
|
|
|
@@ -296,7 +296,7 @@ function highlightLine(line: string, keywords: Set<string>): string {
|
|
|
296
296
|
j++;
|
|
297
297
|
}
|
|
298
298
|
j = Math.min(j + 1, line.length); // include closing quote
|
|
299
|
-
result += `${
|
|
299
|
+
result += `${syntax.string}${line.slice(i, j)}${c.reset}`;
|
|
300
300
|
i = j;
|
|
301
301
|
continue;
|
|
302
302
|
}
|
|
@@ -305,7 +305,7 @@ function highlightLine(line: string, keywords: Set<string>): string {
|
|
|
305
305
|
if (/[0-9]/.test(ch) && (i === 0 || /[\s(,=+\-*/<>![]/.test(line[i - 1]))) {
|
|
306
306
|
let j = i;
|
|
307
307
|
while (j < line.length && /[0-9._xXabcdefABCDEF]/.test(line[j])) j++;
|
|
308
|
-
result += `${
|
|
308
|
+
result += `${syntax.number}${line.slice(i, j)}${c.reset}`;
|
|
309
309
|
i = j;
|
|
310
310
|
continue;
|
|
311
311
|
}
|
|
@@ -317,10 +317,10 @@ function highlightLine(line: string, keywords: Set<string>): string {
|
|
|
317
317
|
const word = line.slice(i, j);
|
|
318
318
|
|
|
319
319
|
if (keywords.has(word)) {
|
|
320
|
-
result += `${
|
|
320
|
+
result += `${syntax.keyword}${word}${c.reset}`;
|
|
321
321
|
} else if (i > 0 && line[i - 1] === '.') {
|
|
322
322
|
// Method/property access
|
|
323
|
-
result += `${
|
|
323
|
+
result += `${syntax.method}${word}${c.reset}`;
|
|
324
324
|
} else {
|
|
325
325
|
result += word;
|
|
326
326
|
}
|
|
@@ -347,12 +347,12 @@ function highlightJson(line: string): string {
|
|
|
347
347
|
return (
|
|
348
348
|
line
|
|
349
349
|
// Keys: "key":
|
|
350
|
-
.replace(/"([^"\\]*)"(\s*:)/g, `${
|
|
350
|
+
.replace(/"([^"\\]*)"(\s*:)/g, `${syntax.jsonKey}$1${c.reset}$2`)
|
|
351
351
|
// String values: "value"
|
|
352
|
-
.replace(/"([^"\\]*)"(?!\s*:)/g, `${
|
|
352
|
+
.replace(/"([^"\\]*)"(?!\s*:)/g, `${syntax.string}"$1"${c.reset}`)
|
|
353
353
|
// Booleans and null
|
|
354
|
-
.replace(/\b(true|false|null)\b/g, `${
|
|
354
|
+
.replace(/\b(true|false|null)\b/g, `${syntax.jsonPrimitive}$1${c.reset}`)
|
|
355
355
|
// Numbers
|
|
356
|
-
.replace(/\b(-?[0-9]+\.?[0-9]*)\b/g, `${
|
|
356
|
+
.replace(/\b(-?[0-9]+\.?[0-9]*)\b/g, `${syntax.number}$1${c.reset}`)
|
|
357
357
|
);
|
|
358
358
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OSC 10/11 canvas sync — set the terminal's own default fg/bg to the active
|
|
3
|
+
* theme's colours so cleared cells, gutters and the editor row repaint in
|
|
4
|
+
* theme colours (pi-tui's per-line `ESC[0m` resets then resolve to *our*
|
|
5
|
+
* canvas instead of the terminal default).
|
|
6
|
+
*
|
|
7
|
+
* Everything here is pure: the plan says what to do, the builders produce the
|
|
8
|
+
* bytes, the parser harvests the raw query replies so shutdown can restore the
|
|
9
|
+
* terminal byte-for-byte (rgb:RRRR/GGGG/BBBB vs #RRGGBB vs uppercase forms
|
|
10
|
+
* all round-trip untouched). The index.ts caller owns the I/O.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {wrapTmuxOsc} from './osc52.js';
|
|
14
|
+
|
|
15
|
+
/** Channels to sync: `''`/missing means "leave the terminal default alone". */
|
|
16
|
+
export interface CanvasSync {
|
|
17
|
+
fg?: string;
|
|
18
|
+
bg?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const HEX6 = /^#[0-9a-fA-F]{6}$/;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Decide which channels a theme wants synced. Only `'#rrggbb'` values are
|
|
25
|
+
* plan-worthy — `''` (vanilla: "terminal default, no canvas sync") and any
|
|
26
|
+
* malformed value yield an empty plan, which the caller treats as "emit
|
|
27
|
+
* absolutely nothing": no query, no set, no restore.
|
|
28
|
+
*/
|
|
29
|
+
export function planCanvasSync(window: {bg: string; fg: string}): CanvasSync {
|
|
30
|
+
const plan: CanvasSync = {};
|
|
31
|
+
if (HEX6.test(window.fg)) plan.fg = window.fg;
|
|
32
|
+
if (HEX6.test(window.bg)) plan.bg = window.bg;
|
|
33
|
+
return plan;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** OSC 10 set: default foreground → `hex` (`#rrggbb`). */
|
|
37
|
+
export function buildOsc10Set(
|
|
38
|
+
hex: string,
|
|
39
|
+
env: Record<string, string | undefined> = process.env,
|
|
40
|
+
): string {
|
|
41
|
+
return wrapTmuxOsc(`\x1b]10;${hex}\x07`, env);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** OSC 11 set: default background → `hex` (`#rrggbb`). */
|
|
45
|
+
export function buildOsc11Set(
|
|
46
|
+
hex: string,
|
|
47
|
+
env: Record<string, string | undefined> = process.env,
|
|
48
|
+
): string {
|
|
49
|
+
return wrapTmuxOsc(`\x1b]11;${hex}\x07`, env);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Query both default colours (OSC 10;? + OSC 11;?) as one wrapped payload so
|
|
54
|
+
* the terminal's replies arrive during a single read window. A terminal that
|
|
55
|
+
* never replies must cost one timeout, not one per channel.
|
|
56
|
+
*/
|
|
57
|
+
export function buildCanvasQuery(env: Record<string, string | undefined> = process.env): string {
|
|
58
|
+
return wrapTmuxOsc('\x1b]10;?\x07\x1b]11;?\x07', env);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Harvest the raw reply payloads from a byte string: `ESC]10;<payload>` and
|
|
63
|
+
* `ESC]11;<payload>` terminated by BEL (`\x07`) or ST (`ESC \`).
|
|
64
|
+
*
|
|
65
|
+
* The payload is kept verbatim (not normalised to hex) so the restore emit is
|
|
66
|
+
* exactly what the terminal originally reported. Payload `?` (the "no such
|
|
67
|
+
* colour" answer some terminals send) or an empty payload counts as absent.
|
|
68
|
+
* Anything not matching — interleaved keystrokes, partial chunks, noise — is
|
|
69
|
+
* ignored rather than failing the parse.
|
|
70
|
+
*/
|
|
71
|
+
export function parseCanvasReplies(bytes: string): CanvasSync {
|
|
72
|
+
const saved: CanvasSync = {};
|
|
73
|
+
// ESC/BEL composed via interpolation: a regex literal would trip
|
|
74
|
+
// noControlCharactersInRegex, a static new RegExp() string would trip
|
|
75
|
+
// useRegexLiterals (already at its 2-warning budget in permission-prompt).
|
|
76
|
+
const esc = '\x1b';
|
|
77
|
+
const bel = '\x07';
|
|
78
|
+
const reply = new RegExp(`${esc}\\](10|11);([\\s\\S]*?)(?:${bel}|${esc}\\\\)`, 'g');
|
|
79
|
+
for (const match of bytes.matchAll(reply)) {
|
|
80
|
+
const payload = match[2];
|
|
81
|
+
if (payload === '' || payload === '?') continue;
|
|
82
|
+
if (match[1] === '10') saved.fg = payload;
|
|
83
|
+
else saved.bg = payload;
|
|
84
|
+
}
|
|
85
|
+
return saved;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Re-emit the saved raw payloads verbatim (OSC 10 first, then OSC 11 — query
|
|
90
|
+
* order), only for channels that were actually captured. Called on every exit
|
|
91
|
+
* path after a successful set so the user's terminal is never left themed.
|
|
92
|
+
*/
|
|
93
|
+
export function buildCanvasRestore(
|
|
94
|
+
saved: CanvasSync,
|
|
95
|
+
env: Record<string, string | undefined> = process.env,
|
|
96
|
+
): string {
|
|
97
|
+
let payload = '';
|
|
98
|
+
if (saved.fg !== undefined) payload += `\x1b]10;${saved.fg}\x07`;
|
|
99
|
+
if (saved.bg !== undefined) payload += `\x1b]11;${saved.bg}\x07`;
|
|
100
|
+
if (payload === '') return '';
|
|
101
|
+
return wrapTmuxOsc(payload, env);
|
|
102
|
+
}
|
|
@@ -37,6 +37,22 @@ export function lastCodeBlock(messages: readonly ConversationMessage[]): string
|
|
|
37
37
|
return null;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Wrap an OSC sequence for the current environment: unchanged outside tmux,
|
|
42
|
+
* DCS pass-through (every ESC doubled) inside it. tmux swallows OSC sequences
|
|
43
|
+
* emitted inside it, so without the wrap the sequence stops at tmux and never
|
|
44
|
+
* reaches the real terminal. Shared by the OSC 52/22 builders here and the
|
|
45
|
+
* OSC 10/11 canvas builders in osc-canvas.ts.
|
|
46
|
+
*/
|
|
47
|
+
export function wrapTmuxOsc(
|
|
48
|
+
payload: string,
|
|
49
|
+
env: Record<string, string | undefined> = process.env,
|
|
50
|
+
): string {
|
|
51
|
+
const tmuxTerm = env.TERM?.startsWith('tmux') ?? false;
|
|
52
|
+
if (!env.TMUX && !tmuxTerm) return payload;
|
|
53
|
+
return `\x1bPtmux;${payload.replaceAll('\x1b', '\x1b\x1b')}\x1b\\`;
|
|
54
|
+
}
|
|
55
|
+
|
|
40
56
|
/**
|
|
41
57
|
* Build the OSC 52 sequence that sets the system clipboard to `text`.
|
|
42
58
|
*
|
|
@@ -49,7 +65,20 @@ export function buildOsc52Sequence(
|
|
|
49
65
|
env: Record<string, string | undefined> = process.env,
|
|
50
66
|
): string {
|
|
51
67
|
const payload = `\x1b]52;c;${Buffer.from(text, 'utf8').toString('base64')}\x07`;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
68
|
+
return wrapTmuxOsc(payload, env);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Build the OSC 22 sequence that switches the cursor to the pointer (hand)
|
|
73
|
+
* shape (`on`) or restores the default cursor (`off`).
|
|
74
|
+
*
|
|
75
|
+
* Same tmux DCS wrap as OSC 52 — tmux swallows bare OSC 22 the same way.
|
|
76
|
+
* Terminals without OSC 22 support ignore the sequence silently, so this is
|
|
77
|
+
* always safe to emit; the hover highlight does not depend on it.
|
|
78
|
+
*/
|
|
79
|
+
export function buildOsc22Pointer(
|
|
80
|
+
on: boolean,
|
|
81
|
+
env: Record<string, string | undefined> = process.env,
|
|
82
|
+
): string {
|
|
83
|
+
return wrapTmuxOsc(`\x1b]22;${on ? 'pointer' : 'default'}\x07`, env);
|
|
55
84
|
}
|