pal-code 0.0.21 → 0.0.23
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/README.md +131 -0
- package/package.json +2 -2
- package/packages/cli/src/approval-gate.ts +113 -0
- package/packages/cli/src/commands.ts +21 -3
- package/packages/cli/src/components/sidebar.ts +72 -1
- package/packages/cli/src/headless.ts +495 -0
- package/packages/cli/src/index.ts +835 -94
- package/packages/cli/src/state.ts +23 -0
- package/packages/cli/src/tui/selection.ts +232 -0
|
@@ -52,6 +52,8 @@ export interface AppState extends SharedState {
|
|
|
52
52
|
activeAbort: AbortController | null;
|
|
53
53
|
/** First Esc armed a stop; a second one inside the window aborts the turn. */
|
|
54
54
|
abortArmed: boolean;
|
|
55
|
+
/** First Ctrl+C armed an exit; a second one inside the window quits. */
|
|
56
|
+
exitArmed: boolean;
|
|
55
57
|
/** The most recently collapsed run, for ctrl+o. */
|
|
56
58
|
lastRun: RunView | null;
|
|
57
59
|
/** Messages submitted while a turn was running; drained one per settled turn. */
|
|
@@ -97,6 +99,7 @@ export const state: AppState = {
|
|
|
97
99
|
activeRun: null,
|
|
98
100
|
activeAbort: null,
|
|
99
101
|
abortArmed: false,
|
|
102
|
+
exitArmed: false,
|
|
100
103
|
lastRun: null,
|
|
101
104
|
pendingMessages: [],
|
|
102
105
|
refreshRoleIndicator: () => {},
|
|
@@ -127,6 +130,26 @@ export function nextEscAction(armed: boolean): {armed: boolean; abort: boolean}
|
|
|
127
130
|
return armed ? {armed: false, abort: true} : {armed: true, abort: false};
|
|
128
131
|
}
|
|
129
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Resolve one Ctrl/Cmd+C press against the two-press exit contract.
|
|
135
|
+
*
|
|
136
|
+
* Ctrl+C is overloaded on Windows and Linux: it is both the copy key and the
|
|
137
|
+
* quit key, and a bare `\x03` carries no modifier information, so the two cannot
|
|
138
|
+
* be told apart at the byte level. Precedence handles the common case — a live
|
|
139
|
+
* selection is copied — but with nothing selected the first press would quit
|
|
140
|
+
* instantly, which is exactly the collision the copy feature was meant to
|
|
141
|
+
* remove. So quitting also takes two presses: the first arms, the second inside
|
|
142
|
+
* the window exits. `copy` outranks both: while something is selected the press
|
|
143
|
+
* is a copy, never an arm.
|
|
144
|
+
*/
|
|
145
|
+
export function nextExitAction(
|
|
146
|
+
armed: boolean,
|
|
147
|
+
hasSelection: boolean,
|
|
148
|
+
): {armed: boolean; exit: boolean; copy: boolean} {
|
|
149
|
+
if (hasSelection) return {armed: false, exit: false, copy: true};
|
|
150
|
+
return armed ? {armed: false, exit: true, copy: false} : {armed: true, exit: false, copy: false};
|
|
151
|
+
}
|
|
152
|
+
|
|
130
153
|
/**
|
|
131
154
|
* Take the next message to run when a turn settles — completed, errored, or
|
|
132
155
|
* aborted alike. Returns null when nothing is queued.
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mouse drag-selection over the transcript.
|
|
3
|
+
*
|
|
4
|
+
* Why pal has to own this: while mouse reporting is on (`?1000h ?1003h ?1006h`)
|
|
5
|
+
* the terminal stops building its own selection, so `Shift+drag` is the only
|
|
6
|
+
* way to get a native one — and native selection is screen-linear and dumb. It
|
|
7
|
+
* copies whole screen rows, so dragging across a code block also drags in the
|
|
8
|
+
* sidebar overlay glyphs and trailing padding painted on the same rows.
|
|
9
|
+
*
|
|
10
|
+
* These helpers give pal its own selection instead: points are in TRANSCRIPT
|
|
11
|
+
* coordinates (absolute rendered row + column), the highlight is applied to the
|
|
12
|
+
* rendered window, and the copied text is built from the same rows. The caller
|
|
13
|
+
* clips columns to the transcript width, which is what keeps the sidebar out.
|
|
14
|
+
*
|
|
15
|
+
* Column math uses pi-tui's `sliceByColumn`/`visibleWidth` pair rather than a
|
|
16
|
+
* second implementation, because slicing and measuring have to agree exactly or
|
|
17
|
+
* the highlight drifts from the text it claims to cover.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import {sliceByColumn, visibleWidth} from '@earendil-works/pi-tui';
|
|
21
|
+
|
|
22
|
+
/** A cell in transcript space: row is an absolute rendered row, col is 0-based. */
|
|
23
|
+
export interface SelectionPoint {
|
|
24
|
+
row: number;
|
|
25
|
+
col: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** An ordered selection: `first` is up-left of `last`. */
|
|
29
|
+
export interface SelectionRange {
|
|
30
|
+
first: SelectionPoint;
|
|
31
|
+
last: SelectionPoint;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Reverse video marks the selection; `\x1b[27m` cancels only that attribute. */
|
|
35
|
+
const REVERSE = '\x1b[7m';
|
|
36
|
+
const REVERSE_OFF = '\x1b[27m';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Order two drag endpoints into a normalised range.
|
|
40
|
+
*
|
|
41
|
+
* Dragging up or left is normal, so the anchor is whichever corner the user
|
|
42
|
+
* started from — not necessarily the top-left one. Returns null when both
|
|
43
|
+
* endpoints are absent.
|
|
44
|
+
*/
|
|
45
|
+
export function normalizeSelection(
|
|
46
|
+
anchor: SelectionPoint | null,
|
|
47
|
+
focus: SelectionPoint | null,
|
|
48
|
+
): SelectionRange | null {
|
|
49
|
+
if (!anchor || !focus) return null;
|
|
50
|
+
const anchorBefore =
|
|
51
|
+
anchor.row < focus.row || (anchor.row === focus.row && anchor.col <= focus.col);
|
|
52
|
+
return anchorBefore
|
|
53
|
+
? {first: {...anchor}, last: {...focus}}
|
|
54
|
+
: {first: {...focus}, last: {...anchor}};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** True when the range covers no cells at all, so there is nothing to copy. */
|
|
58
|
+
export function isEmptySelection(range: SelectionRange): boolean {
|
|
59
|
+
return range.first.row === range.last.row && range.first.col === range.last.col;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Which transcript edge the pointer is past: -1 top, 0 inside, 1 bottom. */
|
|
63
|
+
export type DragEdge = -1 | 0 | 1;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Classify a pointer row against the visible transcript window.
|
|
67
|
+
*
|
|
68
|
+
* `windowIdx` is the row offset inside the rendered window and may be negative
|
|
69
|
+
* (above it) or `>= winLen` (below it) — that overflow is the signal to scroll.
|
|
70
|
+
* A zero-length window has no inside, so it reports the nearer edge rather than
|
|
71
|
+
* pretending the pointer is within the content.
|
|
72
|
+
*
|
|
73
|
+
* `movingUp` exists because the two edges are not symmetric in screen space.
|
|
74
|
+
* The bottom needs no help: the transcript window ends above the editor and
|
|
75
|
+
* status line, so there are real screen rows below it to drag into. The top
|
|
76
|
+
* does not — while the transcript is taller than the viewport it begins at
|
|
77
|
+
* screen row 1, no pointer row exists above row 1, and `windowIdx` can never go
|
|
78
|
+
* negative. So the first row doubles as the top edge *while the drag is heading
|
|
79
|
+
* upward*, which is what separates "drag past the top" from "a selection that
|
|
80
|
+
* legitimately starts on the first row".
|
|
81
|
+
*/
|
|
82
|
+
export function dragEdgeFor(windowIdx: number, winLen: number, movingUp = false): DragEdge {
|
|
83
|
+
if (winLen <= 0) return windowIdx < 0 ? -1 : 1;
|
|
84
|
+
if (windowIdx < 0) return -1;
|
|
85
|
+
if (windowIdx >= winLen) return 1;
|
|
86
|
+
if (windowIdx === 0 && movingUp) return -1;
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Clamp a pointer row into the visible window, in transcript coordinates.
|
|
92
|
+
*
|
|
93
|
+
* While the pointer is past an edge the selection focus has to pin to the edge
|
|
94
|
+
* row — otherwise the focus would point at a row that is not painted, and the
|
|
95
|
+
* highlight (and the copy) would silently include rows the user cannot see.
|
|
96
|
+
*/
|
|
97
|
+
export function clampRowIntoWindow(windowIdx: number, winLen: number, start: number): number {
|
|
98
|
+
const last = Math.max(0, winLen - 1);
|
|
99
|
+
const clamped = Math.max(0, Math.min(windowIdx, last));
|
|
100
|
+
return start + clamped;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Wrap visible columns `[from, to)` of a rendered line in reverse video.
|
|
105
|
+
*
|
|
106
|
+
* The span is re-armed after every SGR reset inside it: transcript lines carry
|
|
107
|
+
* their own colour changes (`dim … reset`, code-block backgrounds), and a bare
|
|
108
|
+
* reset would otherwise cancel the highlight halfway through the selection.
|
|
109
|
+
*/
|
|
110
|
+
export function highlightSpan(line: string, from: number, to: number): string {
|
|
111
|
+
const width = visibleWidth(line);
|
|
112
|
+
const start = Math.max(0, Math.min(from, width));
|
|
113
|
+
const end = Math.max(start, Math.min(to, width));
|
|
114
|
+
if (end <= start) return line;
|
|
115
|
+
|
|
116
|
+
const before = sliceByColumn(line, 0, start, true);
|
|
117
|
+
const middle = sliceByColumn(line, start, end - start, true);
|
|
118
|
+
const after = sliceByColumn(line, end, width - end, true);
|
|
119
|
+
const stable = middle.replace(/\x1b\[(?:0|)m/g, (reset) => reset + REVERSE);
|
|
120
|
+
return `${before}${REVERSE}${stable}${REVERSE_OFF}${after}`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Copy the text under a selection.
|
|
125
|
+
*
|
|
126
|
+
* `lines` are rendered transcript rows starting at `range.first.row`, so
|
|
127
|
+
* `lines[0]` is the row the selection starts on. The first row is taken from
|
|
128
|
+
* the anchor column to the end, the last row from the start to the focus
|
|
129
|
+
* column, and every row in between in full — the usual block-selection reading.
|
|
130
|
+
* Escapes are stripped and trailing padding is trimmed, because the copied
|
|
131
|
+
* value is text, not the painted row.
|
|
132
|
+
*/
|
|
133
|
+
export function extractSelection(lines: string[], range: SelectionRange): string {
|
|
134
|
+
if (lines.length === 0) return '';
|
|
135
|
+
const out: string[] = [];
|
|
136
|
+
const lastIndex = Math.min(lines.length - 1, range.last.row - range.first.row);
|
|
137
|
+
|
|
138
|
+
for (let i = 0; i <= lastIndex; i++) {
|
|
139
|
+
const line = lines[i] ?? '';
|
|
140
|
+
const width = visibleWidth(line);
|
|
141
|
+
const isFirst = i === 0;
|
|
142
|
+
const isLast = i === lastIndex;
|
|
143
|
+
const from = isFirst ? Math.max(0, Math.min(range.first.col, width)) : 0;
|
|
144
|
+
const to = isLast ? Math.max(from, Math.min(range.last.col, width)) : width;
|
|
145
|
+
const slice = sliceByColumn(line, from, Math.max(0, to - from), true);
|
|
146
|
+
out.push(stripAnsi(slice).replace(/\s+$/, ''));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// A click without a drag yields blank lines; report "nothing selected" rather
|
|
150
|
+
// than copying an empty string to the clipboard.
|
|
151
|
+
return out.join('\n');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Strip CSI/OSC/APC sequences. Local so this module has one dependency pair. */
|
|
155
|
+
function stripAnsi(text: string): string {
|
|
156
|
+
if (!text.includes('\x1b')) return text;
|
|
157
|
+
// eslint-disable-next-line no-control-regex
|
|
158
|
+
return text.replace(/\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[@-_]/g, '');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ── Word selection (double-click) ──
|
|
162
|
+
|
|
163
|
+
/** How a cell groups with its neighbours when expanding a double-click. */
|
|
164
|
+
export type CharClass = 'word' | 'space' | 'punct';
|
|
165
|
+
|
|
166
|
+
const WORD_CHAR = /[\p{L}\p{N}_]/u;
|
|
167
|
+
|
|
168
|
+
function classify(text: string): CharClass {
|
|
169
|
+
if (/^\s+$/u.test(text)) return 'space';
|
|
170
|
+
if (WORD_CHAR.test(text)) return 'word';
|
|
171
|
+
return 'punct';
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** A grapheme with its visible column span, so grouping never splits wide glyphs. */
|
|
175
|
+
interface Cell {
|
|
176
|
+
start: number;
|
|
177
|
+
end: number;
|
|
178
|
+
cls: CharClass;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Grapheme cells of a rendered row, in visible columns.
|
|
183
|
+
*
|
|
184
|
+
* Grapheme-aware rather than per-code-unit: a double-click must never select
|
|
185
|
+
* half of a wide CJK glyph or split an emoji, and both would land off-centre if
|
|
186
|
+
* columns were counted by string index.
|
|
187
|
+
*/
|
|
188
|
+
function cellsOf(line: string): Cell[] {
|
|
189
|
+
const plain = stripAnsi(line);
|
|
190
|
+
if (plain.length === 0) return [];
|
|
191
|
+
const cells: Cell[] = [];
|
|
192
|
+
let col = 0;
|
|
193
|
+
for (const {segment} of new Intl.Segmenter(undefined, {granularity: 'grapheme'}).segment(plain)) {
|
|
194
|
+
const width = visibleWidth(segment);
|
|
195
|
+
// Zero-width (combining marks) belongs to the glyph it decorates, so it
|
|
196
|
+
// inherits that cell's class instead of starting one of its own.
|
|
197
|
+
if (width <= 0) continue;
|
|
198
|
+
cells.push({start: col, end: col + width, cls: classify(segment)});
|
|
199
|
+
col += width;
|
|
200
|
+
}
|
|
201
|
+
return cells;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The run of same-class characters under a column, for double-click selection.
|
|
206
|
+
*
|
|
207
|
+
* Word characters (letters, digits, underscore) group together; whitespace forms
|
|
208
|
+
* its own run and so does punctuation. That is the usual terminal/vim reading,
|
|
209
|
+
* so a double-click on `foo.bar` selects `foo` or `bar` rather than the whole
|
|
210
|
+
* dotted expression, and a double-click on a blank area selects the indentation
|
|
211
|
+
* instead of nothing.
|
|
212
|
+
*
|
|
213
|
+
* Returns columns as a half-open span in the same space `highlightSpan` and
|
|
214
|
+
* `extractSelection` use, or null when the column is past the row's text.
|
|
215
|
+
*/
|
|
216
|
+
export function wordRangeAt(line: string, col: number): {from: number; to: number} | null {
|
|
217
|
+
const cells = cellsOf(line);
|
|
218
|
+
const last = cells[cells.length - 1];
|
|
219
|
+
if (last === undefined) return null;
|
|
220
|
+
// A click one cell past the end of the text (trailing padding) is not a word.
|
|
221
|
+
if (col < 0 || col >= last.end) return null;
|
|
222
|
+
|
|
223
|
+
let idx = cells.findIndex((cell) => col >= cell.start && col < cell.end);
|
|
224
|
+
if (idx === -1) idx = 0;
|
|
225
|
+
|
|
226
|
+
const cls = cells[idx]!.cls;
|
|
227
|
+
let lo = idx;
|
|
228
|
+
while (lo > 0 && cells[lo - 1]!.cls === cls) lo--;
|
|
229
|
+
let hi = idx;
|
|
230
|
+
while (hi < cells.length - 1 && cells[hi + 1]!.cls === cls) hi++;
|
|
231
|
+
return {from: cells[lo]!.start, to: cells[hi]!.end};
|
|
232
|
+
}
|