@yanzc/claude-pro 0.7.8 → 0.9.0

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.
@@ -7,6 +7,7 @@ import path from 'node:path';
7
7
  import { spawn } from 'node:child_process';
8
8
  import { resolvePath, displayPath } from '../utils/paths.js';
9
9
  import { ripgrepPath } from '../utils/shell.js';
10
+ import { decodeText as decodeFile } from '../utils/encoding.js';
10
11
 
11
12
  const IGNORED_DIRS = new Set(['node_modules', '.git', '.svn', '.hg', '__pycache__', '.venv', 'dist-newstyle']);
12
13
 
@@ -240,14 +241,9 @@ async function collectFiles(base, globs, exts, signal) {
240
241
  return files;
241
242
  }
242
243
 
243
- // Text of a file, or null for binary files. UTF-16 with a BOM is decoded like ripgrep does.
244
- function decodeText(buf) {
245
- if (buf[0] === 0xFF && buf[1] === 0xFE) return buf.toString('utf16le', 2);
246
- if (buf[0] === 0xFE && buf[1] === 0xFF) return new TextDecoder('utf-16be').decode(buf.subarray(2));
247
- if (buf.subarray(0, 8000).includes(0)) return null;
248
- const text = buf.toString('utf8');
249
- return text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text;
250
- }
244
+ // Text of a file, or null for binary files: UTF-16 with a BOM as ripgrep does it, and GBK files as Read shows them
245
+ // (so that a Chinese word is found in them too).
246
+ const decodeText = (buf) => decodeFile(buf, { writable: false })?.text ?? null;
251
247
 
252
248
  const clip = (line) => (line.length > MAX_COLUMNS ? `${line.slice(0, MAX_COLUMNS)} [... omitted end of long line]` : line);
253
249
 
@@ -255,8 +251,16 @@ function runRg(bin, args, cwd, signal) {
255
251
  return new Promise((resolve) => {
256
252
  const child = spawn(bin, args, { cwd, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
257
253
  let out = '', err = '';
258
- child.stdout.on('data', (d) => { if (out.length < 20_000_000) out += d; });
259
- child.stderr.on('data', (d) => { err += d; });
254
+ // Decoded as a stream (a Chinese path or line can be split between two chunks), and rg is stopped once
255
+ // there is more than will ever be shown instead of scanning on.
256
+ child.stdout.setEncoding('utf8');
257
+ child.stderr.setEncoding('utf8');
258
+ child.stdout.on('data', (d) => {
259
+ if (out.length >= 20_000_000) return;
260
+ out += d;
261
+ if (out.length >= 20_000_000) { out = out.slice(0, out.lastIndexOf('\n') + 1); child.kill(); }
262
+ });
263
+ child.stderr.on('data', (d) => { if (err.length < 100_000) err += d; });
260
264
  const onAbort = () => child.kill();
261
265
  signal?.addEventListener('abort', onAbort, { once: true });
262
266
  child.on('close', (code) => { signal?.removeEventListener('abort', onAbort); resolve({ code, out, err }); });
package/src/tools/web.js CHANGED
@@ -210,17 +210,25 @@ async function searchDuckDuckGo(query, signal) {
210
210
  body: new URLSearchParams({ q: query }).toString(),
211
211
  signal: withTimeout(signal),
212
212
  });
213
- const html = await res.text();
213
+ return parseDuckDuckGo(await res.text());
214
+ }
215
+
216
+ // The results of DuckDuckGo's HTML page, one block per result link. (One regex with an optional snippet group after
217
+ // a lazy gap never took the snippet: the gap matched nothing, and every snippet came back empty.)
218
+ export function parseDuckDuckGo(html) {
214
219
  const results = [];
215
- const re = /<a[^>]+class="result__a"[^>]+href="([^"]+)"[^>]*>([\s\S]*?)<\/a>[\s\S]*?(?:<a[^>]+class="result__snippet"[^>]*>([\s\S]*?)<\/a>)?/g;
216
- let m;
217
- while ((m = re.exec(html)) && results.length < 10) {
218
- let href = decodeEntities(m[1]);
220
+ const starts = [...html.matchAll(/<a[^>]+class="result__a"/g)].map((m) => m.index);
221
+ for (let i = 0; i < starts.length && results.length < 10; i++) {
222
+ const block = html.slice(starts[i], starts[i + 1] ?? html.length);
223
+ const a = /^<a[^>]+href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/.exec(block);
224
+ if (!a) continue;
225
+ let href = decodeEntities(a[1]);
219
226
  const uddg = /[?&]uddg=([^&]+)/.exec(href);
220
227
  if (uddg) href = decodeURIComponent(uddg[1]);
221
228
  if (href.startsWith('//')) href = 'https:' + href;
222
229
  if (/duckduckgo\.com\/y\.js/.test(href)) continue; // ads
223
- results.push({ title: decodeEntities(m[2].replace(/<[^>]+>/g, '')).trim(), url: href, snippet: decodeEntities((m[3] || '').replace(/<[^>]+>/g, '')).trim() });
230
+ const s = /class="result__snippet"[^>]*>([\s\S]*?)<\/(?:a|div|td)>/.exec(block);
231
+ results.push({ title: decodeEntities(a[2].replace(/<[^>]+>/g, '')).trim(), url: href, snippet: s ? decodeEntities(s[1].replace(/<[^>]+>/g, '')).trim() : '' });
224
232
  }
225
233
  return results;
226
234
  }
package/src/ui/ansi.js CHANGED
@@ -192,17 +192,32 @@ export function wordWrap(line, width) {
192
192
  return lines;
193
193
  }
194
194
 
195
+ // Cut a (possibly ANSI colored) string to `width` columns, ending in `ellipsis` when it was longer. It reads only as
196
+ // far as the width reaches: the live region calls it every frame on queued messages, tool input and commands of any
197
+ // length, and measuring all of a 200 KB paste first took some 50 ms a frame.
195
198
  export function truncate(s, width, ellipsis = '…') {
196
- if (stringWidth(s) <= width) return s;
197
- let res = '', w = 0;
198
- const ew = stringWidth(ellipsis);
199
- for (const tok of tokenize(s)) {
200
- if (tok.ansi) { res += tok.t; continue; }
201
- if (w + tok.w > width - ew) break;
202
- res += tok.t;
203
- w += tok.w;
199
+ const room = width - stringWidth(ellipsis);
200
+ let out = '', w = 0;
201
+ let cut = null; // what fits before the ellipsis, once the string has gone past `room`
202
+ const take = (text) => { // true: the string is wider than `width`
203
+ for (const { segment: g } of segmenter.segment(text)) {
204
+ const gw = graphemeWidth(g);
205
+ if (cut === null && w + gw > room) cut = out;
206
+ if (w + gw > width) return true;
207
+ out += g;
208
+ w += gw;
209
+ }
210
+ return false;
211
+ };
212
+ const re = new RegExp(ANSI_RE.source, 'g');
213
+ let last = 0, m;
214
+ while ((m = re.exec(s))) {
215
+ if (m.index > last && take(s.slice(last, m.index))) return cut + ellipsis + (colorEnabled ? '\x1b[0m' : '');
216
+ out += m[0];
217
+ last = m.index + m[0].length;
204
218
  }
205
- return res + ellipsis + (colorEnabled ? '\x1b[0m' : '');
219
+ if (last < s.length && take(s.slice(last))) return cut + ellipsis + (colorEnabled ? '\x1b[0m' : '');
220
+ return s;
206
221
  }
207
222
 
208
223
  export function padEnd(s, width) {