@xynogen/pix-display 0.1.11 → 0.1.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-display",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
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",
@@ -9,10 +9,11 @@ const OPENING_FENCE_RE = /^```([^`]*)$/;
9
9
  const CLOSING_FENCE_RE = /^```\s*$/;
10
10
  const DEFAULT_LABEL = "code";
11
11
  const ANSI_RE = /\x1b\[[0-?]*[ -/]*[@-~]/g;
12
+ const BACKGROUND_ANSI_RE = /\x1b\[(?:4[0-9]|48)(?:;[^m]*)?m/g;
12
13
  const OSC_RE = /\x1b\][^\x07]*(?:\x07|\x1b\\)/g;
13
14
  const PATCHED = Symbol.for("@xynogen/pix-display:code-block-renderer");
14
15
 
15
- type CodeFrameTheme = Pick<Theme, "bg" | "bold" | "fg" | "getBgAnsi">;
16
+ type CodeFrameTheme = Pick<Theme, "bold" | "fg">;
16
17
  type RenderMode = "tui" | "rpc" | "json" | "print";
17
18
 
18
19
  type PatchablePrototype = {
@@ -52,6 +53,7 @@ function stripLayoutWhitespace(line: string, count: number): string {
52
53
  let remaining = count;
53
54
  return line
54
55
  .replace(OSC_RE, "")
56
+ .replace(BACKGROUND_ANSI_RE, "")
55
57
  .replace(/^(?:\x1b\[[0-?]*[ -/]*[@-~]| )+/, (prefix) =>
56
58
  prefix.replace(/ /g, (space) => {
57
59
  if (remaining <= 0) return space;
@@ -62,18 +64,6 @@ function stripLayoutWhitespace(line: string, count: number): string {
62
64
  .replace(/ +$/, "");
63
65
  }
64
66
 
65
- function paintFrame(theme: CodeFrameTheme, text: string): string {
66
- try {
67
- const bg = theme.getBgAnsi("toolSuccessBg");
68
- const preserved = text.replace(/\x1b\[([0-9;]*)m/g, (sequence, params: string) =>
69
- params === "0" || params.split(";").includes("49") ? `${sequence}${bg}` : sequence,
70
- );
71
- return `${bg}${preserved}\x1b[49m`;
72
- } catch {
73
- return theme.bg("toolSuccessBg", text);
74
- }
75
- }
76
-
77
67
  function fenceLabel(line: string): string | undefined {
78
68
  const match = plainText(line).trim().match(OPENING_FENCE_RE);
79
69
  if (!match) return undefined;
@@ -86,26 +76,18 @@ function topRule(width: number, language: string, theme: CodeFrameTheme): string
86
76
  const displayLanguage = truncateToWidth(language, available, "…");
87
77
  const label = theme.bold(theme.fg("accent", ` ${displayLanguage} `));
88
78
  const ruleWidth = Math.max(0, width - visibleWidth(label) - 2);
89
- return paintFrame(
90
- theme,
91
- `${theme.fg("borderMuted", "──")}${label}${theme.fg("borderMuted", "─".repeat(ruleWidth))}`,
92
- );
79
+ return `${theme.fg("borderMuted", "──")}${label}${theme.fg("borderMuted", "─".repeat(ruleWidth))}`;
93
80
  }
94
81
 
95
82
  function bottomRule(width: number, theme: CodeFrameTheme): string {
96
- return paintFrame(theme, theme.fg("borderMuted", "─".repeat(width)));
83
+ return theme.fg("borderMuted", "─".repeat(width));
97
84
  }
98
85
 
99
- function bodyLine(
100
- line: string,
101
- width: number,
102
- layoutIndent: number,
103
- theme: CodeFrameTheme,
104
- ): string {
86
+ function bodyLine(line: string, width: number, layoutIndent: number): string {
105
87
  const content = truncateToWidth(stripLayoutWhitespace(line, layoutIndent), width, "…");
106
88
  // Keep every code row free of frame glyphs and layout padding. Selection must
107
89
  // copy only source text, including meaningful relative indentation in Python.
108
- return paintFrame(theme, content);
90
+ return content;
109
91
  }
110
92
 
111
93
  /**
@@ -138,7 +120,7 @@ export function renderCodeFences(lines: string[], width: number, theme: CodeFram
138
120
  framed.push(`${oscSequences(out[start] ?? "")}${topRule(frameWidth, language, theme)}`);
139
121
  for (let index = start + 1; index < end; index++) {
140
122
  framed.push(
141
- `${oscSequences(out[index] ?? "")}${bodyLine(out[index] ?? "", width, bodyIndent, theme)}`,
123
+ `${oscSequences(out[index] ?? "")}${bodyLine(out[index] ?? "", width, bodyIndent)}`,
142
124
  );
143
125
  }
144
126
  framed.push(`${oscSequences(out[end] ?? "")}${bottomRule(frameWidth, theme)}`);
@@ -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) => {