@xynogen/pix-display 0.1.11 → 0.1.12

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/paste-chips.ts +37 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-display",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "Pi core extension \u2014 paste chips, thinking blocks, and framed code rendering",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -149,11 +149,20 @@ export function replaceImagePaths(
149
149
  *
150
150
  * Width-preserving is not required — Pi re-wraps each render call.
151
151
  */
152
+ // A `[...]` span that starts with `paste #` and may carry interleaved cursor
153
+ // SGR codes (e.g. `[paste #1 \x1b[7m5\x1b[0m8 chars]`). We strip ANSI from the
154
+ // matched span only, never the whole line, so the editor cursor's own
155
+ // inverse-video block outside any marker survives.
156
+ const MARKER_SPAN_RE =
157
+ /(?:\x1b\[[0-9;]*m)*\[(?:\x1b\[[0-9;]*m)*paste #(?:[^\]]|\x1b\[[0-9;]*m)*\]/g;
158
+
152
159
  export function restyleMarkers(line: string, imageIds: Set<number>): string {
153
- // Strip cursor inversion codes the TUI embeds when the cursor
154
- // intersects a marker — plain MARKER_RE handles the rest.
155
- const clean = line.includes("\x1b") ? line.replace(CURSOR_RE, "") : line;
156
- return clean.replace(MARKER_RE, (_full, idStr, _g2, _g3, linesStr, charsStr) => {
160
+ return line.replace(MARKER_SPAN_RE, (span) => {
161
+ const clean = span.includes("\x1b") ? span.replace(CURSOR_RE, "") : span;
162
+ MARKER_RE.lastIndex = 0;
163
+ const m = MARKER_RE.exec(clean);
164
+ if (!m?.[1]) return span;
165
+ const [, idStr, , , linesStr, charsStr] = m;
157
166
  const id = Number.parseInt(idStr, 10);
158
167
  if (imageIds.has(id)) {
159
168
  return chip(FG_BLUE, icon("paste.image"), "image", `#${id}`);
@@ -191,6 +200,18 @@ function compactNumber(raw: string): string {
191
200
  // per-instance, capturing the base implementation, so large text pastes get the
192
201
  // same trailing-space treatment as image chips.
193
202
  type PasteHandler = { handlePaste(text: string): void };
203
+ type SubmitHandler = { submitValue(): void };
204
+
205
+ /**
206
+ * Drop image classifications when Pi resets its paste IDs after a submit.
207
+ *
208
+ * The base editor clears `pastes` and resets `pasteCounter` to zero, so IDs are
209
+ * reused in the next prompt. Keeping the old IDs would make a later text paste
210
+ * with the same ID render as an image chip.
211
+ */
212
+ export function clearImageIdsAfterSubmit(imageIds: Set<number>): void {
213
+ imageIds.clear();
214
+ }
194
215
 
195
216
  class ChipEditor extends CustomEditor {
196
217
  private readonly imageIds = new Set<number>();
@@ -199,6 +220,7 @@ class ChipEditor extends CustomEditor {
199
220
  super(...args);
200
221
  this.patchHandlePaste();
201
222
  this.patchExpandPasteMarkers();
223
+ this.patchSubmitValue();
202
224
  }
203
225
 
204
226
  override insertTextAtCursor(text: string): void {
@@ -237,6 +259,17 @@ class ChipEditor extends CustomEditor {
237
259
  };
238
260
  }
239
261
 
262
+ private patchSubmitValue(): void {
263
+ const self = this as unknown as SubmitHandler;
264
+ const base = self.submitValue.bind(self);
265
+ self.submitValue = () => {
266
+ // Pi resets pasteCounter to zero in submitValue. Clear the parallel
267
+ // image-ID registry in the same operation before those IDs get reused.
268
+ clearImageIdsAfterSubmit(this.imageIds);
269
+ base();
270
+ };
271
+ }
272
+
240
273
  override render(width: number): string[] {
241
274
  const raw = super.render(width);
242
275
  return raw.map((line) => {