pi-render-btw 0.5.0 → 0.5.1
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 -38
- package/extensions/text-area.ts +463 -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 {
|
|
@@ -1311,22 +1312,12 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1311
1312
|
this.input.handleInput(data);
|
|
1312
1313
|
}
|
|
1313
1314
|
|
|
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
|
-
}
|
|
1315
|
+
private inputFrameLines(innerWidth: number): string[] {
|
|
1316
|
+
const lines = this.input.render(innerWidth);
|
|
1317
|
+
return lines.map((l) => {
|
|
1318
|
+
const pad = Math.max(0, innerWidth - visibleWidth(l));
|
|
1319
|
+
return `${this.theme.fg("border", "│")}${l}${" ".repeat(pad)}${this.theme.fg("border", "│")}`;
|
|
1320
|
+
});
|
|
1330
1321
|
}
|
|
1331
1322
|
|
|
1332
1323
|
private fitRenderedLine(line: string, width: number): string {
|
|
@@ -1336,9 +1327,14 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1336
1327
|
override render(width: number): string[] {
|
|
1337
1328
|
const dialogWidth = Math.max(24, width);
|
|
1338
1329
|
const innerWidth = Math.max(22, dialogWidth - 2);
|
|
1330
|
+
|
|
1331
|
+
// Compute input lines (cap at 6 for layout stability)
|
|
1332
|
+
const rawInputLines = this.input.render(innerWidth);
|
|
1333
|
+
const inputLineCount = Math.max(1, Math.min(rawInputLines.length, 6));
|
|
1334
|
+
|
|
1339
1335
|
const transcriptLines = this.buildTranscriptLines(innerWidth);
|
|
1340
|
-
const
|
|
1341
|
-
const
|
|
1336
|
+
const chromeHeight = BTW_OVERLAY_CHROME_BASE + inputLineCount;
|
|
1337
|
+
const dialogHeight = this.getDialogHeight(inputLineCount);
|
|
1342
1338
|
const transcriptHeight = Math.max(6, dialogHeight - chromeHeight);
|
|
1343
1339
|
this.transcriptViewportHeight = transcriptHeight;
|
|
1344
1340
|
|
|
@@ -1379,7 +1375,7 @@ class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1379
1375
|
|
|
1380
1376
|
lines.push(this.ruleLine(innerWidth));
|
|
1381
1377
|
lines.push(this.frameLine(this.theme.fg("warning", this.statusTextValue.trim()), innerWidth));
|
|
1382
|
-
lines.push(this.
|
|
1378
|
+
lines.push(...this.inputFrameLines(innerWidth));
|
|
1383
1379
|
lines.push(this.frameLine(this.theme.fg("dim", this.hintsTextValue.trim()), innerWidth));
|
|
1384
1380
|
lines.push(this.borderLine(innerWidth, "bottom"));
|
|
1385
1381
|
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { decodeKittyPrintable, getKeybindings, CURSOR_MARKER, getGraphemeSegmenter, isWhitespaceChar, sliceByColumn, visibleWidth, findWordBackward, findWordForward, KillRing, UndoStack } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
const segmenter = getGraphemeSegmenter();
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Multi-line text area with pi-style editing keybindings.
|
|
7
|
+
*
|
|
8
|
+
* - All editing shortcuts: Ctrl+W (delete word), Ctrl+A/E (line start/end),
|
|
9
|
+
* Ctrl+U/K (delete to line start/end), cursor word navigation, undo, yank.
|
|
10
|
+
* - Enter → calls onSubmit(value).
|
|
11
|
+
* - Ctrl+Enter / Ctrl+J → insert newline.
|
|
12
|
+
* - render(width) returns lines for the overlay to embed.
|
|
13
|
+
*/
|
|
14
|
+
export class TextArea {
|
|
15
|
+
value = "";
|
|
16
|
+
cursor = 0;
|
|
17
|
+
focused = false;
|
|
18
|
+
onSubmit: ((value: string) => void) | undefined;
|
|
19
|
+
onEscape: (() => void) | undefined;
|
|
20
|
+
|
|
21
|
+
// Paste buffering
|
|
22
|
+
private pasteBuffer = "";
|
|
23
|
+
private isInPaste = false;
|
|
24
|
+
|
|
25
|
+
// Kill ring + undo
|
|
26
|
+
private killRing = new KillRing();
|
|
27
|
+
private lastAction: string | null = null;
|
|
28
|
+
private undoStack = new UndoStack();
|
|
29
|
+
|
|
30
|
+
getValue(): string { return this.value; }
|
|
31
|
+
|
|
32
|
+
setValue(v: string): void {
|
|
33
|
+
this.value = v;
|
|
34
|
+
this.cursor = Math.min(this.cursor, v.length);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Number of visual lines when rendered at given width. */
|
|
38
|
+
getLineCount(width: number): number {
|
|
39
|
+
if (!this.value) return 1;
|
|
40
|
+
const lines = this.value.split("\n");
|
|
41
|
+
let count = 0;
|
|
42
|
+
for (const line of lines) {
|
|
43
|
+
if (!line) { count += 1; continue; }
|
|
44
|
+
const w = visibleWidth(line);
|
|
45
|
+
count += Math.max(1, Math.ceil(w / Math.max(1, width - 2)));
|
|
46
|
+
}
|
|
47
|
+
return Math.max(1, count);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
handleInput(data: string): void {
|
|
51
|
+
// Paste bracketing
|
|
52
|
+
if (data.includes("\x1b[200~")) {
|
|
53
|
+
this.isInPaste = true;
|
|
54
|
+
this.pasteBuffer = "";
|
|
55
|
+
data = data.replace("\x1b[200~", "");
|
|
56
|
+
}
|
|
57
|
+
if (this.isInPaste) {
|
|
58
|
+
this.pasteBuffer += data;
|
|
59
|
+
const end = this.pasteBuffer.indexOf("\x1b[201~");
|
|
60
|
+
if (end !== -1) {
|
|
61
|
+
this.handlePaste(this.pasteBuffer.slice(0, end));
|
|
62
|
+
this.isInPaste = false;
|
|
63
|
+
const rem = this.pasteBuffer.slice(end + 6);
|
|
64
|
+
this.pasteBuffer = "";
|
|
65
|
+
if (rem) this.handleInput(rem);
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const kb = getKeybindings();
|
|
71
|
+
|
|
72
|
+
// Escape
|
|
73
|
+
if (kb.matches(data, "tui.select.cancel")) {
|
|
74
|
+
this.onEscape?.();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Undo
|
|
79
|
+
if (kb.matches(data, "tui.editor.undo")) {
|
|
80
|
+
this.undo();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Ctrl+Enter / Ctrl+J → insert newline (checked before plain Enter)
|
|
85
|
+
if (kb.matches(data, "ctrl+enter") || kb.matches(data, "ctrl+j") || data === "\x1b[13;5u") {
|
|
86
|
+
this.insertChar("\n");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Submit (Enter). On most terminals Enter sends \r; \n is ambiguous
|
|
91
|
+
// (Ctrl+J) but treat as submit by convention.
|
|
92
|
+
if (kb.matches(data, "tui.input.submit") || data === "\r" || data === "\n") {
|
|
93
|
+
this.onSubmit?.(this.value);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Deletion
|
|
98
|
+
if (kb.matches(data, "tui.editor.deleteCharBackward")) { this.backspace(); return; }
|
|
99
|
+
if (kb.matches(data, "tui.editor.deleteCharForward")) { this.fwdDelete(); return; }
|
|
100
|
+
if (kb.matches(data, "tui.editor.deleteWordBackward")) { this.deleteWordBackward(); return; }
|
|
101
|
+
if (kb.matches(data, "tui.editor.deleteWordForward")) { this.deleteWordForward(); return; }
|
|
102
|
+
if (kb.matches(data, "tui.editor.deleteToLineStart")) { this.deleteToLineStart(); return; }
|
|
103
|
+
if (kb.matches(data, "tui.editor.deleteToLineEnd")) { this.deleteToLineEnd(); return; }
|
|
104
|
+
|
|
105
|
+
// Yank
|
|
106
|
+
if (kb.matches(data, "tui.editor.yank")) { this.yank(); return; }
|
|
107
|
+
if (kb.matches(data, "tui.editor.yankPop")) { this.yankPop(); return; }
|
|
108
|
+
|
|
109
|
+
// Cursor movement
|
|
110
|
+
if (kb.matches(data, "tui.editor.cursorLeft")) { this.cursorLeft(); return; }
|
|
111
|
+
if (kb.matches(data, "tui.editor.cursorRight")) { this.cursorRight(); return; }
|
|
112
|
+
if (kb.matches(data, "tui.editor.cursorLineStart")) { this.cursorLineStart(); return; }
|
|
113
|
+
if (kb.matches(data, "tui.editor.cursorLineEnd")) { this.cursorLineEnd(); return; }
|
|
114
|
+
if (kb.matches(data, "tui.editor.cursorWordLeft")) { this.moveWordBackward(); return; }
|
|
115
|
+
if (kb.matches(data, "tui.editor.cursorWordRight")) { this.moveWordForward(); return; }
|
|
116
|
+
|
|
117
|
+
// Up/Down — move cursor between lines
|
|
118
|
+
if (kb.matches(data, "tui.select.up")) { this.cursorUp(); return; }
|
|
119
|
+
if (kb.matches(data, "tui.select.down")) { this.cursorDown(); return; }
|
|
120
|
+
|
|
121
|
+
// Kitty printable
|
|
122
|
+
const kp = decodeKittyPrintable(data);
|
|
123
|
+
if (kp !== undefined) { this.insertChar(kp); return; }
|
|
124
|
+
|
|
125
|
+
// Regular printable chars
|
|
126
|
+
const hasCtrl = [...data].some(ch => {
|
|
127
|
+
const cc = ch.charCodeAt(0);
|
|
128
|
+
return cc < 32 || cc === 0x7f || (cc >= 0x80 && cc <= 0x9f);
|
|
129
|
+
});
|
|
130
|
+
if (!hasCtrl) this.insertChar(data);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ── Cursor movement ──────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
private cursorLeft(): void {
|
|
136
|
+
this.lastAction = null;
|
|
137
|
+
if (this.cursor <= 0) return;
|
|
138
|
+
const before = this.value.slice(0, this.cursor);
|
|
139
|
+
const graphemes = [...segmenter.segment(before)];
|
|
140
|
+
const last = graphemes[graphemes.length - 1];
|
|
141
|
+
this.cursor -= last ? last.segment.length : 1;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private cursorRight(): void {
|
|
145
|
+
this.lastAction = null;
|
|
146
|
+
if (this.cursor >= this.value.length) return;
|
|
147
|
+
const after = this.value.slice(this.cursor);
|
|
148
|
+
const graphemes = [...segmenter.segment(after)];
|
|
149
|
+
this.cursor += graphemes[0] ? graphemes[0].segment.length : 1;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private cursorLineStart(): void {
|
|
153
|
+
this.lastAction = null;
|
|
154
|
+
const before = this.value.slice(0, this.cursor);
|
|
155
|
+
const nl = before.lastIndexOf("\n");
|
|
156
|
+
this.cursor = nl < 0 ? 0 : nl + 1;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private cursorLineEnd(): void {
|
|
160
|
+
this.lastAction = null;
|
|
161
|
+
const after = this.value.slice(this.cursor);
|
|
162
|
+
const nl = after.indexOf("\n");
|
|
163
|
+
this.cursor = nl < 0 ? this.value.length : this.cursor + nl;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private moveWordBackward(): void {
|
|
167
|
+
this.lastAction = null;
|
|
168
|
+
if (this.cursor <= 0) return;
|
|
169
|
+
this.cursor = findWordBackward(this.value, this.cursor);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private moveWordForward(): void {
|
|
173
|
+
this.lastAction = null;
|
|
174
|
+
if (this.cursor >= this.value.length) return;
|
|
175
|
+
this.cursor = findWordForward(this.value, this.cursor);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private cursorUp(): void {
|
|
179
|
+
this.lastAction = null;
|
|
180
|
+
const before = this.value.slice(0, this.cursor);
|
|
181
|
+
const nl = before.lastIndexOf("\n");
|
|
182
|
+
if (nl < 0) { this.cursor = 0; return; }
|
|
183
|
+
const lineStart = before.lastIndexOf("\n", nl - 1);
|
|
184
|
+
const prevLineStart = lineStart < 0 ? 0 : lineStart + 1;
|
|
185
|
+
const col = this.cursor - nl - 1;
|
|
186
|
+
const prevLine = this.value.slice(prevLineStart, nl);
|
|
187
|
+
const prevLineWidth = visibleWidth(prevLine);
|
|
188
|
+
// Try to stay at same visual column
|
|
189
|
+
let newCol = 0;
|
|
190
|
+
let vis = 0;
|
|
191
|
+
for (const ch of prevLine) {
|
|
192
|
+
const chW = visibleWidth(ch);
|
|
193
|
+
if (vis + chW > col) break;
|
|
194
|
+
vis += chW;
|
|
195
|
+
newCol += ch.length;
|
|
196
|
+
}
|
|
197
|
+
this.cursor = prevLineStart + newCol;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private cursorDown(): void {
|
|
201
|
+
this.lastAction = null;
|
|
202
|
+
const after = this.value.slice(this.cursor);
|
|
203
|
+
const nl = after.indexOf("\n");
|
|
204
|
+
if (nl < 0) { this.cursor = this.value.length; return; }
|
|
205
|
+
const nextLineStart = this.cursor + nl + 1;
|
|
206
|
+
const afterNext = this.value.slice(nextLineStart);
|
|
207
|
+
const nextNl = afterNext.indexOf("\n");
|
|
208
|
+
const nextLineEnd = nextNl < 0 ? this.value.length : nextLineStart + nextNl;
|
|
209
|
+
const currentLine = this.value.slice(this.cursor - this.getLineStartOffset(), this.cursor);
|
|
210
|
+
const col = visibleWidth(currentLine);
|
|
211
|
+
// Try to stay at same column
|
|
212
|
+
const nextLine = this.value.slice(nextLineStart, nextLineEnd);
|
|
213
|
+
let newCol = 0;
|
|
214
|
+
let vis = 0;
|
|
215
|
+
for (const ch of nextLine) {
|
|
216
|
+
const chW = visibleWidth(ch);
|
|
217
|
+
if (vis + chW > col) break;
|
|
218
|
+
vis += chW;
|
|
219
|
+
newCol += ch.length;
|
|
220
|
+
}
|
|
221
|
+
this.cursor = nextLineStart + newCol;
|
|
222
|
+
if (this.cursor > nextLineEnd) this.cursor = nextLineEnd;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private getLineStartOffset(): number {
|
|
226
|
+
const before = this.value.slice(0, this.cursor);
|
|
227
|
+
const nl = before.lastIndexOf("\n");
|
|
228
|
+
return nl < 0 ? 0 : nl + 1;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ── Editing ─────────────────────────────────────────────────────
|
|
232
|
+
|
|
233
|
+
private insertChar(ch: string): void {
|
|
234
|
+
if (isWhitespaceChar(ch) || this.lastAction !== "type-word") this.pushUndo();
|
|
235
|
+
this.lastAction = "type-word";
|
|
236
|
+
this.value = this.value.slice(0, this.cursor) + ch + this.value.slice(this.cursor);
|
|
237
|
+
this.cursor += ch.length;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private backspace(): void {
|
|
241
|
+
this.lastAction = null;
|
|
242
|
+
if (this.cursor <= 0) return;
|
|
243
|
+
this.pushUndo();
|
|
244
|
+
const before = this.value.slice(0, this.cursor);
|
|
245
|
+
const graphemes = [...segmenter.segment(before)];
|
|
246
|
+
const last = graphemes[graphemes.length - 1];
|
|
247
|
+
const glen = last ? last.segment.length : 1;
|
|
248
|
+
this.value = this.value.slice(0, this.cursor - glen) + this.value.slice(this.cursor);
|
|
249
|
+
this.cursor -= glen;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private fwdDelete(): void {
|
|
253
|
+
this.lastAction = null;
|
|
254
|
+
if (this.cursor >= this.value.length) return;
|
|
255
|
+
this.pushUndo();
|
|
256
|
+
const after = this.value.slice(this.cursor);
|
|
257
|
+
const graphemes = [...segmenter.segment(after)];
|
|
258
|
+
const glen = graphemes[0] ? graphemes[0].segment.length : 1;
|
|
259
|
+
this.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + glen);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private deleteToLineStart(): void {
|
|
263
|
+
const start = this.getLineStartOffset();
|
|
264
|
+
if (this.cursor <= start) return;
|
|
265
|
+
this.pushUndo();
|
|
266
|
+
const deleted = this.value.slice(start, this.cursor);
|
|
267
|
+
this.killRing.push(deleted, { prepend: true, accumulate: this.lastAction === "kill" });
|
|
268
|
+
this.lastAction = "kill";
|
|
269
|
+
this.value = this.value.slice(0, start) + this.value.slice(this.cursor);
|
|
270
|
+
this.cursor = start;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private deleteToLineEnd(): void {
|
|
274
|
+
const after = this.value.slice(this.cursor);
|
|
275
|
+
const nl = after.indexOf("\n");
|
|
276
|
+
const end = nl < 0 ? this.value.length : this.cursor + nl;
|
|
277
|
+
if (this.cursor >= end) return;
|
|
278
|
+
this.pushUndo();
|
|
279
|
+
const deleted = this.value.slice(this.cursor, end);
|
|
280
|
+
this.killRing.push(deleted, { prepend: false, accumulate: this.lastAction === "kill" });
|
|
281
|
+
this.lastAction = "kill";
|
|
282
|
+
this.value = this.value.slice(0, this.cursor) + this.value.slice(end);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private deleteWordBackward(): void {
|
|
286
|
+
if (this.cursor <= 0) return;
|
|
287
|
+
const wasKill = this.lastAction === "kill";
|
|
288
|
+
this.pushUndo();
|
|
289
|
+
const old = this.cursor;
|
|
290
|
+
this.moveWordBackward();
|
|
291
|
+
const from = this.cursor;
|
|
292
|
+
this.cursor = old;
|
|
293
|
+
const deleted = this.value.slice(from, this.cursor);
|
|
294
|
+
this.killRing.push(deleted, { prepend: true, accumulate: wasKill });
|
|
295
|
+
this.lastAction = "kill";
|
|
296
|
+
this.value = this.value.slice(0, from) + this.value.slice(this.cursor);
|
|
297
|
+
this.cursor = from;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private deleteWordForward(): void {
|
|
301
|
+
if (this.cursor >= this.value.length) return;
|
|
302
|
+
const wasKill = this.lastAction === "kill";
|
|
303
|
+
this.pushUndo();
|
|
304
|
+
const old = this.cursor;
|
|
305
|
+
this.moveWordForward();
|
|
306
|
+
const to = this.cursor;
|
|
307
|
+
this.cursor = old;
|
|
308
|
+
const deleted = this.value.slice(this.cursor, to);
|
|
309
|
+
this.killRing.push(deleted, { prepend: false, accumulate: wasKill });
|
|
310
|
+
this.lastAction = "kill";
|
|
311
|
+
this.value = this.value.slice(0, this.cursor) + this.value.slice(to);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private yank(): void {
|
|
315
|
+
const text = this.killRing.peek();
|
|
316
|
+
if (!text) return;
|
|
317
|
+
this.pushUndo();
|
|
318
|
+
this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
|
|
319
|
+
this.cursor += text.length;
|
|
320
|
+
this.lastAction = "yank";
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private yankPop(): void {
|
|
324
|
+
if (this.lastAction !== "yank" || this.killRing.length <= 1) return;
|
|
325
|
+
this.pushUndo();
|
|
326
|
+
const prev = this.killRing.peek() || "";
|
|
327
|
+
this.value = this.value.slice(0, this.cursor - prev.length) + this.value.slice(this.cursor);
|
|
328
|
+
this.cursor -= prev.length;
|
|
329
|
+
this.killRing.rotate();
|
|
330
|
+
const text = this.killRing.peek() || "";
|
|
331
|
+
this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
|
|
332
|
+
this.cursor += text.length;
|
|
333
|
+
this.lastAction = "yank";
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private pushUndo(): void {
|
|
337
|
+
this.undoStack.push({ value: this.value, cursor: this.cursor });
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private undo(): void {
|
|
341
|
+
const snap = this.undoStack.pop();
|
|
342
|
+
if (!snap) return;
|
|
343
|
+
this.value = snap.value;
|
|
344
|
+
this.cursor = snap.cursor;
|
|
345
|
+
this.lastAction = null;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
private handlePaste(text: string): void {
|
|
349
|
+
this.lastAction = null;
|
|
350
|
+
this.pushUndo();
|
|
351
|
+
const cleaned = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\t/g, " ");
|
|
352
|
+
this.value = this.value.slice(0, this.cursor) + cleaned + this.value.slice(this.cursor);
|
|
353
|
+
this.cursor += cleaned.length;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ── Render ─────────────────────────────────────────────────────
|
|
357
|
+
|
|
358
|
+
invalidate(): void {}
|
|
359
|
+
|
|
360
|
+
render(width: number): string[] {
|
|
361
|
+
const prompt = "> ";
|
|
362
|
+
const avail = Math.max(1, width - prompt.length);
|
|
363
|
+
|
|
364
|
+
// Split into logic lines
|
|
365
|
+
const logicLines = this.value.split("\n");
|
|
366
|
+
// Compute wrap for each logic line
|
|
367
|
+
const wrapLines: { text: string; lineIdx: number; isContinuation: boolean }[] = [];
|
|
368
|
+
for (let li = 0; li < logicLines.length; li++) {
|
|
369
|
+
const line = logicLines[li];
|
|
370
|
+
if (!line) {
|
|
371
|
+
wrapLines.push({ text: "", lineIdx: li, isContinuation: false });
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
const w = visibleWidth(line);
|
|
375
|
+
if (w <= avail) {
|
|
376
|
+
wrapLines.push({ text: line, lineIdx: li, isContinuation: false });
|
|
377
|
+
} else {
|
|
378
|
+
// Word-wrap
|
|
379
|
+
const words = line.split(/(\s+)/);
|
|
380
|
+
let current = "";
|
|
381
|
+
let curW = 0;
|
|
382
|
+
for (const word of words) {
|
|
383
|
+
const wW = visibleWidth(word);
|
|
384
|
+
if (curW + wW > avail && current) {
|
|
385
|
+
wrapLines.push({ text: current, lineIdx: li, isContinuation: wrapLines.length > 0 && wrapLines[wrapLines.length-1].lineIdx === li });
|
|
386
|
+
current = "";
|
|
387
|
+
curW = 0;
|
|
388
|
+
}
|
|
389
|
+
current += word;
|
|
390
|
+
curW += wW;
|
|
391
|
+
}
|
|
392
|
+
if (current) {
|
|
393
|
+
wrapLines.push({ text: current, lineIdx: li, isContinuation: wrapLines.length > 0 && wrapLines[wrapLines.length-1].lineIdx === li });
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Find cursor position: map cursor to (wrapLineIdx, col)
|
|
399
|
+
let cursorLineIdx = 0;
|
|
400
|
+
let cursorCol = 0;
|
|
401
|
+
let pos = 0;
|
|
402
|
+
for (let wi = 0; wi < wrapLines.length; wi++) {
|
|
403
|
+
const wl = wrapLines[wi];
|
|
404
|
+
// Count chars in this wrap line's text (not continuing from wrap)
|
|
405
|
+
// Actually, the whole text is flat in the logic line, so we need to track through logic lines
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Simpler approach: find which render line the cursor is on
|
|
409
|
+
// Track char position through rendered lines
|
|
410
|
+
let remaining = this.cursor;
|
|
411
|
+
let cursorRenderLine = 0;
|
|
412
|
+
let cursorRenderCol = 0;
|
|
413
|
+
for (let wi = 0; wi < wrapLines.length; wi++) {
|
|
414
|
+
const wl = wrapLines[wi];
|
|
415
|
+
const len = wl.text.length;
|
|
416
|
+
// Account for the implicit newline between logic lines (not after the last one)
|
|
417
|
+
const extraNewline = (wl.text === "" || wi < wrapLines.length - 1 && wrapLines[wi+1].lineIdx !== wl.lineIdx) ? 1 : 0;
|
|
418
|
+
if (remaining <= len + extraNewline) {
|
|
419
|
+
cursorRenderLine = wi;
|
|
420
|
+
cursorRenderCol = remaining;
|
|
421
|
+
// Clip to text length for display (cursor at end of line)
|
|
422
|
+
if (cursorRenderCol > len) cursorRenderCol = len;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
remaining -= len + extraNewline;
|
|
426
|
+
cursorRenderLine = wi + 1;
|
|
427
|
+
}
|
|
428
|
+
if (cursorRenderLine >= wrapLines.length) {
|
|
429
|
+
cursorRenderLine = wrapLines.length > 0 ? wrapLines.length - 1 : 0;
|
|
430
|
+
cursorRenderCol = wrapLines[cursorRenderLine]?.text.length ?? 0;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Build output
|
|
434
|
+
const result: string[] = [];
|
|
435
|
+
|
|
436
|
+
if (wrapLines.length === 0) {
|
|
437
|
+
const marker = this.focused ? CURSOR_MARKER : "";
|
|
438
|
+
const cursor = `\x1b[7m \x1b[27m`;
|
|
439
|
+
const line = `${prompt}${marker}${cursor}${" ".repeat(Math.max(0, avail - 1))}`;
|
|
440
|
+
result.push(line);
|
|
441
|
+
} else {
|
|
442
|
+
for (let wi = 0; wi < wrapLines.length; wi++) {
|
|
443
|
+
const wl = wrapLines[wi];
|
|
444
|
+
let lineText = wl.text;
|
|
445
|
+
|
|
446
|
+
// If this is the cursor line, insert cursor marker
|
|
447
|
+
if (wi === cursorRenderLine && this.focused) {
|
|
448
|
+
const before = wl.text.slice(0, cursorRenderCol);
|
|
449
|
+
const at = wl.text.slice(cursorRenderCol, cursorRenderCol + 1) || " ";
|
|
450
|
+
const after = wl.text.slice(cursorRenderCol + 1);
|
|
451
|
+
const cursorChar = `\x1b[7m${at}\x1b[27m`;
|
|
452
|
+
lineText = `${before}${CURSOR_MARKER}${cursorChar}${after}`;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const visual = visibleWidth(lineText);
|
|
456
|
+
const pad = Math.max(0, avail - visual);
|
|
457
|
+
result.push(`${prompt}${lineText}${" ".repeat(pad)}`);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return result;
|
|
462
|
+
}
|
|
463
|
+
}
|