pi-render-btw 0.5.0 → 0.6.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/extensions/btw.ts +34 -50
- package/extensions/text-area.ts +460 -0
- package/package.json +1 -1
package/extensions/btw.ts
CHANGED
|
@@ -15,7 +15,6 @@ import { type AssistantMessage, type Message, type ThinkingLevel as AiThinkingLe
|
|
|
15
15
|
import {
|
|
16
16
|
Box,
|
|
17
17
|
Container,
|
|
18
|
-
Input,
|
|
19
18
|
Key,
|
|
20
19
|
Markdown,
|
|
21
20
|
Text,
|
|
@@ -28,6 +27,7 @@ import {
|
|
|
28
27
|
type OverlayHandle,
|
|
29
28
|
type TUI,
|
|
30
29
|
} from "@earendil-works/pi-tui";
|
|
30
|
+
import { TextArea } from "./text-area.ts";
|
|
31
31
|
|
|
32
32
|
const BTW_MESSAGE_TYPE = "btw-note";
|
|
33
33
|
const BTW_ENTRY_TYPE = "btw-thread-entry";
|
|
@@ -1003,8 +1003,9 @@ function notify(ctx: ExtensionContext | ExtensionCommandContext, message: string
|
|
|
1003
1003
|
}
|
|
1004
1004
|
}
|
|
1005
1005
|
|
|
1006
|
-
/**
|
|
1007
|
-
|
|
1006
|
+
/** Minimum rows for fixed overlay chrome outside the transcript viewport (without input lines).
|
|
1007
|
+
* render() computes the actual value dynamically: chrome = 8 + inputLines. */
|
|
1008
|
+
const BTW_OVERLAY_CHROME_BASE = 8;
|
|
1008
1009
|
|
|
1009
1010
|
function getOverlayTitle(mode: BtwThreadMode): string {
|
|
1010
1011
|
return mode === "tangent" ? "BTW tangent" : "BTW";
|
|
@@ -1020,7 +1021,7 @@ function buildTranscriptBadge(
|
|
|
1020
1021
|
}
|
|
1021
1022
|
|
|
1022
1023
|
class BtwOverlayComponent extends Container implements Focusable {
|
|
1023
|
-
private readonly input:
|
|
1024
|
+
private readonly input: TextArea;
|
|
1024
1025
|
private readonly transcript: Container;
|
|
1025
1026
|
private readonly statusText: Text;
|
|
1026
1027
|
private readonly modeText: Text;
|
|
@@ -1081,7 +1082,7 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1081
1082
|
this.transcript = new Container();
|
|
1082
1083
|
this.statusText = new Text("", 1, 0);
|
|
1083
1084
|
|
|
1084
|
-
this.input = new
|
|
1085
|
+
this.input = new TextArea();
|
|
1085
1086
|
this.input.onSubmit = (value) => {
|
|
1086
1087
|
this.followTranscript = true;
|
|
1087
1088
|
this.onSubmitCallback(value);
|
|
@@ -1098,22 +1099,20 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1098
1099
|
|
|
1099
1100
|
const originalHandleInput = this.input.handleInput.bind(this.input);
|
|
1100
1101
|
this.input.handleInput = (data: string) => {
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
return;
|
|
1106
|
-
}
|
|
1107
|
-
|
|
1108
|
-
this.onDismissCallback();
|
|
1102
|
+
// app.clear with non-empty input → clear input
|
|
1103
|
+
if (keybindings.matches(data, "app.clear") && this.input.getValue().length > 0) {
|
|
1104
|
+
this.input.setValue("");
|
|
1105
|
+
this.tui.requestRender();
|
|
1109
1106
|
return;
|
|
1110
1107
|
}
|
|
1111
1108
|
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
return;
|
|
1115
|
-
}
|
|
1109
|
+
// Everything else (including Escape → dismiss, Enter → submit,
|
|
1110
|
+
// Ctrl+Enter → newline) goes to TextArea's own handler.
|
|
1116
1111
|
originalHandleInput(data);
|
|
1112
|
+
|
|
1113
|
+
// After TextArea processed the key, check if Escape was pressed
|
|
1114
|
+
// (TextArea calls onEscape which calls onDismissCallback).
|
|
1115
|
+
// tui.select.cancel is handled by TextArea internally.
|
|
1117
1116
|
};
|
|
1118
1117
|
|
|
1119
1118
|
this.refresh();
|
|
@@ -1253,9 +1252,11 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1253
1252
|
return allLines;
|
|
1254
1253
|
}
|
|
1255
1254
|
|
|
1256
|
-
private getDialogHeight(): number {
|
|
1255
|
+
private getDialogHeight(inputLines: number): number {
|
|
1257
1256
|
const terminalRows = process.stdout.rows ?? 30;
|
|
1258
|
-
|
|
1257
|
+
const minHeight = Math.max(14, 8 + inputLines + 6); // chrome + input + min transcript
|
|
1258
|
+
const maxHeight = Math.min(40, Math.floor(terminalRows * 0.82));
|
|
1259
|
+
return Math.max(minHeight, Math.min(maxHeight, Math.floor(terminalRows * 0.78)));
|
|
1259
1260
|
}
|
|
1260
1261
|
|
|
1261
1262
|
private scrollTranscript(delta: number): void {
|
|
@@ -1296,37 +1297,15 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1296
1297
|
return;
|
|
1297
1298
|
}
|
|
1298
1299
|
|
|
1299
|
-
if (matchesKey(data, Key.pageUp) || matchesKey(data, Key.up)) {
|
|
1300
|
-
const step = matchesKey(data, Key.pageUp) ? Math.max(1, this.transcriptViewportHeight - 1) : 1;
|
|
1301
|
-
this.scrollTranscript(-step);
|
|
1302
|
-
return;
|
|
1303
|
-
}
|
|
1304
|
-
|
|
1305
|
-
if (matchesKey(data, Key.pageDown) || matchesKey(data, Key.down)) {
|
|
1306
|
-
const step = matchesKey(data, Key.pageDown) ? Math.max(1, this.transcriptViewportHeight - 1) : 1;
|
|
1307
|
-
this.scrollTranscript(step);
|
|
1308
|
-
return;
|
|
1309
|
-
}
|
|
1310
|
-
|
|
1311
1300
|
this.input.handleInput(data);
|
|
1312
1301
|
}
|
|
1313
1302
|
|
|
1314
|
-
private
|
|
1315
|
-
const
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
// the row stays geometrically stable while the overlay still owns keyboard input.
|
|
1321
|
-
this.input.focused = false;
|
|
1322
|
-
try {
|
|
1323
|
-
const renderedInputLine = this.input.render(targetWidth)[0] ?? "";
|
|
1324
|
-
const inputLine = truncateToWidth(renderedInputLine, targetWidth, "");
|
|
1325
|
-
const padding = Math.max(0, targetWidth - visibleWidth(inputLine));
|
|
1326
|
-
return `${this.theme.fg("border", "│")}${inputLine}${" ".repeat(padding)}${this.theme.fg("border", "│")}`;
|
|
1327
|
-
} finally {
|
|
1328
|
-
this.input.focused = previousFocused;
|
|
1329
|
-
}
|
|
1303
|
+
private inputFrameLines(innerWidth: number): string[] {
|
|
1304
|
+
const lines = this.input.render(innerWidth);
|
|
1305
|
+
return lines.map((l) => {
|
|
1306
|
+
const pad = Math.max(0, innerWidth - visibleWidth(l));
|
|
1307
|
+
return `${this.theme.fg("border", "│")}${l}${" ".repeat(pad)}${this.theme.fg("border", "│")}`;
|
|
1308
|
+
});
|
|
1330
1309
|
}
|
|
1331
1310
|
|
|
1332
1311
|
private fitRenderedLine(line: string, width: number): string {
|
|
@@ -1336,9 +1315,14 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1336
1315
|
override render(width: number): string[] {
|
|
1337
1316
|
const dialogWidth = Math.max(24, width);
|
|
1338
1317
|
const innerWidth = Math.max(22, dialogWidth - 2);
|
|
1318
|
+
|
|
1319
|
+
// Compute input lines (cap at 6 for layout stability)
|
|
1320
|
+
const rawInputLines = this.input.render(innerWidth);
|
|
1321
|
+
const inputLineCount = Math.max(1, Math.min(rawInputLines.length, 6));
|
|
1322
|
+
|
|
1339
1323
|
const transcriptLines = this.buildTranscriptLines(innerWidth);
|
|
1340
|
-
const
|
|
1341
|
-
const
|
|
1324
|
+
const chromeHeight = BTW_OVERLAY_CHROME_BASE + inputLineCount;
|
|
1325
|
+
const dialogHeight = this.getDialogHeight(inputLineCount);
|
|
1342
1326
|
const transcriptHeight = Math.max(6, dialogHeight - chromeHeight);
|
|
1343
1327
|
this.transcriptViewportHeight = transcriptHeight;
|
|
1344
1328
|
|
|
@@ -1379,7 +1363,7 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1379
1363
|
|
|
1380
1364
|
lines.push(this.ruleLine(innerWidth));
|
|
1381
1365
|
lines.push(this.frameLine(this.theme.fg("warning", this.statusTextValue.trim()), innerWidth));
|
|
1382
|
-
lines.push(this.
|
|
1366
|
+
lines.push(...this.inputFrameLines(innerWidth));
|
|
1383
1367
|
lines.push(this.frameLine(this.theme.fg("dim", this.hintsTextValue.trim()), innerWidth));
|
|
1384
1368
|
lines.push(this.borderLine(innerWidth, "bottom"));
|
|
1385
1369
|
|
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
import { getKeybindings, CURSOR_MARKER, visibleWidth, decodeKittyPrintable, matchesKey } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
4
|
+
|
|
5
|
+
// ── Helpers (re-implemented to avoid non-public pi-tui imports) ───────
|
|
6
|
+
|
|
7
|
+
/** Check if a character is whitespace. */
|
|
8
|
+
function isWs(ch: string): boolean {
|
|
9
|
+
return /\s/.test(ch);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Find word boundary before cursor. */
|
|
13
|
+
function wordBackward(text: string, cursor: number): number {
|
|
14
|
+
if (cursor <= 0) return 0;
|
|
15
|
+
let pos = cursor;
|
|
16
|
+
// Skip trailing whitespace
|
|
17
|
+
while (pos > 0 && isWs(text[pos - 1])) pos--;
|
|
18
|
+
if (pos === 0) return 0;
|
|
19
|
+
// Check if char before is punctuation
|
|
20
|
+
const prev = text[pos - 1];
|
|
21
|
+
if (/[(){}[\]<>.,;:'"!?+\-=*/\\|&%^$#@~`]/.test(prev)) {
|
|
22
|
+
return pos - 1;
|
|
23
|
+
}
|
|
24
|
+
// Skip word chars (alphanumeric/underscore)
|
|
25
|
+
while (pos > 0 && /\w/.test(text[pos - 1])) pos--;
|
|
26
|
+
return pos;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Find word boundary after cursor. */
|
|
30
|
+
function wordForward(text: string, cursor: number): number {
|
|
31
|
+
if (cursor >= text.length) return text.length;
|
|
32
|
+
let pos = cursor;
|
|
33
|
+
// Skip leading whitespace
|
|
34
|
+
while (pos < text.length && isWs(text[pos])) pos++;
|
|
35
|
+
if (pos >= text.length) return text.length;
|
|
36
|
+
// Check if char is punctuation
|
|
37
|
+
if (/[(){}[\]<>.,;:'"!?+\-=*/\\|&%^$#@~`]/.test(text[pos])) {
|
|
38
|
+
return pos + 1;
|
|
39
|
+
}
|
|
40
|
+
// Skip word chars
|
|
41
|
+
while (pos < text.length && /\w/.test(text[pos])) pos++;
|
|
42
|
+
return pos;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class KillRing {
|
|
46
|
+
private ring: string[] = [];
|
|
47
|
+
push(text: string, opts: { prepend?: boolean; accumulate?: boolean }): void {
|
|
48
|
+
if (!text) return;
|
|
49
|
+
if (opts.accumulate && this.ring.length > 0) {
|
|
50
|
+
const last = this.ring.pop()!;
|
|
51
|
+
this.ring.push(opts.prepend ? text + last : last + text);
|
|
52
|
+
} else {
|
|
53
|
+
this.ring.push(text);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
peek(): string | undefined { return this.ring[this.ring.length - 1]; }
|
|
57
|
+
rotate(): void {
|
|
58
|
+
if (this.ring.length > 1) this.ring.unshift(this.ring.pop()!);
|
|
59
|
+
}
|
|
60
|
+
get length(): number { return this.ring.length; }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class UndoStack {
|
|
64
|
+
private stack: Array<{ value: string; cursor: number }> = [];
|
|
65
|
+
push(state: { value: string; cursor: number }): void {
|
|
66
|
+
this.stack.push({ value: state.value, cursor: state.cursor });
|
|
67
|
+
}
|
|
68
|
+
pop(): { value: string; cursor: number } | undefined { return this.stack.pop(); }
|
|
69
|
+
clear(): void { this.stack.length = 0; }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ── TextArea Component ───────────────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Multi-line text area with pi-style editing keybindings.
|
|
76
|
+
*
|
|
77
|
+
* - All editing shortcuts: Ctrl+W (delete word), Ctrl+A/E (line start/end),
|
|
78
|
+
* Ctrl+U/K (delete to line start/end), cursor word navigation, undo, yank.
|
|
79
|
+
* - Enter → calls onSubmit(value).
|
|
80
|
+
* - Ctrl+Enter / Ctrl+J → insert newline.
|
|
81
|
+
* - render(width) returns lines for the overlay to embed.
|
|
82
|
+
*/
|
|
83
|
+
export class TextArea {
|
|
84
|
+
value = "";
|
|
85
|
+
cursor = 0;
|
|
86
|
+
focused = false;
|
|
87
|
+
onSubmit: ((value: string) => void) | undefined;
|
|
88
|
+
onEscape: (() => void) | undefined;
|
|
89
|
+
|
|
90
|
+
private pasteBuffer = "";
|
|
91
|
+
private isInPaste = false;
|
|
92
|
+
private killRing = new KillRing();
|
|
93
|
+
private lastAction: string | null = null;
|
|
94
|
+
private undoStack = new UndoStack();
|
|
95
|
+
|
|
96
|
+
getValue(): string { return this.value; }
|
|
97
|
+
|
|
98
|
+
setValue(v: string): void {
|
|
99
|
+
this.value = v;
|
|
100
|
+
this.cursor = Math.min(this.cursor, v.length);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
handleInput(data: string): void {
|
|
104
|
+
// Bracketed paste
|
|
105
|
+
if (data.includes("\x1b[200~")) {
|
|
106
|
+
this.isInPaste = true;
|
|
107
|
+
this.pasteBuffer = "";
|
|
108
|
+
data = data.replace("\x1b[200~", "");
|
|
109
|
+
}
|
|
110
|
+
if (this.isInPaste) {
|
|
111
|
+
this.pasteBuffer += data;
|
|
112
|
+
const end = this.pasteBuffer.indexOf("\x1b[201~");
|
|
113
|
+
if (end !== -1) {
|
|
114
|
+
this.handlePaste(this.pasteBuffer.slice(0, end));
|
|
115
|
+
this.isInPaste = false;
|
|
116
|
+
const rem = this.pasteBuffer.slice(end + 6);
|
|
117
|
+
this.pasteBuffer = "";
|
|
118
|
+
if (rem) this.handleInput(rem);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const kb = getKeybindings();
|
|
124
|
+
|
|
125
|
+
// Escape
|
|
126
|
+
if (kb.matches(data, "tui.select.cancel")) {
|
|
127
|
+
this.onEscape?.();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Undo
|
|
132
|
+
if (kb.matches(data, "tui.editor.undo")) {
|
|
133
|
+
this.undo();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Ctrl+Enter / Ctrl+J → insert newline (check BEFORE plain Enter)
|
|
138
|
+
if (matchesKey(data, "ctrl+enter") || matchesKey(data, "ctrl+j") || data === "\x1b[13;5u") {
|
|
139
|
+
this.insertChar("\n");
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Submit (Enter)
|
|
144
|
+
if (kb.matches(data, "tui.input.submit") || data === "\r" || data === "\n") {
|
|
145
|
+
this.onSubmit?.(this.value);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Deletion
|
|
150
|
+
if (kb.matches(data, "tui.editor.deleteCharBackward")) { this.backspace(); return; }
|
|
151
|
+
if (kb.matches(data, "tui.editor.deleteCharForward")) { this.fwdDelete(); return; }
|
|
152
|
+
if (kb.matches(data, "tui.editor.deleteWordBackward")) { this.delWordBwd(); return; }
|
|
153
|
+
if (kb.matches(data, "tui.editor.deleteWordForward")) { this.delWordFwd(); return; }
|
|
154
|
+
if (kb.matches(data, "tui.editor.deleteToLineStart")) { this.delToLineStart(); return; }
|
|
155
|
+
if (kb.matches(data, "tui.editor.deleteToLineEnd")) { this.delToLineEnd(); return; }
|
|
156
|
+
|
|
157
|
+
// Yank
|
|
158
|
+
if (kb.matches(data, "tui.editor.yank")) { this.yank(); return; }
|
|
159
|
+
if (kb.matches(data, "tui.editor.yankPop")) { this.yankPop(); return; }
|
|
160
|
+
|
|
161
|
+
// Cursor
|
|
162
|
+
if (kb.matches(data, "tui.editor.cursorLeft")) { this.cursorLeft(); return; }
|
|
163
|
+
if (kb.matches(data, "tui.editor.cursorRight")) { this.cursorRight(); return; }
|
|
164
|
+
if (kb.matches(data, "tui.editor.cursorLineStart")) { this.cursorLineStart(); return; }
|
|
165
|
+
if (kb.matches(data, "tui.editor.cursorLineEnd")) { this.cursorLineEnd(); return; }
|
|
166
|
+
if (kb.matches(data, "tui.editor.cursorWordLeft")) { this.cursorWordLeft(); return; }
|
|
167
|
+
if (kb.matches(data, "tui.editor.cursorWordRight")) { this.cursorWordRight(); return; }
|
|
168
|
+
|
|
169
|
+
// Up/Down (between lines)
|
|
170
|
+
if (kb.matches(data, "tui.select.up")) { this.cursorUp(); return; }
|
|
171
|
+
if (kb.matches(data, "tui.select.down")) { this.cursorDown(); return; }
|
|
172
|
+
|
|
173
|
+
// Kitty printable
|
|
174
|
+
const kp = decodeKittyPrintable(data);
|
|
175
|
+
if (kp !== undefined) { this.insertChar(kp); return; }
|
|
176
|
+
|
|
177
|
+
// Regular printable chars
|
|
178
|
+
const hasCtrl = [...data].some(ch => {
|
|
179
|
+
const cc = ch.charCodeAt(0);
|
|
180
|
+
return cc < 32 || cc === 0x7f || (cc >= 0x80 && cc <= 0x9f);
|
|
181
|
+
});
|
|
182
|
+
if (!hasCtrl) this.insertChar(data);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ── Cursor ────────────────────────────────────────────────────────
|
|
186
|
+
|
|
187
|
+
private cursorLeft(): void {
|
|
188
|
+
this.lastAction = null;
|
|
189
|
+
if (this.cursor <= 0) return;
|
|
190
|
+
const before = this.value.slice(0, this.cursor);
|
|
191
|
+
const gs = [...segmenter.segment(before)];
|
|
192
|
+
this.cursor -= (gs[gs.length - 1]?.segment.length ?? 1);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private cursorRight(): void {
|
|
196
|
+
this.lastAction = null;
|
|
197
|
+
if (this.cursor >= this.value.length) return;
|
|
198
|
+
const gs = [...segmenter.segment(this.value.slice(this.cursor))];
|
|
199
|
+
this.cursor += (gs[0]?.segment.length ?? 1);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private cursorLineStart(): void {
|
|
203
|
+
this.lastAction = null;
|
|
204
|
+
const nl = this.value.slice(0, this.cursor).lastIndexOf("\n");
|
|
205
|
+
this.cursor = nl < 0 ? 0 : nl + 1;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private cursorLineEnd(): void {
|
|
209
|
+
this.lastAction = null;
|
|
210
|
+
const after = this.value.slice(this.cursor);
|
|
211
|
+
const nl = after.indexOf("\n");
|
|
212
|
+
this.cursor = nl < 0 ? this.value.length : this.cursor + nl;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private cursorWordLeft(): void {
|
|
216
|
+
this.lastAction = null;
|
|
217
|
+
if (this.cursor <= 0) return;
|
|
218
|
+
this.cursor = wordBackward(this.value, this.cursor);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private cursorWordRight(): void {
|
|
222
|
+
this.lastAction = null;
|
|
223
|
+
if (this.cursor >= this.value.length) return;
|
|
224
|
+
this.cursor = wordForward(this.value, this.cursor);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private cursorUp(): void {
|
|
228
|
+
this.lastAction = null;
|
|
229
|
+
const before = this.value.slice(0, this.cursor);
|
|
230
|
+
const nl = before.lastIndexOf("\n");
|
|
231
|
+
if (nl < 0) { this.cursor = 0; return; }
|
|
232
|
+
const prevNl = before.lastIndexOf("\n", nl - 1);
|
|
233
|
+
const prevLineStart = prevNl < 0 ? 0 : prevNl + 1;
|
|
234
|
+
const col = this.cursor - nl - 1;
|
|
235
|
+
const prevLine = this.value.slice(prevLineStart, nl);
|
|
236
|
+
// Try to keep visual column
|
|
237
|
+
let newCol = 0, vis = 0;
|
|
238
|
+
for (const ch of prevLine) { if (vis + (visibleWidth(ch)) > col) break; vis += visibleWidth(ch); newCol += ch.length; }
|
|
239
|
+
this.cursor = prevLineStart + newCol;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private cursorDown(): void {
|
|
243
|
+
this.lastAction = null;
|
|
244
|
+
const after = this.value.slice(this.cursor);
|
|
245
|
+
const nl = after.indexOf("\n");
|
|
246
|
+
if (nl < 0) { this.cursor = this.value.length; return; }
|
|
247
|
+
const nextLineStart = this.cursor + nl + 1;
|
|
248
|
+
const afterNext = this.value.slice(nextLineStart);
|
|
249
|
+
const nextNl = afterNext.indexOf("\n");
|
|
250
|
+
const nextLineEnd = nextNl < 0 ? this.value.length : nextLineStart + nextNl;
|
|
251
|
+
const currentLine = this.value.slice(this.getLineStart(), this.cursor);
|
|
252
|
+
const col = visibleWidth(currentLine);
|
|
253
|
+
const nextLine = this.value.slice(nextLineStart, nextLineEnd);
|
|
254
|
+
let newCol = 0, vis = 0;
|
|
255
|
+
for (const ch of nextLine) { if (vis + visibleWidth(ch) > col) break; vis += visibleWidth(ch); newCol += ch.length; }
|
|
256
|
+
this.cursor = Math.min(nextLineStart + newCol, nextLineEnd);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private getLineStart(): number {
|
|
260
|
+
const nl = this.value.slice(0, this.cursor).lastIndexOf("\n");
|
|
261
|
+
return nl < 0 ? 0 : nl + 1;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ── Editing ───────────────────────────────────────────────────────
|
|
265
|
+
|
|
266
|
+
private insertChar(ch: string): void {
|
|
267
|
+
if (isWs(ch) || this.lastAction !== "type-word") this.pushUndo();
|
|
268
|
+
this.lastAction = "type-word";
|
|
269
|
+
this.value = this.value.slice(0, this.cursor) + ch + this.value.slice(this.cursor);
|
|
270
|
+
this.cursor += ch.length;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private backspace(): void {
|
|
274
|
+
this.lastAction = null;
|
|
275
|
+
if (this.cursor <= 0) return;
|
|
276
|
+
this.pushUndo();
|
|
277
|
+
const before = this.value.slice(0, this.cursor);
|
|
278
|
+
const gs = [...segmenter.segment(before)];
|
|
279
|
+
const glen = (gs[gs.length - 1]?.segment.length ?? 1);
|
|
280
|
+
this.value = this.value.slice(0, this.cursor - glen) + this.value.slice(this.cursor);
|
|
281
|
+
this.cursor -= glen;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private fwdDelete(): void {
|
|
285
|
+
this.lastAction = null;
|
|
286
|
+
if (this.cursor >= this.value.length) return;
|
|
287
|
+
this.pushUndo();
|
|
288
|
+
const gs = [...segmenter.segment(this.value.slice(this.cursor))];
|
|
289
|
+
const glen = gs[0]?.segment.length ?? 1;
|
|
290
|
+
this.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + glen);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private delToLineStart(): void {
|
|
294
|
+
const start = this.getLineStart();
|
|
295
|
+
if (this.cursor <= start) return;
|
|
296
|
+
this.pushUndo();
|
|
297
|
+
const deleted = this.value.slice(start, this.cursor);
|
|
298
|
+
this.killRing.push(deleted, { prepend: true, accumulate: this.lastAction === "kill" });
|
|
299
|
+
this.lastAction = "kill";
|
|
300
|
+
this.value = this.value.slice(0, start) + this.value.slice(this.cursor);
|
|
301
|
+
this.cursor = start;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private delToLineEnd(): void {
|
|
305
|
+
const after = this.value.slice(this.cursor);
|
|
306
|
+
const nl = after.indexOf("\n");
|
|
307
|
+
const end = nl < 0 ? this.value.length : this.cursor + nl;
|
|
308
|
+
if (this.cursor >= end) return;
|
|
309
|
+
this.pushUndo();
|
|
310
|
+
const deleted = this.value.slice(this.cursor, end);
|
|
311
|
+
this.killRing.push(deleted, { prepend: false, accumulate: this.lastAction === "kill" });
|
|
312
|
+
this.lastAction = "kill";
|
|
313
|
+
this.value = this.value.slice(0, this.cursor) + this.value.slice(end);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private delWordBwd(): void {
|
|
317
|
+
if (this.cursor <= 0) return;
|
|
318
|
+
const wasKill = this.lastAction === "kill";
|
|
319
|
+
this.pushUndo();
|
|
320
|
+
const old = this.cursor;
|
|
321
|
+
this.cursor = wordBackward(this.value, this.cursor);
|
|
322
|
+
const from = this.cursor;
|
|
323
|
+
this.cursor = old;
|
|
324
|
+
const deleted = this.value.slice(from, this.cursor);
|
|
325
|
+
this.killRing.push(deleted, { prepend: true, accumulate: wasKill });
|
|
326
|
+
this.lastAction = "kill";
|
|
327
|
+
this.value = this.value.slice(0, from) + this.value.slice(this.cursor);
|
|
328
|
+
this.cursor = from;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private delWordFwd(): void {
|
|
332
|
+
if (this.cursor >= this.value.length) return;
|
|
333
|
+
const wasKill = this.lastAction === "kill";
|
|
334
|
+
this.pushUndo();
|
|
335
|
+
const old = this.cursor;
|
|
336
|
+
this.cursor = wordForward(this.value, this.cursor);
|
|
337
|
+
const to = this.cursor;
|
|
338
|
+
this.cursor = old;
|
|
339
|
+
const deleted = this.value.slice(this.cursor, to);
|
|
340
|
+
this.killRing.push(deleted, { prepend: false, accumulate: wasKill });
|
|
341
|
+
this.lastAction = "kill";
|
|
342
|
+
this.value = this.value.slice(0, this.cursor) + this.value.slice(to);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private yank(): void {
|
|
346
|
+
const text = this.killRing.peek();
|
|
347
|
+
if (!text) return;
|
|
348
|
+
this.pushUndo();
|
|
349
|
+
this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
|
|
350
|
+
this.cursor += text.length;
|
|
351
|
+
this.lastAction = "yank";
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
private yankPop(): void {
|
|
355
|
+
if (this.lastAction !== "yank" || this.killRing.length <= 1) return;
|
|
356
|
+
this.pushUndo();
|
|
357
|
+
const prev = this.killRing.peek() || "";
|
|
358
|
+
this.value = this.value.slice(0, this.cursor - prev.length) + this.value.slice(this.cursor);
|
|
359
|
+
this.cursor -= prev.length;
|
|
360
|
+
this.killRing.rotate();
|
|
361
|
+
const text = this.killRing.peek() || "";
|
|
362
|
+
this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
|
|
363
|
+
this.cursor += text.length;
|
|
364
|
+
this.lastAction = "yank";
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
private pushUndo(): void { this.undoStack.push({ value: this.value, cursor: this.cursor }); }
|
|
368
|
+
|
|
369
|
+
private undo(): void {
|
|
370
|
+
const snap = this.undoStack.pop();
|
|
371
|
+
if (!snap) return;
|
|
372
|
+
this.value = snap.value;
|
|
373
|
+
this.cursor = snap.cursor;
|
|
374
|
+
this.lastAction = null;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private handlePaste(text: string): void {
|
|
378
|
+
this.lastAction = null;
|
|
379
|
+
this.pushUndo();
|
|
380
|
+
const cleaned = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\t/g, " ");
|
|
381
|
+
this.value = this.value.slice(0, this.cursor) + cleaned + this.value.slice(this.cursor);
|
|
382
|
+
this.cursor += cleaned.length;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// ── Render ───────────────────────────────────────────────────────
|
|
386
|
+
|
|
387
|
+
invalidate(): void {}
|
|
388
|
+
|
|
389
|
+
render(width: number): string[] {
|
|
390
|
+
const avail = Math.max(1, width - 2); // room for "> "
|
|
391
|
+
|
|
392
|
+
// Handle empty value
|
|
393
|
+
if (!this.value) {
|
|
394
|
+
const marker = this.focused ? CURSOR_MARKER : "";
|
|
395
|
+
const cursor = `\x1b[7m \x1b[27m`;
|
|
396
|
+
return [`> ${marker}${cursor}${" ".repeat(Math.max(0, avail - 1))}`];
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const logicLines = this.value.split("\n");
|
|
400
|
+
const wrapLines: string[] = [];
|
|
401
|
+
|
|
402
|
+
for (const line of logicLines) {
|
|
403
|
+
if (!line) { wrapLines.push(""); continue; }
|
|
404
|
+
const w = visibleWidth(line);
|
|
405
|
+
if (w <= avail) { wrapLines.push(line); continue; }
|
|
406
|
+
// Simple greedy word-wrap
|
|
407
|
+
const words = line.split(/(\s+)/);
|
|
408
|
+
let cur = "", curW = 0;
|
|
409
|
+
for (const word of words) {
|
|
410
|
+
const wW = visibleWidth(word);
|
|
411
|
+
if (curW + wW > avail && cur) {
|
|
412
|
+
wrapLines.push(cur);
|
|
413
|
+
cur = word;
|
|
414
|
+
curW = wW;
|
|
415
|
+
} else {
|
|
416
|
+
cur += word;
|
|
417
|
+
curW += wW;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (cur) wrapLines.push(cur);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (wrapLines.length === 0) wrapLines.push("");
|
|
424
|
+
|
|
425
|
+
// Map cursor to render line
|
|
426
|
+
let cursorLine = 0, cursorCol = 0, remaining = this.cursor;
|
|
427
|
+
for (let wi = 0; wi < wrapLines.length; wi++) {
|
|
428
|
+
const len = wrapLines[wi].length;
|
|
429
|
+
const extra = (wi < wrapLines.length - 1) ? 1 : 0; // accounts for implicit newlines
|
|
430
|
+
if (remaining <= len + extra) {
|
|
431
|
+
cursorLine = wi;
|
|
432
|
+
cursorCol = Math.min(remaining, len);
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
remaining -= len + extra;
|
|
436
|
+
}
|
|
437
|
+
if (cursorLine >= wrapLines.length) {
|
|
438
|
+
cursorLine = Math.max(0, wrapLines.length - 1);
|
|
439
|
+
cursorCol = wrapLines[cursorLine].length;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const lines: string[] = [];
|
|
443
|
+
for (let wi = 0; wi < wrapLines.length; wi++) {
|
|
444
|
+
let text = wrapLines[wi];
|
|
445
|
+
const pad = Math.max(0, avail - visibleWidth(text));
|
|
446
|
+
|
|
447
|
+
if (wi === cursorLine && this.focused) {
|
|
448
|
+
const before = text.slice(0, cursorCol);
|
|
449
|
+
const at = text.slice(cursorCol, cursorCol + 1) || " ";
|
|
450
|
+
const after = text.slice(cursorCol + 1);
|
|
451
|
+
text = `${before}${CURSOR_MARKER}\x1b[7m${at}\x1b[27m${after}`;
|
|
452
|
+
lines.push(`> ${text}${" ".repeat(pad)}`);
|
|
453
|
+
} else {
|
|
454
|
+
lines.push(`> ${text}${" ".repeat(pad)}`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
return lines;
|
|
459
|
+
}
|
|
460
|
+
}
|