@narumitw/pi-notes 0.0.0 → 0.1.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/README.md +28 -14
- package/package.json +1 -1
- package/src/child-session.ts +54 -15
- package/src/constants.ts +8 -3
- package/src/fullscreen.ts +253 -0
- package/src/menu.ts +102 -46
- package/src/notes-extension.ts +92 -16
- package/src/storage.ts +152 -26
- package/src/template-editor.ts +664 -0
- package/src/workspace.ts +179 -23
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
import type { ExtensionCommandContext, KeybindingsManager, Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
type Component,
|
|
4
|
+
decodeKittyPrintable,
|
|
5
|
+
Editor,
|
|
6
|
+
type EditorTheme,
|
|
7
|
+
type Focusable,
|
|
8
|
+
Key,
|
|
9
|
+
matchesKey,
|
|
10
|
+
parseKey,
|
|
11
|
+
type TUI,
|
|
12
|
+
type TuiMouseEvent,
|
|
13
|
+
type TuiMouseEventResult,
|
|
14
|
+
truncateToWidth,
|
|
15
|
+
} from "@earendil-works/pi-tui";
|
|
16
|
+
import { runCustomInteraction } from "@narumitw/pi-tui-kit/custom-interaction";
|
|
17
|
+
import { sanitizeTerminalText } from "@narumitw/pi-tui-kit/terminal-text";
|
|
18
|
+
import type { TemplateSnapshot } from "./storage.js";
|
|
19
|
+
|
|
20
|
+
const BRACKETED_PASTE_START = "\u001b[200~";
|
|
21
|
+
const BRACKETED_PASTE_END = "\u001b[201~";
|
|
22
|
+
const PASTE_TAIL_GUARD_MS = 50;
|
|
23
|
+
// biome-ignore lint/complexity/useRegexLiterals: A literal ESC pattern violates noControlCharactersInRegex.
|
|
24
|
+
const TMUX_PASTE_CONTROL_PATTERN = new RegExp("\\x1b\\[(\\d+);5u", "gu");
|
|
25
|
+
|
|
26
|
+
// Pi 0.86 Editor checks these actions before newline and submit. Autocomplete-only
|
|
27
|
+
// selection actions are omitted because this editor has no autocomplete provider.
|
|
28
|
+
const EDITOR_ACTIONS_BEFORE_NEWLINE = [
|
|
29
|
+
"tui.input.copy",
|
|
30
|
+
"tui.editor.undo",
|
|
31
|
+
"tui.input.tab",
|
|
32
|
+
"tui.editor.deleteCharBackward",
|
|
33
|
+
"tui.editor.deleteCharForward",
|
|
34
|
+
"tui.editor.deleteWordBackward",
|
|
35
|
+
"tui.editor.deleteWordForward",
|
|
36
|
+
"tui.editor.deleteToLineStart",
|
|
37
|
+
"tui.editor.deleteToLineEnd",
|
|
38
|
+
"tui.editor.yank",
|
|
39
|
+
"tui.editor.yankPop",
|
|
40
|
+
"tui.editor.historyPrevious",
|
|
41
|
+
"tui.editor.historyNext",
|
|
42
|
+
"tui.editor.cursorLineStart",
|
|
43
|
+
"tui.editor.cursorLineEnd",
|
|
44
|
+
"tui.editor.cursorWordLeft",
|
|
45
|
+
"tui.editor.cursorWordRight",
|
|
46
|
+
] as const;
|
|
47
|
+
|
|
48
|
+
const EDITOR_PRIORITY_ACTIONS = [
|
|
49
|
+
"tui.editor.cursorUp",
|
|
50
|
+
"tui.editor.cursorDown",
|
|
51
|
+
"tui.editor.historyPrevious",
|
|
52
|
+
"tui.editor.historyNext",
|
|
53
|
+
"tui.editor.cursorLeft",
|
|
54
|
+
"tui.editor.cursorRight",
|
|
55
|
+
"tui.editor.cursorWordLeft",
|
|
56
|
+
"tui.editor.cursorWordRight",
|
|
57
|
+
"tui.editor.cursorLineStart",
|
|
58
|
+
"tui.editor.cursorLineEnd",
|
|
59
|
+
"tui.editor.jumpForward",
|
|
60
|
+
"tui.editor.jumpBackward",
|
|
61
|
+
"tui.editor.pageUp",
|
|
62
|
+
"tui.editor.pageDown",
|
|
63
|
+
"tui.editor.deleteCharBackward",
|
|
64
|
+
"tui.editor.deleteCharForward",
|
|
65
|
+
"tui.editor.deleteWordBackward",
|
|
66
|
+
"tui.editor.deleteWordForward",
|
|
67
|
+
"tui.editor.deleteToLineStart",
|
|
68
|
+
"tui.editor.deleteToLineEnd",
|
|
69
|
+
"tui.editor.yank",
|
|
70
|
+
"tui.editor.yankPop",
|
|
71
|
+
"tui.editor.undo",
|
|
72
|
+
"tui.input.newLine",
|
|
73
|
+
"tui.input.submit",
|
|
74
|
+
"tui.input.tab",
|
|
75
|
+
] as const;
|
|
76
|
+
|
|
77
|
+
interface TemplateEditorOwnership {
|
|
78
|
+
signal: AbortSignal;
|
|
79
|
+
isCurrent(): boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function showTemplateEditor(
|
|
83
|
+
ctx: ExtensionCommandContext,
|
|
84
|
+
template: TemplateSnapshot,
|
|
85
|
+
ownership: TemplateEditorOwnership,
|
|
86
|
+
): Promise<string | undefined> {
|
|
87
|
+
const result = await runCustomInteraction<string | undefined>(ctx, {
|
|
88
|
+
signal: ownership.signal,
|
|
89
|
+
isCurrent: ownership.isCurrent,
|
|
90
|
+
create: ({ tui, theme, keybindings, complete }) =>
|
|
91
|
+
new TemplateEditor({
|
|
92
|
+
tui,
|
|
93
|
+
theme,
|
|
94
|
+
keybindings,
|
|
95
|
+
title: `Edit template · ${sanitizeTerminalText(template.relativePath)}`,
|
|
96
|
+
content: template.content,
|
|
97
|
+
onDone: complete,
|
|
98
|
+
}),
|
|
99
|
+
});
|
|
100
|
+
if (result.kind === "error") throw result.error;
|
|
101
|
+
return result.kind === "completed" ? result.value : undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface TemplateEditorOptions {
|
|
105
|
+
tui: TUI;
|
|
106
|
+
theme: Theme;
|
|
107
|
+
keybindings: KeybindingsManager;
|
|
108
|
+
title: string;
|
|
109
|
+
content: string;
|
|
110
|
+
onDone(value: string | undefined): void;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
class TemplateEditor implements Component, Focusable {
|
|
114
|
+
private readonly tui: TUI;
|
|
115
|
+
private readonly theme: Theme;
|
|
116
|
+
private readonly keybindings: KeybindingsManager;
|
|
117
|
+
private readonly editor: RawPreservingEditor;
|
|
118
|
+
private readonly title: string;
|
|
119
|
+
private readonly onDone: (value: string | undefined) => void;
|
|
120
|
+
private editorStartRow = 1;
|
|
121
|
+
private editorRows = 0;
|
|
122
|
+
private deferredPasteInputs: string[] = [];
|
|
123
|
+
private pasteTailTimer: ReturnType<typeof setTimeout> | undefined;
|
|
124
|
+
private pendingPasteWork: Promise<void> | undefined;
|
|
125
|
+
private resolvePendingPasteWork: (() => void) | undefined;
|
|
126
|
+
private finished = false;
|
|
127
|
+
private _focused = false;
|
|
128
|
+
|
|
129
|
+
constructor(options: TemplateEditorOptions) {
|
|
130
|
+
this.tui = options.tui;
|
|
131
|
+
this.theme = options.theme;
|
|
132
|
+
this.keybindings = options.keybindings;
|
|
133
|
+
this.title = options.title;
|
|
134
|
+
this.onDone = options.onDone;
|
|
135
|
+
const editorTheme: EditorTheme = {
|
|
136
|
+
borderColor: (text) => this.theme.fg("accent", text),
|
|
137
|
+
selectList: {
|
|
138
|
+
selectedPrefix: (text) => this.theme.fg("accent", text),
|
|
139
|
+
selectedText: (text) => this.theme.fg("accent", text),
|
|
140
|
+
description: (text) => this.theme.fg("muted", text),
|
|
141
|
+
scrollInfo: (text) => this.theme.fg("dim", text),
|
|
142
|
+
noMatch: (text) => this.theme.fg("warning", text),
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
this.editor = new RawPreservingEditor(this.tui, editorTheme);
|
|
146
|
+
this.editor.setText(options.content);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
get focused(): boolean {
|
|
150
|
+
return this._focused;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
set focused(value: boolean) {
|
|
154
|
+
this._focused = value;
|
|
155
|
+
this.editor.focused = value;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
render(width: number): string[] {
|
|
159
|
+
const safeWidth = Math.max(1, width);
|
|
160
|
+
const title = truncateToWidth(this.theme.fg("accent", this.theme.bold(this.title)), safeWidth);
|
|
161
|
+
const warnings: string[] = [];
|
|
162
|
+
const pasteError = this.editor.getPasteError();
|
|
163
|
+
if (pasteError) warnings.push(truncateToWidth(this.theme.fg("error", pasteError), safeWidth));
|
|
164
|
+
if (this.editor.hasHiddenCharacters()) {
|
|
165
|
+
warnings.push(
|
|
166
|
+
truncateToWidth(
|
|
167
|
+
this.theme.fg("warning", "Terminal controls are hidden as spaces and preserved unless removed."),
|
|
168
|
+
safeWidth,
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
const editorLines = this.editor.render(safeWidth);
|
|
173
|
+
this.editorStartRow = 1 + warnings.length;
|
|
174
|
+
this.editorRows = editorLines.length;
|
|
175
|
+
const hint = truncateToWidth(this.theme.fg("muted", this.hintText()), safeWidth);
|
|
176
|
+
return [title, ...warnings, ...editorLines, hint];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
handleInput(data: string): void {
|
|
180
|
+
if (this.finished) return;
|
|
181
|
+
if (this.editor.hasPendingPaste) {
|
|
182
|
+
this.handlePendingPasteInput(data);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
this.dispatchInput(data);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async waitForPending(): Promise<void> {
|
|
189
|
+
for (;;) {
|
|
190
|
+
const pending = this.pendingPasteWork;
|
|
191
|
+
if (!pending) return;
|
|
192
|
+
await pending;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
197
|
+
if (event.y < this.editorStartRow || event.y >= this.editorStartRow + this.editorRows) {
|
|
198
|
+
return { handled: true, focus: true };
|
|
199
|
+
}
|
|
200
|
+
return this.editor.handleMouse({
|
|
201
|
+
...event,
|
|
202
|
+
y: event.y - this.editorStartRow,
|
|
203
|
+
height: Math.max(1, this.editorRows),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
invalidate(): void {
|
|
208
|
+
this.editor.invalidate();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
dispose(): void {
|
|
212
|
+
this.finished = true;
|
|
213
|
+
this.cancelPendingPasteWork();
|
|
214
|
+
this.editor.dispose();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private dispatchInput(data: string): void {
|
|
218
|
+
if (this.editor.isPasting || data.includes(BRACKETED_PASTE_START) || data.includes(BRACKETED_PASTE_END)) {
|
|
219
|
+
const remaining = this.editor.handleInput(data);
|
|
220
|
+
if (this.editor.hasPendingPaste) this.armPasteTailTimer();
|
|
221
|
+
this.tui.requestRender();
|
|
222
|
+
if (remaining) this.handleInput(remaining);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (matchesKey(data, Key.ctrl("c"))) {
|
|
226
|
+
this.finish(undefined);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (isEditorActionBeforeNewline(data, this.keybindings)) {
|
|
230
|
+
this.editor.handleKeyInput(data);
|
|
231
|
+
this.tui.requestRender();
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (this.keybindings.matches(data, "tui.select.cancel") && !isFocusedEditorInput(data, this.keybindings)) {
|
|
235
|
+
this.finish(undefined);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (isEditorNewlineInput(data, this.keybindings)) {
|
|
239
|
+
this.editor.handleKeyInput(data);
|
|
240
|
+
this.tui.requestRender();
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (this.keybindings.matches(data, "tui.input.submit")) {
|
|
244
|
+
if (this.editor.hasBackslashBeforeCursor()) {
|
|
245
|
+
this.editor.replaceBackslashWithNewline();
|
|
246
|
+
this.tui.requestRender();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (!this.editor.confirmPasteSubmission()) {
|
|
250
|
+
this.tui.requestRender();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
this.finish(this.editor.getExpandedText());
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
this.editor.handleInput(data);
|
|
257
|
+
this.tui.requestRender();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private handlePendingPasteInput(data: string): void {
|
|
261
|
+
if (data.includes(BRACKETED_PASTE_START) || data.includes(BRACKETED_PASTE_END)) {
|
|
262
|
+
const resolve = this.detachPendingPasteWork();
|
|
263
|
+
this.deferredPasteInputs = [];
|
|
264
|
+
this.editor.rejectPendingPaste();
|
|
265
|
+
this.tui.requestRender();
|
|
266
|
+
resolve?.();
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
this.deferredPasteInputs.push(data);
|
|
270
|
+
this.armPasteTailTimer();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private armPasteTailTimer(): void {
|
|
274
|
+
if (!this.pendingPasteWork) {
|
|
275
|
+
this.pendingPasteWork = new Promise<void>((resolve) => {
|
|
276
|
+
this.resolvePendingPasteWork = resolve;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
if (this.pasteTailTimer) clearTimeout(this.pasteTailTimer);
|
|
280
|
+
this.pasteTailTimer = setTimeout(() => this.flushPendingPasteInputs(), PASTE_TAIL_GUARD_MS);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private flushPendingPasteInputs(): void {
|
|
284
|
+
const resolve = this.detachPendingPasteWork();
|
|
285
|
+
const inputs = this.deferredPasteInputs;
|
|
286
|
+
this.deferredPasteInputs = [];
|
|
287
|
+
this.editor.commitPendingPaste();
|
|
288
|
+
for (const input of inputs) {
|
|
289
|
+
if (this.finished) break;
|
|
290
|
+
this.dispatchInput(input);
|
|
291
|
+
}
|
|
292
|
+
resolve?.();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private cancelPendingPasteWork(): void {
|
|
296
|
+
const resolve = this.detachPendingPasteWork();
|
|
297
|
+
this.deferredPasteInputs = [];
|
|
298
|
+
resolve?.();
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private detachPendingPasteWork(): (() => void) | undefined {
|
|
302
|
+
if (this.pasteTailTimer) clearTimeout(this.pasteTailTimer);
|
|
303
|
+
this.pasteTailTimer = undefined;
|
|
304
|
+
const resolve = this.resolvePendingPasteWork;
|
|
305
|
+
this.resolvePendingPasteWork = undefined;
|
|
306
|
+
this.pendingPasteWork = undefined;
|
|
307
|
+
return resolve;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
private finish(value: string | undefined): void {
|
|
311
|
+
if (this.finished) return;
|
|
312
|
+
this.finished = true;
|
|
313
|
+
this.cancelPendingPasteWork();
|
|
314
|
+
this.editor.dispose();
|
|
315
|
+
this.onDone(value);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private hintText(): string {
|
|
319
|
+
const groups = [
|
|
320
|
+
keyHint(this.keybindings, "tui.input.submit", "save"),
|
|
321
|
+
keyHint(this.keybindings, "tui.input.newLine", "newline"),
|
|
322
|
+
"ctrl+c cancel",
|
|
323
|
+
].filter(Boolean);
|
|
324
|
+
return groups.join(" ");
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
class RawPreservingEditor implements Focusable {
|
|
329
|
+
private editor: Editor;
|
|
330
|
+
private readonly rawByMarker = new Map<string, { raw: string; hidden: boolean }>();
|
|
331
|
+
private readonly markerByVisibleRaw = new Map<string, string>();
|
|
332
|
+
private readonly reservedRawCharacters = new Set<string>();
|
|
333
|
+
private markerCodePoint = 0xe000;
|
|
334
|
+
private pasteBuffer: string | undefined;
|
|
335
|
+
private pasteSnapshot: string | undefined;
|
|
336
|
+
private pasteTailGuarded = false;
|
|
337
|
+
private pasteSubmissionArmed = false;
|
|
338
|
+
private pasteError: string | undefined;
|
|
339
|
+
|
|
340
|
+
constructor(
|
|
341
|
+
private readonly tui: TUI,
|
|
342
|
+
private readonly theme: EditorTheme,
|
|
343
|
+
) {
|
|
344
|
+
this.editor = this.createEditor();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
get focused(): boolean {
|
|
348
|
+
return this.editor.focused;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
set focused(value: boolean) {
|
|
352
|
+
this.editor.focused = value;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
get isPasting(): boolean {
|
|
356
|
+
return this.pasteBuffer !== undefined;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
get hasPendingPaste(): boolean {
|
|
360
|
+
return this.pasteSnapshot !== undefined && this.pasteTailGuarded && this.pasteBuffer === undefined;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
handleInput(data: string): string | undefined {
|
|
364
|
+
if (this.pasteBuffer !== undefined) {
|
|
365
|
+
this.pasteBuffer += data;
|
|
366
|
+
return this.flushPasteBuffer();
|
|
367
|
+
}
|
|
368
|
+
const pasteStart = data.indexOf(BRACKETED_PASTE_START);
|
|
369
|
+
if (pasteStart >= 0) {
|
|
370
|
+
if (pasteStart > 0) this.handleInput(data.slice(0, pasteStart));
|
|
371
|
+
this.pasteSnapshot ??= this.getExpandedText();
|
|
372
|
+
this.pasteTailGuarded = false;
|
|
373
|
+
this.pasteSubmissionArmed = false;
|
|
374
|
+
this.pasteError = undefined;
|
|
375
|
+
this.pasteBuffer = data.slice(pasteStart + BRACKETED_PASTE_START.length);
|
|
376
|
+
return this.flushPasteBuffer();
|
|
377
|
+
}
|
|
378
|
+
if (data.includes(BRACKETED_PASTE_END) && this.pasteSnapshot !== undefined) {
|
|
379
|
+
this.rejectPendingPaste();
|
|
380
|
+
return undefined;
|
|
381
|
+
}
|
|
382
|
+
this.pasteSubmissionArmed = false;
|
|
383
|
+
this.pasteError = undefined;
|
|
384
|
+
const parsedKey = parseKey(data);
|
|
385
|
+
if (parsedKey === "#") {
|
|
386
|
+
this.editor.handleInput(this.encode(parsedKey));
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
if (parsedKey === undefined) {
|
|
390
|
+
this.reserveRawCharacters(data);
|
|
391
|
+
if ([...data].some((character) => needsRawMarker(character, this.rawByMarker))) {
|
|
392
|
+
this.editor.handleInput(this.encode(data));
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
this.editor.handleInput(data);
|
|
397
|
+
return undefined;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
401
|
+
this.pasteSubmissionArmed = false;
|
|
402
|
+
return this.editor.handleMouse(event);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
render(width: number): string[] {
|
|
406
|
+
return this.editor.render(width).map((line) =>
|
|
407
|
+
[...line]
|
|
408
|
+
.map((character) => {
|
|
409
|
+
const marker = this.rawByMarker.get(character);
|
|
410
|
+
return marker ? (marker.hidden ? " " : marker.raw) : character;
|
|
411
|
+
})
|
|
412
|
+
.join(""),
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
invalidate(): void {
|
|
417
|
+
this.editor.invalidate();
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
setText(value: string): void {
|
|
421
|
+
this.rawByMarker.clear();
|
|
422
|
+
this.markerByVisibleRaw.clear();
|
|
423
|
+
this.reservedRawCharacters.clear();
|
|
424
|
+
this.markerCodePoint = 0xe000;
|
|
425
|
+
this.pasteBuffer = undefined;
|
|
426
|
+
this.pasteSnapshot = undefined;
|
|
427
|
+
this.pasteTailGuarded = false;
|
|
428
|
+
this.pasteSubmissionArmed = false;
|
|
429
|
+
this.pasteError = undefined;
|
|
430
|
+
this.editor.setText(this.encode(value));
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
getExpandedText(): string {
|
|
434
|
+
return this.decode(this.editor.getExpandedText());
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
getPasteError(): string | undefined {
|
|
438
|
+
return this.pasteError;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
hasHiddenCharacters(): boolean {
|
|
442
|
+
return [...this.editor.getExpandedText()].some((character) => this.rawByMarker.get(character)?.hidden);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
hasBackslashBeforeCursor(): boolean {
|
|
446
|
+
const cursor = this.editor.getCursor();
|
|
447
|
+
const line = this.editor.getLines()[cursor.line] ?? "";
|
|
448
|
+
return cursor.col > 0 && line[cursor.col - 1] === "\\";
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
handleKeyInput(data: string): void {
|
|
452
|
+
this.pasteSubmissionArmed = false;
|
|
453
|
+
this.pasteError = undefined;
|
|
454
|
+
this.editor.handleInput(data);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
replaceBackslashWithNewline(): void {
|
|
458
|
+
this.editor.handleInput(Key.backspace);
|
|
459
|
+
this.editor.insertTextAtCursor("\n");
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
commitPendingPaste(): void {
|
|
463
|
+
this.pasteTailGuarded = false;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
confirmPasteSubmission(): boolean {
|
|
467
|
+
if (this.pasteSnapshot === undefined) return true;
|
|
468
|
+
if (!this.pasteSubmissionArmed) {
|
|
469
|
+
this.pasteSubmissionArmed = true;
|
|
470
|
+
this.pasteError = "Paste boundary pending; review the content and press save again to confirm.";
|
|
471
|
+
return false;
|
|
472
|
+
}
|
|
473
|
+
this.pasteSnapshot = undefined;
|
|
474
|
+
this.pasteTailGuarded = false;
|
|
475
|
+
this.pasteSubmissionArmed = false;
|
|
476
|
+
this.pasteError = undefined;
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
rejectPendingPaste(): void {
|
|
481
|
+
const snapshot = this.pasteSnapshot;
|
|
482
|
+
if (snapshot === undefined) return;
|
|
483
|
+
this.restorePasteSnapshot(snapshot);
|
|
484
|
+
this.pasteError = "Paste rejected because it contains an ambiguous bracketed-paste terminator.";
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
dispose(): void {
|
|
488
|
+
this.editor.focused = false;
|
|
489
|
+
this.pasteBuffer = undefined;
|
|
490
|
+
this.pasteSnapshot = undefined;
|
|
491
|
+
this.pasteTailGuarded = false;
|
|
492
|
+
this.pasteSubmissionArmed = false;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
private flushPasteBuffer(): string | undefined {
|
|
496
|
+
if (this.pasteBuffer === undefined) return undefined;
|
|
497
|
+
const pasteEnd = this.pasteBuffer.indexOf(BRACKETED_PASTE_END);
|
|
498
|
+
if (pasteEnd < 0) return undefined;
|
|
499
|
+
if (this.pasteBuffer.includes(BRACKETED_PASTE_END, pasteEnd + BRACKETED_PASTE_END.length)) {
|
|
500
|
+
this.rejectPendingPaste();
|
|
501
|
+
return undefined;
|
|
502
|
+
}
|
|
503
|
+
const raw = this.pasteBuffer.slice(0, pasteEnd);
|
|
504
|
+
const remaining = this.pasteBuffer.slice(pasteEnd + BRACKETED_PASTE_END.length);
|
|
505
|
+
this.pasteBuffer = undefined;
|
|
506
|
+
const decoded = decodeTmuxPasteControls(raw);
|
|
507
|
+
this.editor.handleInput(`${BRACKETED_PASTE_START}${this.encode(decoded)}${BRACKETED_PASTE_END}`);
|
|
508
|
+
this.pasteTailGuarded = true;
|
|
509
|
+
return remaining || undefined;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
private restorePasteSnapshot(value: string): void {
|
|
513
|
+
const focused = this.editor.focused;
|
|
514
|
+
this.rawByMarker.clear();
|
|
515
|
+
this.markerByVisibleRaw.clear();
|
|
516
|
+
this.reservedRawCharacters.clear();
|
|
517
|
+
this.markerCodePoint = 0xe000;
|
|
518
|
+
this.pasteBuffer = undefined;
|
|
519
|
+
this.pasteSnapshot = undefined;
|
|
520
|
+
this.pasteTailGuarded = false;
|
|
521
|
+
this.pasteSubmissionArmed = false;
|
|
522
|
+
this.pasteError = undefined;
|
|
523
|
+
this.editor = this.createEditor();
|
|
524
|
+
this.editor.focused = focused;
|
|
525
|
+
this.editor.setText(this.encode(value));
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
private createEditor(): Editor {
|
|
529
|
+
const editor = new Editor(this.tui, this.theme, { paddingX: 0 });
|
|
530
|
+
editor.disableSubmit = true;
|
|
531
|
+
return editor;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
private encode(value: string): string {
|
|
535
|
+
this.reserveRawCharacters(value);
|
|
536
|
+
const forbidden = new Set([
|
|
537
|
+
...this.reservedRawCharacters,
|
|
538
|
+
...this.editor.getExpandedText(),
|
|
539
|
+
...this.rawByMarker.keys(),
|
|
540
|
+
]);
|
|
541
|
+
return [...value]
|
|
542
|
+
.map((character) => {
|
|
543
|
+
if (!needsRawMarker(character, this.rawByMarker)) return character;
|
|
544
|
+
const hidden = isUnsafeEditorCharacter(character);
|
|
545
|
+
const existing = hidden ? undefined : this.markerByVisibleRaw.get(character);
|
|
546
|
+
if (existing) return existing;
|
|
547
|
+
const marker = this.nextMarker(forbidden);
|
|
548
|
+
this.rawByMarker.set(marker, { raw: character, hidden });
|
|
549
|
+
if (!hidden) this.markerByVisibleRaw.set(character, marker);
|
|
550
|
+
forbidden.add(marker);
|
|
551
|
+
return marker;
|
|
552
|
+
})
|
|
553
|
+
.join("");
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
private decode(value: string): string {
|
|
557
|
+
return [...value].map((character) => this.rawByMarker.get(character)?.raw ?? character).join("");
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
private reserveRawCharacters(value: string): void {
|
|
561
|
+
for (const character of value) this.reservedRawCharacters.add(character);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
private nextMarker(forbidden: ReadonlySet<string>): string {
|
|
565
|
+
for (;;) {
|
|
566
|
+
if (this.markerCodePoint === 0xf900) this.markerCodePoint = 0xf0000;
|
|
567
|
+
if (this.markerCodePoint === 0xffffe) this.markerCodePoint = 0x100000;
|
|
568
|
+
if (this.markerCodePoint > 0x10fffd) throw new Error("Template editor exhausted its safe input markers");
|
|
569
|
+
const marker = String.fromCodePoint(this.markerCodePoint++);
|
|
570
|
+
if (!forbidden.has(marker)) return marker;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function isEditorActionBeforeNewline(data: string, keybindings: KeybindingsManager): boolean {
|
|
576
|
+
return (
|
|
577
|
+
EDITOR_ACTIONS_BEFORE_NEWLINE.some((action) => keybindings.matches(data, action)) ||
|
|
578
|
+
matchesKey(data, "shift+backspace") ||
|
|
579
|
+
matchesKey(data, "shift+delete")
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function isFocusedEditorInput(data: string, keybindings: KeybindingsManager): boolean {
|
|
584
|
+
return (
|
|
585
|
+
EDITOR_PRIORITY_ACTIONS.some((action) => keybindings.matches(data, action)) ||
|
|
586
|
+
matchesKey(data, "shift+backspace") ||
|
|
587
|
+
matchesKey(data, "shift+delete") ||
|
|
588
|
+
matchesKey(data, "shift+space") ||
|
|
589
|
+
isEditorNewlineInput(data, keybindings) ||
|
|
590
|
+
isEditorPrintableInput(data)
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function isEditorNewlineInput(data: string, keybindings: KeybindingsManager): boolean {
|
|
595
|
+
return (
|
|
596
|
+
keybindings.matches(data, "tui.input.newLine") ||
|
|
597
|
+
(data.charCodeAt(0) === 10 && data.length > 1) ||
|
|
598
|
+
data === "\u001b\r" ||
|
|
599
|
+
data === "\u001b[13;2~" ||
|
|
600
|
+
(data.length > 1 && data.includes("\u001b") && data.includes("\r")) ||
|
|
601
|
+
data === "\n"
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function isEditorPrintableInput(data: string): boolean {
|
|
606
|
+
if (decodeKittyPrintable(data) !== undefined || data.charCodeAt(0) >= 32) return true;
|
|
607
|
+
const modifyOtherKeysPrefix = "\u001b[27;";
|
|
608
|
+
if (!data.startsWith(modifyOtherKeysPrefix)) return false;
|
|
609
|
+
const match = data.slice(modifyOtherKeysPrefix.length).match(/^(\d+);(\d+)~$/u);
|
|
610
|
+
if (!match) return false;
|
|
611
|
+
const modifier = (Number.parseInt(match[1] ?? "", 10) - 1) & ~(64 | 128);
|
|
612
|
+
const codepoint = Number.parseInt(match[2] ?? "", 10);
|
|
613
|
+
if ((modifier & ~1) !== 0 || !Number.isFinite(codepoint) || codepoint < 32) return false;
|
|
614
|
+
try {
|
|
615
|
+
String.fromCodePoint(codepoint);
|
|
616
|
+
return true;
|
|
617
|
+
} catch {
|
|
618
|
+
return false;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
function decodeTmuxPasteControls(value: string): string {
|
|
623
|
+
return value.replace(TMUX_PASTE_CONTROL_PATTERN, (match, code: string) => {
|
|
624
|
+
const codePoint = Number(code);
|
|
625
|
+
if (codePoint >= 97 && codePoint <= 122) return String.fromCharCode(codePoint - 96);
|
|
626
|
+
if (codePoint >= 65 && codePoint <= 90) return String.fromCharCode(codePoint - 64);
|
|
627
|
+
return match;
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function keyHint(
|
|
632
|
+
keybindings: KeybindingsManager,
|
|
633
|
+
binding: "tui.input.submit" | "tui.input.newLine",
|
|
634
|
+
label: string,
|
|
635
|
+
): string {
|
|
636
|
+
const keys = keybindings.getKeys(binding);
|
|
637
|
+
return keys.length > 0 ? `${sanitizeTerminalText(keys.join("/"))} ${label}` : "";
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function needsRawMarker(character: string, rawByMarker: ReadonlyMap<string, unknown>): boolean {
|
|
641
|
+
return character === "#" || isUnsafeEditorCharacter(character) || rawByMarker.has(character);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function isUnsafeEditorCharacter(character: string): boolean {
|
|
645
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
646
|
+
return (
|
|
647
|
+
character !== "\n" &&
|
|
648
|
+
(codePoint <= 0x1f ||
|
|
649
|
+
(codePoint >= 0x7f && codePoint <= 0x9f) ||
|
|
650
|
+
codePoint === 0x2028 ||
|
|
651
|
+
codePoint === 0x2029 ||
|
|
652
|
+
isBidiControl(codePoint))
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function isBidiControl(codePoint: number): boolean {
|
|
657
|
+
return (
|
|
658
|
+
codePoint === 0x061c ||
|
|
659
|
+
codePoint === 0x200e ||
|
|
660
|
+
codePoint === 0x200f ||
|
|
661
|
+
(codePoint >= 0x202a && codePoint <= 0x202e) ||
|
|
662
|
+
(codePoint >= 0x2066 && codePoint <= 0x2069)
|
|
663
|
+
);
|
|
664
|
+
}
|