pi-render-btw 0.5.1 → 0.6.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.
@@ -0,0 +1,49 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { BtwOverlayComponent } from "./btw.ts";
3
+
4
+ const theme = {
5
+ bg: (_name: string, text: string) => text,
6
+ fg: (_name: string, text: string) => text,
7
+ bold: (text: string) => text,
8
+ italic: (text: string) => text,
9
+ } as any;
10
+
11
+ const keybindings = { matches: () => false } as any;
12
+ const tui = {
13
+ terminal: { write: () => {} },
14
+ requestRender: () => {},
15
+ } as any;
16
+
17
+ function transcriptLines(entries: unknown[]): string[] {
18
+ const component = new BtwOverlayComponent(
19
+ tui,
20
+ theme,
21
+ keybindings,
22
+ () => entries,
23
+ () => null,
24
+ () => "contextual",
25
+ () => {},
26
+ () => {},
27
+ () => {},
28
+ );
29
+ return (component as any).buildTranscriptLines(80) as string[];
30
+ }
31
+
32
+ describe("BTW transcript message layout", () => {
33
+ it("renders a user badge on its own line before the prompt", () => {
34
+ const lines = transcriptLines([
35
+ { id: 1, turnId: 1, type: "user-message", text: "Prompt" },
36
+ ]);
37
+
38
+ expect(lines.slice(0, 2)).toEqual([" You ", " Prompt"]);
39
+ });
40
+
41
+ it("keeps assistant heading and response on separate lines", () => {
42
+ const lines = transcriptLines([
43
+ { id: 1, turnId: 1, type: "assistant-text", text: "Response", streaming: false },
44
+ ]);
45
+
46
+ expect(lines[0]).toBe(" Assistant ");
47
+ expect(lines[1].startsWith(" Response")).toBe(true);
48
+ });
49
+ });
package/extensions/btw.ts CHANGED
@@ -850,7 +850,7 @@ function buildOverlayTranscript(entries: BtwTranscript, theme: ExtensionContext[
850
850
  }
851
851
 
852
852
  if (entry.type === "user-message") {
853
- pushInlineBlock(userBadge, entry.text, { blankBefore: false });
853
+ pushStackedBlock(userBadge, entry.text, { blankBefore: false });
854
854
  continue;
855
855
  }
856
856
 
@@ -1020,7 +1020,7 @@ function buildTranscriptBadge(
1020
1020
  return theme.bg(background, theme.fg(foreground, theme.bold(` ${label} `)));
1021
1021
  }
1022
1022
 
1023
- class BtwOverlayComponent extends Container implements Focusable {
1023
+ export class BtwOverlayComponent extends Container implements Focusable {
1024
1024
  private readonly input: TextArea;
1025
1025
  private readonly transcript: Container;
1026
1026
  private readonly statusText: Text;
@@ -1179,7 +1179,11 @@ class BtwOverlayComponent extends Container implements Focusable {
1179
1179
 
1180
1180
  if (entry.type === "user-message") {
1181
1181
  pushBlank();
1182
- allLines.push(...wrapTextWithAnsi(`${userBadge} ${entry.text}`, innerWidth));
1182
+ allLines.push(userBadge);
1183
+ const styledLines = wrapTextWithAnsi(entry.text, mdContentWidth);
1184
+ for (const line of styledLines) {
1185
+ allLines.push(`${blockIndent}${line}`);
1186
+ }
1183
1187
  continue;
1184
1188
  }
1185
1189
 
@@ -1297,18 +1301,6 @@ class BtwOverlayComponent extends Container implements Focusable {
1297
1301
  return;
1298
1302
  }
1299
1303
 
1300
- if (matchesKey(data, Key.pageUp) || matchesKey(data, Key.up)) {
1301
- const step = matchesKey(data, Key.pageUp) ? Math.max(1, this.transcriptViewportHeight - 1) : 1;
1302
- this.scrollTranscript(-step);
1303
- return;
1304
- }
1305
-
1306
- if (matchesKey(data, Key.pageDown) || matchesKey(data, Key.down)) {
1307
- const step = matchesKey(data, Key.pageDown) ? Math.max(1, this.transcriptViewportHeight - 1) : 1;
1308
- this.scrollTranscript(step);
1309
- return;
1310
- }
1311
-
1312
1304
  this.input.handleInput(data);
1313
1305
  }
1314
1306
 
@@ -1,6 +1,75 @@
1
- import { decodeKittyPrintable, getKeybindings, CURSOR_MARKER, getGraphemeSegmenter, isWhitespaceChar, sliceByColumn, visibleWidth, findWordBackward, findWordForward, KillRing, UndoStack } from "@earendil-works/pi-tui";
1
+ import { getKeybindings, CURSOR_MARKER, visibleWidth, decodeKittyPrintable, matchesKey } from "@earendil-works/pi-tui";
2
2
 
3
- const segmenter = getGraphemeSegmenter();
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 ───────────────────────────────────────────────
4
73
 
5
74
  /**
6
75
  * Multi-line text area with pi-style editing keybindings.
@@ -18,11 +87,8 @@ export class TextArea {
18
87
  onSubmit: ((value: string) => void) | undefined;
19
88
  onEscape: (() => void) | undefined;
20
89
 
21
- // Paste buffering
22
90
  private pasteBuffer = "";
23
91
  private isInPaste = false;
24
-
25
- // Kill ring + undo
26
92
  private killRing = new KillRing();
27
93
  private lastAction: string | null = null;
28
94
  private undoStack = new UndoStack();
@@ -34,21 +100,8 @@ export class TextArea {
34
100
  this.cursor = Math.min(this.cursor, v.length);
35
101
  }
36
102
 
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
103
  handleInput(data: string): void {
51
- // Paste bracketing
104
+ // Bracketed paste
52
105
  if (data.includes("\x1b[200~")) {
53
106
  this.isInPaste = true;
54
107
  this.pasteBuffer = "";
@@ -81,14 +134,13 @@ export class TextArea {
81
134
  return;
82
135
  }
83
136
 
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") {
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") {
86
139
  this.insertChar("\n");
87
140
  return;
88
141
  }
89
142
 
90
- // Submit (Enter). On most terminals Enter sends \r; \n is ambiguous
91
- // (Ctrl+J) but treat as submit by convention.
143
+ // Submit (Enter)
92
144
  if (kb.matches(data, "tui.input.submit") || data === "\r" || data === "\n") {
93
145
  this.onSubmit?.(this.value);
94
146
  return;
@@ -97,24 +149,24 @@ export class TextArea {
97
149
  // Deletion
98
150
  if (kb.matches(data, "tui.editor.deleteCharBackward")) { this.backspace(); return; }
99
151
  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; }
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; }
104
156
 
105
157
  // Yank
106
158
  if (kb.matches(data, "tui.editor.yank")) { this.yank(); return; }
107
159
  if (kb.matches(data, "tui.editor.yankPop")) { this.yankPop(); return; }
108
160
 
109
- // Cursor movement
161
+ // Cursor
110
162
  if (kb.matches(data, "tui.editor.cursorLeft")) { this.cursorLeft(); return; }
111
163
  if (kb.matches(data, "tui.editor.cursorRight")) { this.cursorRight(); return; }
112
164
  if (kb.matches(data, "tui.editor.cursorLineStart")) { this.cursorLineStart(); return; }
113
165
  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; }
166
+ if (kb.matches(data, "tui.editor.cursorWordLeft")) { this.cursorWordLeft(); return; }
167
+ if (kb.matches(data, "tui.editor.cursorWordRight")) { this.cursorWordRight(); return; }
116
168
 
117
- // Up/Down — move cursor between lines
169
+ // Up/Down (between lines)
118
170
  if (kb.matches(data, "tui.select.up")) { this.cursorUp(); return; }
119
171
  if (kb.matches(data, "tui.select.down")) { this.cursorDown(); return; }
120
172
 
@@ -130,29 +182,26 @@ export class TextArea {
130
182
  if (!hasCtrl) this.insertChar(data);
131
183
  }
132
184
 
133
- // ── Cursor movement ──────────────────────────────────────────────
185
+ // ── Cursor ────────────────────────────────────────────────────────
134
186
 
135
187
  private cursorLeft(): void {
136
188
  this.lastAction = null;
137
189
  if (this.cursor <= 0) return;
138
190
  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;
191
+ const gs = [...segmenter.segment(before)];
192
+ this.cursor -= (gs[gs.length - 1]?.segment.length ?? 1);
142
193
  }
143
194
 
144
195
  private cursorRight(): void {
145
196
  this.lastAction = null;
146
197
  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;
198
+ const gs = [...segmenter.segment(this.value.slice(this.cursor))];
199
+ this.cursor += (gs[0]?.segment.length ?? 1);
150
200
  }
151
201
 
152
202
  private cursorLineStart(): void {
153
203
  this.lastAction = null;
154
- const before = this.value.slice(0, this.cursor);
155
- const nl = before.lastIndexOf("\n");
204
+ const nl = this.value.slice(0, this.cursor).lastIndexOf("\n");
156
205
  this.cursor = nl < 0 ? 0 : nl + 1;
157
206
  }
158
207
 
@@ -163,16 +212,16 @@ export class TextArea {
163
212
  this.cursor = nl < 0 ? this.value.length : this.cursor + nl;
164
213
  }
165
214
 
166
- private moveWordBackward(): void {
215
+ private cursorWordLeft(): void {
167
216
  this.lastAction = null;
168
217
  if (this.cursor <= 0) return;
169
- this.cursor = findWordBackward(this.value, this.cursor);
218
+ this.cursor = wordBackward(this.value, this.cursor);
170
219
  }
171
220
 
172
- private moveWordForward(): void {
221
+ private cursorWordRight(): void {
173
222
  this.lastAction = null;
174
223
  if (this.cursor >= this.value.length) return;
175
- this.cursor = findWordForward(this.value, this.cursor);
224
+ this.cursor = wordForward(this.value, this.cursor);
176
225
  }
177
226
 
178
227
  private cursorUp(): void {
@@ -180,20 +229,13 @@ export class TextArea {
180
229
  const before = this.value.slice(0, this.cursor);
181
230
  const nl = before.lastIndexOf("\n");
182
231
  if (nl < 0) { this.cursor = 0; return; }
183
- const lineStart = before.lastIndexOf("\n", nl - 1);
184
- const prevLineStart = lineStart < 0 ? 0 : lineStart + 1;
232
+ const prevNl = before.lastIndexOf("\n", nl - 1);
233
+ const prevLineStart = prevNl < 0 ? 0 : prevNl + 1;
185
234
  const col = this.cursor - nl - 1;
186
235
  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
- }
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; }
197
239
  this.cursor = prevLineStart + newCol;
198
240
  }
199
241
 
@@ -206,32 +248,23 @@ export class TextArea {
206
248
  const afterNext = this.value.slice(nextLineStart);
207
249
  const nextNl = afterNext.indexOf("\n");
208
250
  const nextLineEnd = nextNl < 0 ? this.value.length : nextLineStart + nextNl;
209
- const currentLine = this.value.slice(this.cursor - this.getLineStartOffset(), this.cursor);
251
+ const currentLine = this.value.slice(this.getLineStart(), this.cursor);
210
252
  const col = visibleWidth(currentLine);
211
- // Try to stay at same column
212
253
  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;
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);
223
257
  }
224
258
 
225
- private getLineStartOffset(): number {
226
- const before = this.value.slice(0, this.cursor);
227
- const nl = before.lastIndexOf("\n");
259
+ private getLineStart(): number {
260
+ const nl = this.value.slice(0, this.cursor).lastIndexOf("\n");
228
261
  return nl < 0 ? 0 : nl + 1;
229
262
  }
230
263
 
231
- // ── Editing ─────────────────────────────────────────────────────
264
+ // ── Editing ───────────────────────────────────────────────────────
232
265
 
233
266
  private insertChar(ch: string): void {
234
- if (isWhitespaceChar(ch) || this.lastAction !== "type-word") this.pushUndo();
267
+ if (isWs(ch) || this.lastAction !== "type-word") this.pushUndo();
235
268
  this.lastAction = "type-word";
236
269
  this.value = this.value.slice(0, this.cursor) + ch + this.value.slice(this.cursor);
237
270
  this.cursor += ch.length;
@@ -242,9 +275,8 @@ export class TextArea {
242
275
  if (this.cursor <= 0) return;
243
276
  this.pushUndo();
244
277
  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;
278
+ const gs = [...segmenter.segment(before)];
279
+ const glen = (gs[gs.length - 1]?.segment.length ?? 1);
248
280
  this.value = this.value.slice(0, this.cursor - glen) + this.value.slice(this.cursor);
249
281
  this.cursor -= glen;
250
282
  }
@@ -253,14 +285,13 @@ export class TextArea {
253
285
  this.lastAction = null;
254
286
  if (this.cursor >= this.value.length) return;
255
287
  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;
288
+ const gs = [...segmenter.segment(this.value.slice(this.cursor))];
289
+ const glen = gs[0]?.segment.length ?? 1;
259
290
  this.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + glen);
260
291
  }
261
292
 
262
- private deleteToLineStart(): void {
263
- const start = this.getLineStartOffset();
293
+ private delToLineStart(): void {
294
+ const start = this.getLineStart();
264
295
  if (this.cursor <= start) return;
265
296
  this.pushUndo();
266
297
  const deleted = this.value.slice(start, this.cursor);
@@ -270,7 +301,7 @@ export class TextArea {
270
301
  this.cursor = start;
271
302
  }
272
303
 
273
- private deleteToLineEnd(): void {
304
+ private delToLineEnd(): void {
274
305
  const after = this.value.slice(this.cursor);
275
306
  const nl = after.indexOf("\n");
276
307
  const end = nl < 0 ? this.value.length : this.cursor + nl;
@@ -282,12 +313,12 @@ export class TextArea {
282
313
  this.value = this.value.slice(0, this.cursor) + this.value.slice(end);
283
314
  }
284
315
 
285
- private deleteWordBackward(): void {
316
+ private delWordBwd(): void {
286
317
  if (this.cursor <= 0) return;
287
318
  const wasKill = this.lastAction === "kill";
288
319
  this.pushUndo();
289
320
  const old = this.cursor;
290
- this.moveWordBackward();
321
+ this.cursor = wordBackward(this.value, this.cursor);
291
322
  const from = this.cursor;
292
323
  this.cursor = old;
293
324
  const deleted = this.value.slice(from, this.cursor);
@@ -297,12 +328,12 @@ export class TextArea {
297
328
  this.cursor = from;
298
329
  }
299
330
 
300
- private deleteWordForward(): void {
331
+ private delWordFwd(): void {
301
332
  if (this.cursor >= this.value.length) return;
302
333
  const wasKill = this.lastAction === "kill";
303
334
  this.pushUndo();
304
335
  const old = this.cursor;
305
- this.moveWordForward();
336
+ this.cursor = wordForward(this.value, this.cursor);
306
337
  const to = this.cursor;
307
338
  this.cursor = old;
308
339
  const deleted = this.value.slice(this.cursor, to);
@@ -333,9 +364,7 @@ export class TextArea {
333
364
  this.lastAction = "yank";
334
365
  }
335
366
 
336
- private pushUndo(): void {
337
- this.undoStack.push({ value: this.value, cursor: this.cursor });
338
- }
367
+ private pushUndo(): void { this.undoStack.push({ value: this.value, cursor: this.cursor }); }
339
368
 
340
369
  private undo(): void {
341
370
  const snap = this.undoStack.pop();
@@ -353,111 +382,79 @@ export class TextArea {
353
382
  this.cursor += cleaned.length;
354
383
  }
355
384
 
356
- // ── Render ─────────────────────────────────────────────────────
385
+ // ── Render ───────────────────────────────────────────────────────
357
386
 
358
387
  invalidate(): void {}
359
388
 
360
389
  render(width: number): string[] {
361
- const prompt = "> ";
362
- const avail = Math.max(1, width - prompt.length);
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
+ }
363
398
 
364
- // Split into logic lines
365
399
  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
- }
400
+ const wrapLines: string[] = [];
401
+
402
+ for (const line of logicLines) {
403
+ if (!line) { wrapLines.push(""); continue; }
374
404
  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;
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;
390
417
  curW += wW;
391
418
  }
392
- if (current) {
393
- wrapLines.push({ text: current, lineIdx: li, isContinuation: wrapLines.length > 0 && wrapLines[wrapLines.length-1].lineIdx === li });
394
- }
395
419
  }
420
+ if (cur) wrapLines.push(cur);
396
421
  }
397
422
 
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
- }
423
+ if (wrapLines.length === 0) wrapLines.push("");
407
424
 
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;
425
+ // Map cursor to render line
426
+ let cursorLine = 0, cursorCol = 0, remaining = this.cursor;
413
427
  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;
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);
423
433
  break;
424
434
  }
425
- remaining -= len + extraNewline;
426
- cursorRenderLine = wi + 1;
435
+ remaining -= len + extra;
427
436
  }
428
- if (cursorRenderLine >= wrapLines.length) {
429
- cursorRenderLine = wrapLines.length > 0 ? wrapLines.length - 1 : 0;
430
- cursorRenderCol = wrapLines[cursorRenderLine]?.text.length ?? 0;
437
+ if (cursorLine >= wrapLines.length) {
438
+ cursorLine = Math.max(0, wrapLines.length - 1);
439
+ cursorCol = wrapLines[cursorLine].length;
431
440
  }
432
441
 
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)}`);
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)}`);
458
455
  }
459
456
  }
460
457
 
461
- return result;
458
+ return lines;
462
459
  }
463
460
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-render-btw",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "description": "A pi extension for parallel side conversations with /btw — with markdown rendering in the floating window",
5
5
  "type": "module",
6
6
  "license": "MIT",