@smoose/pi-beautify 0.1.2 → 0.1.3

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/index.ts +18 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoose/pi-beautify",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Small visual polish extensions for pi coding agent",
5
5
  "type": "module",
6
6
  "files": [
package/src/index.ts CHANGED
@@ -39,8 +39,6 @@ interface EditorInternals {
39
39
  }
40
40
 
41
41
  class ImageTokenController {
42
- private nextId = 1;
43
-
44
42
  constructor(private readonly attachments: Map<string, Attachment>) {}
45
43
 
46
44
  renderChips(lines: string[], theme: Theme, width: number): string[] {
@@ -51,16 +49,18 @@ class ImageTokenController {
51
49
  return rendered.map((line) => truncateToWidth(line, width, ""));
52
50
  }
53
51
 
54
- replaceClipboardPathsInText(text: string): string {
55
- return text.replace(CLIPBOARD_PATH_RE, (path) => this.createImageToken(path));
52
+ replaceClipboardPathsInText(text: string, existingText = ""): string {
53
+ const usedIds = this.collectUsedIds(`${existingText}\n${text}`);
54
+ return text.replace(CLIPBOARD_PATH_RE, (path) => this.createImageToken(path, usedIds));
56
55
  }
57
56
 
58
57
  replaceClipboardPathsInEditor(editor: EditorComponent, tui: TUI): void {
59
58
  const current = editor.getText();
59
+ const usedIds = this.collectUsedIds(current);
60
60
  let changed = false;
61
61
  const next = current.replace(CLIPBOARD_PATH_RE, (path) => {
62
62
  changed = true;
63
- return this.createImageToken(path);
63
+ return this.createImageToken(path, usedIds);
64
64
  });
65
65
  if (!changed) return;
66
66
  editor.setText(next);
@@ -107,8 +107,17 @@ class ImageTokenController {
107
107
  return undefined;
108
108
  }
109
109
 
110
- private createImageToken(path: string): string {
111
- const token = imageChip(this.nextId++);
110
+ private collectUsedIds(text: string): Set<number> {
111
+ const usedIds = new Set<number>();
112
+ for (const match of text.matchAll(TOKEN_RE)) usedIds.add(Number(match[1]));
113
+ return usedIds;
114
+ }
115
+
116
+ private createImageToken(path: string, usedIds: Set<number>): string {
117
+ let id = 1;
118
+ while (usedIds.has(id)) id++;
119
+ usedIds.add(id);
120
+ const token = imageChip(id);
112
121
  this.attachments.set(token, { token, path, mimeType: mimeTypeForPath(path) });
113
122
  return `${token} `;
114
123
  }
@@ -135,7 +144,7 @@ class BeautifyEditor extends CustomEditor {
135
144
  }
136
145
 
137
146
  insertTextAtCursor(text: string): void {
138
- super.insertTextAtCursor(this.imageTokens.replaceClipboardPathsInText(text));
147
+ super.insertTextAtCursor(this.imageTokens.replaceClipboardPathsInText(text, this.getText()));
139
148
  }
140
149
 
141
150
  render(width: number): string[] {
@@ -221,7 +230,7 @@ class BeautifyEditorWrapper implements EditorComponent {
221
230
  }
222
231
 
223
232
  insertTextAtCursor(text: string): void {
224
- const next = this.imageTokens.replaceClipboardPathsInText(text);
233
+ const next = this.imageTokens.replaceClipboardPathsInText(text, this.inner.getText());
225
234
  if (this.inner.insertTextAtCursor) {
226
235
  this.inner.insertTextAtCursor(next);
227
236
  return;