iubcli 4.2.15 → 4.2.17

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/dist/index.js CHANGED
@@ -3,7 +3,6 @@
3
3
  // src/index.ts
4
4
  import fs8 from "fs";
5
5
  import path8 from "path";
6
- import readline from "readline";
7
6
  import { execSync as execSync3 } from "child_process";
8
7
  import { Command } from "commander";
9
8
  import chalk9 from "chalk";
@@ -838,24 +837,37 @@ async function editFile(filePath, target, replacement, signal) {
838
837
  return `Error: "${filePath}" is a directory, not a file.`;
839
838
  }
840
839
  const content = fs2.readFileSync(targetFile, "utf8");
841
- const usesCRLF = content.includes("\r\n");
842
840
  let effectiveTarget = target;
843
841
  let effectiveReplacement = replacement;
844
- let workingContent = content;
845
- let normalizedMode = false;
846
- if (!content.includes(target)) {
842
+ let searchContent = content;
843
+ let matchStart = -1;
844
+ let matchEnd = -1;
845
+ if (content.includes(target)) {
846
+ matchStart = content.indexOf(target);
847
+ matchEnd = matchStart + target.length;
848
+ } else {
847
849
  const contentLF = content.replace(/\r\n/g, "\n");
848
850
  const targetLF = target.replace(/\r\n/g, "\n");
849
- if (contentLF.includes(targetLF)) {
850
- normalizedMode = true;
851
- workingContent = contentLF;
852
- effectiveTarget = targetLF;
853
- effectiveReplacement = replacement.replace(/\r\n/g, "\n");
854
- } else {
851
+ if (!contentLF.includes(targetLF)) {
855
852
  return `Error: Target string not found in "${filePath}". Please use read_file first to verify the exact lines and whitespace before calling edit_file.`;
856
853
  }
854
+ searchContent = contentLF;
855
+ effectiveTarget = targetLF;
856
+ const indexMap = [];
857
+ for (let i = 0; i < content.length; i++) {
858
+ if (content[i] === "\r" && content[i + 1] === "\n") {
859
+ indexMap.push(i);
860
+ i++;
861
+ } else {
862
+ indexMap.push(i);
863
+ }
864
+ }
865
+ indexMap.push(content.length);
866
+ const normalizedMatch = contentLF.indexOf(targetLF);
867
+ matchStart = indexMap[normalizedMatch];
868
+ matchEnd = indexMap[normalizedMatch + targetLF.length];
857
869
  }
858
- const occurrences = workingContent.split(effectiveTarget).length - 1;
870
+ const occurrences = searchContent.split(effectiveTarget).length - 1;
859
871
  if (occurrences > 1) {
860
872
  return `Warning: Target string matches ${occurrences} times in "${filePath}". Please include more surrounding context lines in 'target' so it matches uniquely.`;
861
873
  }
@@ -871,14 +883,15 @@ With: ${effectiveReplacement.split("\n").length} line(s) (${effectiveReplacement
871
883
  console.log(chalk2.cyan("Auto-approve enabled for this session."));
872
884
  }
873
885
  }
874
- if (!normalizedMode && usesCRLF && effectiveReplacement.includes("\n")) {
875
- effectiveReplacement = effectiveReplacement.replace(/\r?\n/g, "\r\n");
876
- }
877
- const idx = workingContent.indexOf(effectiveTarget);
878
- let updatedContent = workingContent.slice(0, idx) + effectiveReplacement + workingContent.slice(idx + effectiveTarget.length);
879
- if (normalizedMode && usesCRLF) {
880
- updatedContent = updatedContent.replace(/\n/g, "\r\n");
886
+ const crlfCount = (content.match(/\r\n/g) || []).length;
887
+ const loneLfCount = (content.match(/\n/g) || []).length - crlfCount;
888
+ const fileEol = crlfCount > loneLfCount ? "\r\n" : "\n";
889
+ const matchedRegion = content.slice(matchStart, matchEnd);
890
+ const regionEol = matchedRegion.includes("\r\n") ? "\r\n" : matchedRegion.includes("\n") ? "\n" : fileEol;
891
+ if (effectiveReplacement.includes("\n")) {
892
+ effectiveReplacement = effectiveReplacement.replace(/\r\n|\n/g, regionEol);
881
893
  }
894
+ const updatedContent = content.slice(0, matchStart) + effectiveReplacement + content.slice(matchEnd);
882
895
  sessionFileHistory.push({
883
896
  filePath: path2.relative(rootDir, targetFile).replace(/\\/g, "/"),
884
897
  isNew: false,
@@ -2300,22 +2313,41 @@ function stripTerminalControlNoise(raw) {
2300
2313
  }
2301
2314
  function isIncompleteEscapePrefix(str) {
2302
2315
  if (str === "\x1B" || str === "\x1BO") return true;
2303
- return /^\x1b\[[0-9;?:]*$/.test(str);
2316
+ return /^\x1b\[[0-9;?:<>=]*$/.test(str);
2317
+ }
2318
+ function enterRawStdin() {
2319
+ const wasRaw = Boolean(process.stdin.isRaw);
2320
+ try {
2321
+ if (process.stdin.isTTY) {
2322
+ if (wasRaw) {
2323
+ process.stdin.setRawMode(false);
2324
+ }
2325
+ process.stdin.setRawMode(true);
2326
+ }
2327
+ } catch {
2328
+ }
2329
+ try {
2330
+ process.stdin.ref?.();
2331
+ process.stdin.resume();
2332
+ } catch {
2333
+ }
2334
+ return wasRaw;
2304
2335
  }
2305
2336
  function parseTerminalEscape(rest) {
2306
- const win32Match = rest.match(/^\x1b\[(\d+);(\d+);(\d+);([01]);(\d+);(\d+)_/);
2337
+ const win32Match = rest.match(/^\x1b\[([0-9;]*)_/);
2307
2338
  if (win32Match) {
2308
2339
  const length = win32Match[0].length;
2309
- const vk = Number(win32Match[1]);
2310
- const uc = Number(win32Match[3]);
2311
- const isKeyDown = win32Match[4] === "1";
2312
- const controlState = Number(win32Match[5]);
2340
+ const parts = win32Match[1].split(";");
2341
+ const vk = Number(parts[0] || 0);
2342
+ const uc = Number(parts[2] || 0);
2343
+ const isKeyDown = parts[3] === "1";
2344
+ const controlState = Number(parts[4] || 0);
2313
2345
  if (!isKeyDown) return { type: "noop", length };
2314
2346
  if (uc === 3 || vk === 67 && (controlState & 12) !== 0) return { type: "ctrl_c", length };
2315
- if (vk === 38) return { type: "up", length };
2316
- if (vk === 40) return { type: "down", length };
2317
- if (vk === 37) return { type: "left", length };
2318
- if (vk === 39) return { type: "right", length };
2347
+ if (vk === 38 || uc === 0 && vk === 104) return { type: "up", length };
2348
+ if (vk === 40 || uc === 0 && vk === 98) return { type: "down", length };
2349
+ if (vk === 37 || uc === 0 && vk === 100) return { type: "left", length };
2350
+ if (vk === 39 || uc === 0 && vk === 102) return { type: "right", length };
2319
2351
  if (vk === 33) return { type: "page_up", length };
2320
2352
  if (vk === 34) return { type: "page_down", length };
2321
2353
  if (vk === 36) return { type: "home", length };
@@ -2331,21 +2363,27 @@ function parseTerminalEscape(rest) {
2331
2363
  if (rest.startsWith("\x1B\r") || rest.startsWith("\x1B\r")) {
2332
2364
  return { type: "newline", length: 2 };
2333
2365
  }
2334
- const kittyEnterMatch = rest.match(/^\x1b\[13(?:;(\d+)(?::\d+)?)?u/);
2366
+ const kittyEnterMatch = rest.match(/^\x1b\[13(?:;(\d+)(?::(\d+))?)?u/);
2335
2367
  if (kittyEnterMatch) {
2368
+ if (kittyEnterMatch[2] === "3") return { type: "noop", length: kittyEnterMatch[0].length };
2336
2369
  const mod = kittyEnterMatch[1] ? Number(kittyEnterMatch[1]) : 1;
2337
2370
  return { type: mod > 1 ? "newline" : "enter", length: kittyEnterMatch[0].length };
2338
2371
  }
2339
2372
  const kittyEscMatch = rest.match(/^\x1b\[27(?:;[0-9:]+)?u/);
2340
2373
  if (kittyEscMatch) {
2374
+ if (/:3(?:$|;)/.test(kittyEscMatch[0])) return { type: "noop", length: kittyEscMatch[0].length };
2341
2375
  return { type: "escape", length: kittyEscMatch[0].length };
2342
2376
  }
2343
- const kittyTabMatch = rest.match(/^\x1b\[9(?:;1(?::\d+)?)?u/);
2377
+ const kittyTabMatch = rest.match(/^\x1b\[9(?:;1(?::(\d+))?)?u/);
2344
2378
  if (kittyTabMatch) {
2379
+ if (kittyTabMatch[1] === "3") return { type: "noop", length: kittyTabMatch[0].length };
2345
2380
  return { type: "tab", length: kittyTabMatch[0].length };
2346
2381
  }
2347
- const csiNavMatch = rest.match(/^\x1b\[[0-9;:]*([ABCDHF])/);
2382
+ const csiNavMatch = rest.match(/^\x1b\[([0-9;:]*)([ABCDHF])/);
2348
2383
  if (csiNavMatch) {
2384
+ if (/:3(?:$|;)/.test(csiNavMatch[1])) {
2385
+ return { type: "noop", length: csiNavMatch[0].length };
2386
+ }
2349
2387
  const dirMap = {
2350
2388
  A: "up",
2351
2389
  B: "down",
@@ -2354,7 +2392,7 @@ function parseTerminalEscape(rest) {
2354
2392
  H: "home",
2355
2393
  F: "end"
2356
2394
  };
2357
- return { type: dirMap[csiNavMatch[1]], length: csiNavMatch[0].length };
2395
+ return { type: dirMap[csiNavMatch[2]], length: csiNavMatch[0].length };
2358
2396
  }
2359
2397
  const rxvtNavMatch = rest.match(/^\x1b\[([abcd])/);
2360
2398
  if (rxvtNavMatch) {
@@ -2366,10 +2404,13 @@ function parseTerminalEscape(rest) {
2366
2404
  };
2367
2405
  return { type: dirMap[rxvtNavMatch[1]], length: 3 };
2368
2406
  }
2369
- const tildeMatch = rest.match(/^\x1b\[(\d+)(?:;[0-9:]+)?~/);
2407
+ const tildeMatch = rest.match(/^\x1b\[(\d+)(?:;([0-9:]+))?~/);
2370
2408
  if (tildeMatch) {
2371
2409
  const code = tildeMatch[1];
2372
2410
  const length = tildeMatch[0].length;
2411
+ if (tildeMatch[2] && /:3(?:$|;)/.test(tildeMatch[2])) {
2412
+ return { type: "noop", length };
2413
+ }
2373
2414
  if (code === "5") return { type: "page_up", length };
2374
2415
  if (code === "6") return { type: "page_down", length };
2375
2416
  if (code === "1" || code === "7") return { type: "home", length };
@@ -2398,7 +2439,7 @@ function parseTerminalEscape(rest) {
2398
2439
  };
2399
2440
  return { type: ss3Map[ch] || "noop", length: 3 };
2400
2441
  }
2401
- const genericCsi = rest.match(/^\x1b\[[0-9;?:]*[a-zA-Z~_]/);
2442
+ const genericCsi = rest.match(/^\x1b\[[0-9;?:<>=]*[a-zA-Z~_]/);
2402
2443
  if (genericCsi) {
2403
2444
  return { type: "noop", length: genericCsi[0].length };
2404
2445
  }
@@ -2450,12 +2491,7 @@ async function interactiveSelect(config) {
2450
2491
  _interactiveSelectActive = true;
2451
2492
  let cursor = config.options.findIndex((o) => o.value === config.initialValue);
2452
2493
  if (cursor === -1) cursor = 0;
2453
- const wasRaw = process.stdin.isRaw;
2454
- try {
2455
- process.stdin.setRawMode(true);
2456
- } catch {
2457
- }
2458
- process.stdin.resume();
2494
+ const wasRaw = enterRawStdin();
2459
2495
  process.stdout.write("\x1B[?25l");
2460
2496
  let termRows = process.stdout.rows || 24;
2461
2497
  let termCols = process.stdout.columns || 80;
@@ -2697,7 +2733,7 @@ async function interactiveSelect(config) {
2697
2733
  }
2698
2734
  };
2699
2735
  process.stdout.on("resize", onResize);
2700
- process.stdin.on("data", onData);
2736
+ process.stdin.prependListener("data", onData);
2701
2737
  draw();
2702
2738
  });
2703
2739
  }
@@ -2719,10 +2755,8 @@ async function interactiveInput(config) {
2719
2755
  let inBracketedPaste = false;
2720
2756
  let pasteBuf = "";
2721
2757
  let lastLines = 0;
2722
- const wasRaw = process.stdin.isRaw;
2758
+ const wasRaw = enterRawStdin();
2723
2759
  try {
2724
- process.stdin.setRawMode(true);
2725
- process.stdin.resume();
2726
2760
  process.stdout.write("\x1B[?2004h");
2727
2761
  } catch {
2728
2762
  }
@@ -2932,7 +2966,7 @@ async function interactiveInput(config) {
2932
2966
  lastLines = initLines.length;
2933
2967
  const initCol = Math.min(termCols - 2, 5 + (value.length === 0 ? 0 : cursorPos));
2934
2968
  process.stdout.write(`\x1B[${lastLines - 1}A\x1B[${initCol}G\x1B[?25h`);
2935
- process.stdin.on("data", onData);
2969
+ process.stdin.prependListener("data", onData);
2936
2970
  });
2937
2971
  }
2938
2972
  function updateDockedModel(provider, model, mode) {
@@ -3035,12 +3069,7 @@ async function readInlinePrompt(options) {
3035
3069
  process.stdout.write("\x1B[?2004h");
3036
3070
  } catch {
3037
3071
  }
3038
- const wasRaw = process.stdin.isRaw;
3039
- try {
3040
- process.stdin.setRawMode(true);
3041
- process.stdin.resume();
3042
- } catch {
3043
- }
3072
+ const wasRaw = enterRawStdin();
3044
3073
  let bufferLines = [""];
3045
3074
  let cursorLine = 0;
3046
3075
  let cursorCol = 0;
@@ -3482,7 +3511,7 @@ async function readInlinePrompt(options) {
3482
3511
  idx = printableEnd;
3483
3512
  }
3484
3513
  };
3485
- process.stdin.on("data", onData);
3514
+ process.stdin.prependListener("data", onData);
3486
3515
  });
3487
3516
  }
3488
3517
  async function readOpenCodePrompt(options) {
@@ -8514,18 +8543,14 @@ function attachEscapeListener(onEscape) {
8514
8543
  return () => {
8515
8544
  };
8516
8545
  }
8517
- readline.emitKeypressEvents(process.stdin);
8518
- const wasRaw = process.stdin.isRaw;
8519
- try {
8520
- process.stdin.setRawMode(true);
8521
- process.stdin.resume();
8522
- } catch {
8523
- }
8546
+ const wasRaw = enterRawStdin();
8524
8547
  let triggered = false;
8548
+ let escBuffer = "";
8525
8549
  let escTimer = null;
8526
8550
  const triggerEscape = () => {
8527
8551
  if (triggered) return;
8528
8552
  triggered = true;
8553
+ escBuffer = "";
8529
8554
  if (escTimer) {
8530
8555
  clearTimeout(escTimer);
8531
8556
  escTimer = null;
@@ -8533,52 +8558,58 @@ function attachEscapeListener(onEscape) {
8533
8558
  onEscape();
8534
8559
  };
8535
8560
  const onData = (chunk) => {
8536
- if (isInteractivePromptActive()) {
8537
- if (escTimer) {
8538
- clearTimeout(escTimer);
8539
- escTimer = null;
8540
- }
8541
- return;
8561
+ if (escTimer) {
8562
+ clearTimeout(escTimer);
8563
+ escTimer = null;
8542
8564
  }
8543
- const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
8544
- if (buf.length === 1 && buf[0] === 3) {
8545
- process.exit(0);
8565
+ if (isInteractivePromptActive() || wasInteractivePromptRecentlyActive(600)) {
8566
+ escBuffer = "";
8567
+ return;
8546
8568
  }
8547
- if (wasInteractivePromptRecentlyActive(600)) return;
8548
- if (buf.length === 1 && buf[0] === 27) {
8549
- if (escTimer) {
8550
- triggerEscape();
8551
- return;
8569
+ const incoming = typeof chunk === "string" ? chunk : chunk.toString("utf-8");
8570
+ const str = escBuffer + incoming;
8571
+ escBuffer = "";
8572
+ if (!str) return;
8573
+ let idx = 0;
8574
+ while (idx < str.length) {
8575
+ const rest = str.slice(idx);
8576
+ if (rest.startsWith("")) {
8577
+ process.exit(0);
8552
8578
  }
8553
- escTimer = setTimeout(() => {
8554
- escTimer = null;
8555
- if (!isInteractivePromptActive() && !wasInteractivePromptRecentlyActive(600)) {
8579
+ if (rest[0] === "\x1B") {
8580
+ const parsed = parseTerminalEscape(rest);
8581
+ if (parsed === null) {
8582
+ escBuffer = rest;
8583
+ escTimer = setTimeout(() => {
8584
+ escBuffer = "";
8585
+ escTimer = null;
8586
+ if (!isInteractivePromptActive() && !wasInteractivePromptRecentlyActive(600)) {
8587
+ triggerEscape();
8588
+ }
8589
+ }, 60);
8590
+ return;
8591
+ }
8592
+ if (parsed.type === "ctrl_c") {
8593
+ process.exit(0);
8594
+ }
8595
+ if (parsed.type === "escape") {
8556
8596
  triggerEscape();
8597
+ return;
8557
8598
  }
8558
- }, 60);
8559
- } else if (escTimer) {
8560
- clearTimeout(escTimer);
8561
- escTimer = null;
8562
- }
8563
- };
8564
- const keypressHandler = (str, key) => {
8565
- if (key && key.ctrl && key.name === "c") {
8566
- process.exit(0);
8567
- }
8568
- if (isInteractivePromptActive() || wasInteractivePromptRecentlyActive(600)) return;
8569
- if (key && key.name === "escape" || str === "\x1B" || str === "\x1B" || key && key.sequence === "\x1B") {
8570
- triggerEscape();
8599
+ idx += parsed.length;
8600
+ continue;
8601
+ }
8602
+ idx += 1;
8571
8603
  }
8572
8604
  };
8573
8605
  process.stdin.on("data", onData);
8574
- process.stdin.on("keypress", keypressHandler);
8575
8606
  return () => {
8607
+ escBuffer = "";
8576
8608
  if (escTimer) {
8577
8609
  clearTimeout(escTimer);
8578
8610
  escTimer = null;
8579
8611
  }
8580
8612
  process.stdin.removeListener("data", onData);
8581
- process.stdin.removeListener("keypress", keypressHandler);
8582
8613
  try {
8583
8614
  if (!wasRaw && process.stdin.isTTY) {
8584
8615
  process.stdin.setRawMode(false);