iubcli 4.2.14 → 4.2.16

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";
@@ -194,7 +193,8 @@ function createSpinner(text) {
194
193
  text: chalk.cyan(text),
195
194
  color: "cyan",
196
195
  spinner: "dots",
197
- stream: process.stdout
196
+ stream: process.stdout,
197
+ discardStdin: false
198
198
  });
199
199
  }
200
200
  function renderPriorityTaskBanner(task, model, mode) {
@@ -972,6 +972,9 @@ function parseDirectoryChangeCommand(subCommand, currentCwd) {
972
972
  return null;
973
973
  }
974
974
  async function executeSequentialCommand(command, signal) {
975
+ if (signal?.aborted) {
976
+ return "Action aborted by user (Escape).";
977
+ }
975
978
  if (!safetySession.autoApproveAll) {
976
979
  const summary = `${chalk2.gray("Shell Command:")} ${chalk2.magenta.bold(command)}
977
980
  ${chalk2.gray("Working Directory:")} ${process.cwd()}`;
@@ -2294,10 +2297,151 @@ function truncateWithAnsi(str, maxWidth) {
2294
2297
  function stripTerminalControlNoise(raw) {
2295
2298
  return raw.replace(/\x1b\[[IO]/g, "").replace(/\x1b\[[0-9;]*R/g, "").replace(/\x1b\[\?[0-9;]*c/g, "").replace(/\x1b\[<[0-9;]*[Mm]/g, "");
2296
2299
  }
2300
+ function isIncompleteEscapePrefix(str) {
2301
+ if (str === "\x1B" || str === "\x1BO") return true;
2302
+ return /^\x1b\[[0-9;?:<>=]*$/.test(str);
2303
+ }
2304
+ function enterRawStdin() {
2305
+ const wasRaw = Boolean(process.stdin.isRaw);
2306
+ try {
2307
+ if (process.stdin.isTTY) {
2308
+ if (wasRaw) {
2309
+ process.stdin.setRawMode(false);
2310
+ }
2311
+ process.stdin.setRawMode(true);
2312
+ }
2313
+ } catch {
2314
+ }
2315
+ try {
2316
+ process.stdin.ref?.();
2317
+ process.stdin.resume();
2318
+ } catch {
2319
+ }
2320
+ return wasRaw;
2321
+ }
2322
+ function parseTerminalEscape(rest) {
2323
+ const win32Match = rest.match(/^\x1b\[([0-9;]*)_/);
2324
+ if (win32Match) {
2325
+ const length = win32Match[0].length;
2326
+ const parts = win32Match[1].split(";");
2327
+ const vk = Number(parts[0] || 0);
2328
+ const uc = Number(parts[2] || 0);
2329
+ const isKeyDown = parts[3] === "1";
2330
+ const controlState = Number(parts[4] || 0);
2331
+ if (!isKeyDown) return { type: "noop", length };
2332
+ if (uc === 3 || vk === 67 && (controlState & 12) !== 0) return { type: "ctrl_c", length };
2333
+ if (vk === 38 || uc === 0 && vk === 104) return { type: "up", length };
2334
+ if (vk === 40 || uc === 0 && vk === 98) return { type: "down", length };
2335
+ if (vk === 37 || uc === 0 && vk === 100) return { type: "left", length };
2336
+ if (vk === 39 || uc === 0 && vk === 102) return { type: "right", length };
2337
+ if (vk === 33) return { type: "page_up", length };
2338
+ if (vk === 34) return { type: "page_down", length };
2339
+ if (vk === 36) return { type: "home", length };
2340
+ if (vk === 35) return { type: "end", length };
2341
+ if (vk === 13) return { type: "enter", length };
2342
+ if (vk === 27) return { type: "escape", length };
2343
+ if (vk === 9) return { type: "tab", length };
2344
+ if (vk === 8) return { type: "backspace", length };
2345
+ if (vk === 46) return { type: "delete", length };
2346
+ if (uc >= 32) return { type: "char", char: String.fromCodePoint(uc), length };
2347
+ return { type: "noop", length };
2348
+ }
2349
+ if (rest.startsWith("\x1B\r") || rest.startsWith("\x1B\r")) {
2350
+ return { type: "newline", length: 2 };
2351
+ }
2352
+ const kittyEnterMatch = rest.match(/^\x1b\[13(?:;(\d+)(?::(\d+))?)?u/);
2353
+ if (kittyEnterMatch) {
2354
+ if (kittyEnterMatch[2] === "3") return { type: "noop", length: kittyEnterMatch[0].length };
2355
+ const mod = kittyEnterMatch[1] ? Number(kittyEnterMatch[1]) : 1;
2356
+ return { type: mod > 1 ? "newline" : "enter", length: kittyEnterMatch[0].length };
2357
+ }
2358
+ const kittyEscMatch = rest.match(/^\x1b\[27(?:;[0-9:]+)?u/);
2359
+ if (kittyEscMatch) {
2360
+ if (/:3(?:$|;)/.test(kittyEscMatch[0])) return { type: "noop", length: kittyEscMatch[0].length };
2361
+ return { type: "escape", length: kittyEscMatch[0].length };
2362
+ }
2363
+ const kittyTabMatch = rest.match(/^\x1b\[9(?:;1(?::(\d+))?)?u/);
2364
+ if (kittyTabMatch) {
2365
+ if (kittyTabMatch[1] === "3") return { type: "noop", length: kittyTabMatch[0].length };
2366
+ return { type: "tab", length: kittyTabMatch[0].length };
2367
+ }
2368
+ const csiNavMatch = rest.match(/^\x1b\[([0-9;:]*)([ABCDHF])/);
2369
+ if (csiNavMatch) {
2370
+ if (/:3(?:$|;)/.test(csiNavMatch[1])) {
2371
+ return { type: "noop", length: csiNavMatch[0].length };
2372
+ }
2373
+ const dirMap = {
2374
+ A: "up",
2375
+ B: "down",
2376
+ C: "right",
2377
+ D: "left",
2378
+ H: "home",
2379
+ F: "end"
2380
+ };
2381
+ return { type: dirMap[csiNavMatch[2]], length: csiNavMatch[0].length };
2382
+ }
2383
+ const rxvtNavMatch = rest.match(/^\x1b\[([abcd])/);
2384
+ if (rxvtNavMatch) {
2385
+ const dirMap = {
2386
+ a: "up",
2387
+ b: "down",
2388
+ c: "right",
2389
+ d: "left"
2390
+ };
2391
+ return { type: dirMap[rxvtNavMatch[1]], length: 3 };
2392
+ }
2393
+ const tildeMatch = rest.match(/^\x1b\[(\d+)(?:;([0-9:]+))?~/);
2394
+ if (tildeMatch) {
2395
+ const code = tildeMatch[1];
2396
+ const length = tildeMatch[0].length;
2397
+ if (tildeMatch[2] && /:3(?:$|;)/.test(tildeMatch[2])) {
2398
+ return { type: "noop", length };
2399
+ }
2400
+ if (code === "5") return { type: "page_up", length };
2401
+ if (code === "6") return { type: "page_down", length };
2402
+ if (code === "1" || code === "7") return { type: "home", length };
2403
+ if (code === "4" || code === "8") return { type: "end", length };
2404
+ if (code === "3") return { type: "delete", length };
2405
+ if (code === "13") return { type: "enter", length };
2406
+ if (code === "12") return { type: "f2", length };
2407
+ return { type: "noop", length };
2408
+ }
2409
+ const ss3Match = rest.match(/^\x1bO([A-Za-z])/);
2410
+ if (ss3Match) {
2411
+ const ch = ss3Match[1];
2412
+ const ss3Map = {
2413
+ A: "up",
2414
+ a: "up",
2415
+ B: "down",
2416
+ b: "down",
2417
+ C: "right",
2418
+ c: "right",
2419
+ D: "left",
2420
+ d: "left",
2421
+ H: "home",
2422
+ F: "end",
2423
+ M: "enter",
2424
+ Q: "f2"
2425
+ };
2426
+ return { type: ss3Map[ch] || "noop", length: 3 };
2427
+ }
2428
+ const genericCsi = rest.match(/^\x1b\[[0-9;?:<>=]*[a-zA-Z~_]/);
2429
+ if (genericCsi) {
2430
+ return { type: "noop", length: genericCsi[0].length };
2431
+ }
2432
+ if (isIncompleteEscapePrefix(rest)) {
2433
+ return null;
2434
+ }
2435
+ return { type: "escape", length: 1 };
2436
+ }
2297
2437
  var _interactiveSelectActive = false;
2438
+ var _lastInteractivePromptEndMs = 0;
2298
2439
  function isInteractivePromptActive() {
2299
2440
  return _interactiveSelectActive;
2300
2441
  }
2442
+ function wasInteractivePromptRecentlyActive(withinMs = 600) {
2443
+ return _interactiveSelectActive || Date.now() - _lastInteractivePromptEndMs < withinMs;
2444
+ }
2301
2445
  function isDockedPromptWaiting() {
2302
2446
  return _dockedPromptActive;
2303
2447
  }
@@ -2333,12 +2477,7 @@ async function interactiveSelect(config) {
2333
2477
  _interactiveSelectActive = true;
2334
2478
  let cursor = config.options.findIndex((o) => o.value === config.initialValue);
2335
2479
  if (cursor === -1) cursor = 0;
2336
- const wasRaw = process.stdin.isRaw;
2337
- try {
2338
- process.stdin.setRawMode(true);
2339
- } catch {
2340
- }
2341
- process.stdin.resume();
2480
+ const wasRaw = enterRawStdin();
2342
2481
  process.stdout.write("\x1B[?25l");
2343
2482
  let termRows = process.stdout.rows || 24;
2344
2483
  let termCols = process.stdout.columns || 80;
@@ -2355,7 +2494,7 @@ async function interactiveSelect(config) {
2355
2494
  const renderFrame = () => {
2356
2495
  updateScroll();
2357
2496
  const maxVisible = getMaxVisible();
2358
- const safeWidth = Math.max(40, termCols - 4);
2497
+ const safeWidth = Math.max(16, termCols - 2);
2359
2498
  const lines = [];
2360
2499
  lines.push(truncateWithAnsi(chalk4.cyan.bold("\u25C6 ") + chalk4.bold.white(config.message), safeWidth));
2361
2500
  const visibleSlice = config.options.slice(scrollOffset, scrollOffset + maxVisible);
@@ -2407,11 +2546,17 @@ async function interactiveSelect(config) {
2407
2546
  process.stdout.write(frame + "\n");
2408
2547
  lastLineCount = frame.split("\n").length;
2409
2548
  };
2549
+ let numBuffer = "";
2550
+ let numTimeout = null;
2551
+ let escBuffer = "";
2552
+ let escTimeout = null;
2410
2553
  const cleanup = () => {
2411
2554
  _interactiveSelectActive = false;
2555
+ _lastInteractivePromptEndMs = Date.now();
2412
2556
  process.stdin.removeListener("data", onData);
2413
2557
  process.stdout.removeListener("resize", onResize);
2414
2558
  if (numTimeout) clearTimeout(numTimeout);
2559
+ if (escTimeout) clearTimeout(escTimeout);
2415
2560
  try {
2416
2561
  if (!wasRaw && process.stdin.isTTY) {
2417
2562
  process.stdin.setRawMode(false);
@@ -2446,10 +2591,48 @@ async function interactiveSelect(config) {
2446
2591
  termCols = process.stdout.columns || 80;
2447
2592
  draw();
2448
2593
  };
2449
- let numBuffer = "";
2450
- let numTimeout = null;
2594
+ const moveUp = () => {
2595
+ cursor = (cursor - 1 + config.options.length) % config.options.length;
2596
+ };
2597
+ const moveDown = () => {
2598
+ cursor = (cursor + 1) % config.options.length;
2599
+ };
2600
+ const applyChar = (ch) => {
2601
+ if (ch === "q" || ch === "Q") return "cancel";
2602
+ if (ch === "\r" || ch === "\n") return "confirm";
2603
+ if (ch === "k" || ch === "K" || ch === "w" || ch === "W") {
2604
+ moveUp();
2605
+ return "dirty";
2606
+ }
2607
+ if (ch === "j" || ch === "J" || ch === "s" || ch === "S") {
2608
+ moveDown();
2609
+ return "dirty";
2610
+ }
2611
+ if (/^[0-9]$/.test(ch)) {
2612
+ numBuffer += ch;
2613
+ const target = parseInt(numBuffer, 10);
2614
+ let changed = false;
2615
+ if (target >= 1 && target <= config.options.length) {
2616
+ cursor = target - 1;
2617
+ changed = true;
2618
+ }
2619
+ if (numTimeout) clearTimeout(numTimeout);
2620
+ numTimeout = setTimeout(() => {
2621
+ numBuffer = "";
2622
+ }, 700);
2623
+ return changed ? "dirty" : "none";
2624
+ }
2625
+ return "none";
2626
+ };
2451
2627
  const onData = (buf) => {
2452
- const str = stripTerminalControlNoise(buf.toString("utf-8"));
2628
+ if (escTimeout) {
2629
+ clearTimeout(escTimeout);
2630
+ escTimeout = null;
2631
+ }
2632
+ const raw = typeof buf === "string" ? buf : buf.toString("utf-8");
2633
+ const incoming = stripTerminalControlNoise(raw);
2634
+ const str = escBuffer + incoming;
2635
+ escBuffer = "";
2453
2636
  if (!str) return;
2454
2637
  let idx = 0;
2455
2638
  let dirty = false;
@@ -2460,109 +2643,74 @@ async function interactiveSelect(config) {
2460
2643
  process.stdout.write("\r\n" + chalk4.yellow("Exiting IUB CLI...\n"));
2461
2644
  process.exit(0);
2462
2645
  }
2463
- if (rest.startsWith("\x1B[A") || rest.startsWith("\x1BOA")) {
2464
- cursor = (cursor - 1 + config.options.length) % config.options.length;
2465
- idx += 3;
2466
- dirty = true;
2467
- continue;
2468
- }
2469
- if (rest.startsWith("\x1B[B") || rest.startsWith("\x1BOB")) {
2470
- cursor = (cursor + 1) % config.options.length;
2471
- idx += 3;
2472
- dirty = true;
2473
- continue;
2474
- }
2475
- if (rest.startsWith("\x1B[5~")) {
2476
- cursor = Math.max(0, cursor - getMaxVisible());
2477
- idx += 4;
2478
- dirty = true;
2479
- continue;
2480
- }
2481
- if (rest.startsWith("\x1B[6~")) {
2482
- cursor = Math.min(config.options.length - 1, cursor + getMaxVisible());
2483
- idx += 4;
2484
- dirty = true;
2485
- continue;
2486
- }
2487
- if (rest.startsWith("\x1B[H")) {
2488
- cursor = 0;
2489
- idx += 3;
2490
- dirty = true;
2491
- continue;
2492
- }
2493
- if (rest.startsWith("\x1B[1~")) {
2494
- cursor = 0;
2495
- idx += 4;
2496
- dirty = true;
2497
- continue;
2498
- }
2499
- if (rest.startsWith("\x1B[F")) {
2500
- cursor = config.options.length - 1;
2501
- idx += 3;
2502
- dirty = true;
2503
- continue;
2504
- }
2505
- if (rest.startsWith("\x1B[4~")) {
2506
- cursor = config.options.length - 1;
2507
- idx += 4;
2508
- dirty = true;
2509
- continue;
2510
- }
2511
- if (rest.startsWith("\x1BOM")) {
2512
- confirm();
2513
- return;
2514
- }
2515
- if (rest.startsWith("\x1B[13~")) {
2516
- confirm();
2517
- return;
2518
- }
2519
- const ch = str[idx];
2520
- if (ch === "\x1B" && idx === str.length - 1) {
2521
- cancel();
2522
- return;
2523
- }
2524
- if (ch === "\x1B") {
2525
- const csiMatch = rest.match(/^\x1b\[[0-9;]*[a-zA-Z~]/);
2526
- if (csiMatch) {
2527
- idx += csiMatch[0].length;
2528
- continue;
2646
+ if (rest[0] === "\x1B") {
2647
+ const parsed = parseTerminalEscape(rest);
2648
+ if (parsed === null) {
2649
+ escBuffer = rest;
2650
+ escTimeout = setTimeout(() => {
2651
+ escBuffer = "";
2652
+ escTimeout = null;
2653
+ cancel();
2654
+ }, 60);
2655
+ break;
2529
2656
  }
2530
- cancel();
2531
- return;
2657
+ if (parsed.type === "ctrl_c") {
2658
+ cleanup();
2659
+ process.stdout.write("\r\n" + chalk4.yellow("Exiting IUB CLI...\n"));
2660
+ process.exit(0);
2661
+ }
2662
+ if (parsed.type === "escape") {
2663
+ cancel();
2664
+ return;
2665
+ }
2666
+ if (parsed.type === "enter") {
2667
+ confirm();
2668
+ return;
2669
+ }
2670
+ if (parsed.type === "up" || parsed.type === "left") {
2671
+ moveUp();
2672
+ dirty = true;
2673
+ } else if (parsed.type === "down" || parsed.type === "right") {
2674
+ moveDown();
2675
+ dirty = true;
2676
+ } else if (parsed.type === "page_up") {
2677
+ cursor = Math.max(0, cursor - getMaxVisible());
2678
+ dirty = true;
2679
+ } else if (parsed.type === "page_down") {
2680
+ cursor = Math.min(config.options.length - 1, cursor + getMaxVisible());
2681
+ dirty = true;
2682
+ } else if (parsed.type === "home") {
2683
+ cursor = 0;
2684
+ dirty = true;
2685
+ } else if (parsed.type === "end") {
2686
+ cursor = config.options.length - 1;
2687
+ dirty = true;
2688
+ } else if (parsed.type === "char") {
2689
+ const res2 = applyChar(parsed.char);
2690
+ if (res2 === "cancel") {
2691
+ cancel();
2692
+ return;
2693
+ }
2694
+ if (res2 === "confirm") {
2695
+ confirm();
2696
+ return;
2697
+ }
2698
+ if (res2 === "dirty") dirty = true;
2699
+ }
2700
+ idx += parsed.length;
2701
+ continue;
2532
2702
  }
2533
- if (ch === "q" || ch === "Q") {
2703
+ const res = applyChar(str[idx]);
2704
+ if (res === "cancel") {
2534
2705
  cancel();
2535
2706
  return;
2536
2707
  }
2537
- if (ch === "\r" || ch === "\n") {
2708
+ if (res === "confirm") {
2538
2709
  confirm();
2539
2710
  return;
2540
2711
  }
2541
- if (ch === "k" || ch === "K" || ch === "w" || ch === "W") {
2542
- cursor = (cursor - 1 + config.options.length) % config.options.length;
2543
- idx += 1;
2712
+ if (res === "dirty") {
2544
2713
  dirty = true;
2545
- continue;
2546
- }
2547
- if (ch === "j" || ch === "J" || ch === "s" || ch === "S") {
2548
- cursor = (cursor + 1) % config.options.length;
2549
- idx += 1;
2550
- dirty = true;
2551
- continue;
2552
- }
2553
- if (/^[0-9]$/.test(ch)) {
2554
- numBuffer += ch;
2555
- const target = parseInt(numBuffer, 10);
2556
- if (target >= 1 && target <= config.options.length) {
2557
- cursor = target - 1;
2558
- dirty = true;
2559
- }
2560
- if (numTimeout) clearTimeout(numTimeout);
2561
- numTimeout = setTimeout(() => {
2562
- numBuffer = "";
2563
- }, 700);
2564
- idx += 1;
2565
- continue;
2566
2714
  }
2567
2715
  idx += 1;
2568
2716
  }
@@ -2571,7 +2719,7 @@ async function interactiveSelect(config) {
2571
2719
  }
2572
2720
  };
2573
2721
  process.stdout.on("resize", onResize);
2574
- process.stdin.on("data", onData);
2722
+ process.stdin.prependListener("data", onData);
2575
2723
  draw();
2576
2724
  });
2577
2725
  }
@@ -2593,10 +2741,8 @@ async function interactiveInput(config) {
2593
2741
  let inBracketedPaste = false;
2594
2742
  let pasteBuf = "";
2595
2743
  let lastLines = 0;
2596
- const wasRaw = process.stdin.isRaw;
2744
+ const wasRaw = enterRawStdin();
2597
2745
  try {
2598
- process.stdin.setRawMode(true);
2599
- process.stdin.resume();
2600
2746
  process.stdout.write("\x1B[?2004h");
2601
2747
  } catch {
2602
2748
  }
@@ -2631,6 +2777,7 @@ async function interactiveInput(config) {
2631
2777
  };
2632
2778
  const cleanup = () => {
2633
2779
  _interactiveSelectActive = false;
2780
+ _lastInteractivePromptEndMs = Date.now();
2634
2781
  try {
2635
2782
  process.stdout.write("\x1B[?2004l");
2636
2783
  } catch {
@@ -2805,7 +2952,7 @@ async function interactiveInput(config) {
2805
2952
  lastLines = initLines.length;
2806
2953
  const initCol = Math.min(termCols - 2, 5 + (value.length === 0 ? 0 : cursorPos));
2807
2954
  process.stdout.write(`\x1B[${lastLines - 1}A\x1B[${initCol}G\x1B[?25h`);
2808
- process.stdin.on("data", onData);
2955
+ process.stdin.prependListener("data", onData);
2809
2956
  });
2810
2957
  }
2811
2958
  function updateDockedModel(provider, model, mode) {
@@ -2908,12 +3055,7 @@ async function readInlinePrompt(options) {
2908
3055
  process.stdout.write("\x1B[?2004h");
2909
3056
  } catch {
2910
3057
  }
2911
- const wasRaw = process.stdin.isRaw;
2912
- try {
2913
- process.stdin.setRawMode(true);
2914
- process.stdin.resume();
2915
- } catch {
2916
- }
3058
+ const wasRaw = enterRawStdin();
2917
3059
  let bufferLines = [""];
2918
3060
  let cursorLine = 0;
2919
3061
  let cursorCol = 0;
@@ -2970,8 +3112,11 @@ async function readInlinePrompt(options) {
2970
3112
  const linesUp = lines.length - 1 - (cursorLine - startLine);
2971
3113
  process.stdout.write(`\x1B[${linesUp}A\r\x1B[${getActiveCol()}G`);
2972
3114
  };
3115
+ let escBuffer = "";
3116
+ let escTimeout = null;
2973
3117
  const cleanup = () => {
2974
3118
  _dockedPromptActive = false;
3119
+ if (escTimeout) clearTimeout(escTimeout);
2975
3120
  try {
2976
3121
  process.stdout.write("\x1B[?2004l");
2977
3122
  } catch {
@@ -3031,8 +3176,112 @@ async function readInlinePrompt(options) {
3031
3176
  redrawFrom(prevLine);
3032
3177
  }
3033
3178
  };
3179
+ const insertNewline = () => {
3180
+ const cur = bufferLines[cursorLine];
3181
+ const prevLine = cursorLine;
3182
+ bufferLines[cursorLine] = cur.slice(0, cursorCol);
3183
+ bufferLines.splice(cursorLine + 1, 0, cur.slice(cursorCol));
3184
+ cursorLine++;
3185
+ cursorCol = 0;
3186
+ redrawFrom(prevLine);
3187
+ };
3188
+ const moveLeft = () => {
3189
+ if (cursorCol > 0) {
3190
+ cursorCol--;
3191
+ updateActiveLine();
3192
+ } else if (cursorLine > 0) {
3193
+ cursorLine--;
3194
+ cursorCol = bufferLines[cursorLine].length;
3195
+ process.stdout.write("\x1B[1A");
3196
+ updateActiveLine();
3197
+ }
3198
+ };
3199
+ const moveRight = () => {
3200
+ if (cursorCol < bufferLines[cursorLine].length) {
3201
+ cursorCol++;
3202
+ updateActiveLine();
3203
+ } else if (cursorLine < bufferLines.length - 1) {
3204
+ cursorLine++;
3205
+ cursorCol = 0;
3206
+ process.stdout.write("\x1B[1B");
3207
+ updateActiveLine();
3208
+ }
3209
+ };
3210
+ const moveUp = () => {
3211
+ if (cursorLine > 0) {
3212
+ cursorLine--;
3213
+ cursorCol = Math.min(cursorCol, bufferLines[cursorLine].length);
3214
+ process.stdout.write("\x1B[1A");
3215
+ updateActiveLine();
3216
+ } else if (_commandHistory.length > 0) {
3217
+ if (historyIdx === -1) savedDraft = bufferLines.join("\n");
3218
+ if (historyIdx < _commandHistory.length - 1) {
3219
+ historyIdx++;
3220
+ const histItem = _commandHistory[_commandHistory.length - 1 - historyIdx];
3221
+ bufferLines = histItem.split("\n");
3222
+ cursorLine = bufferLines.length - 1;
3223
+ cursorCol = bufferLines[cursorLine].length;
3224
+ redrawFrom(0);
3225
+ }
3226
+ }
3227
+ };
3228
+ const moveDown = () => {
3229
+ if (cursorLine < bufferLines.length - 1) {
3230
+ cursorLine++;
3231
+ cursorCol = Math.min(cursorCol, bufferLines[cursorLine].length);
3232
+ process.stdout.write("\x1B[1B");
3233
+ updateActiveLine();
3234
+ } else if (historyIdx > 0) {
3235
+ historyIdx--;
3236
+ const histItem = _commandHistory[_commandHistory.length - 1 - historyIdx];
3237
+ bufferLines = histItem.split("\n");
3238
+ cursorLine = 0;
3239
+ cursorCol = bufferLines[0].length;
3240
+ redrawFrom(0);
3241
+ } else if (historyIdx === 0) {
3242
+ historyIdx = -1;
3243
+ bufferLines = savedDraft.split("\n");
3244
+ cursorLine = 0;
3245
+ cursorCol = bufferLines[0].length;
3246
+ redrawFrom(0);
3247
+ }
3248
+ };
3249
+ const deleteForward = () => {
3250
+ const cur = bufferLines[cursorLine];
3251
+ if (cursorCol < cur.length) {
3252
+ bufferLines[cursorLine] = cur.slice(0, cursorCol) + cur.slice(cursorCol + 1);
3253
+ updateActiveLine();
3254
+ } else if (cursorLine < bufferLines.length - 1) {
3255
+ bufferLines[cursorLine] = cur + bufferLines[cursorLine + 1];
3256
+ bufferLines.splice(cursorLine + 1, 1);
3257
+ redrawFrom(cursorLine);
3258
+ }
3259
+ };
3260
+ const deleteBackward = () => {
3261
+ if (cursorCol > 0) {
3262
+ const cur = bufferLines[cursorLine];
3263
+ bufferLines[cursorLine] = cur.slice(0, cursorCol - 1) + cur.slice(cursorCol);
3264
+ cursorCol--;
3265
+ updateActiveLine();
3266
+ } else if (cursorLine > 0) {
3267
+ const cur = bufferLines[cursorLine];
3268
+ const prev = bufferLines[cursorLine - 1];
3269
+ cursorCol = prev.length;
3270
+ bufferLines[cursorLine - 1] = prev + cur;
3271
+ bufferLines.splice(cursorLine, 1);
3272
+ cursorLine--;
3273
+ process.stdout.write("\x1B[1A");
3274
+ redrawFrom(cursorLine);
3275
+ }
3276
+ };
3034
3277
  const onData = (data) => {
3035
- let str = stripTerminalControlNoise(data.toString("utf-8"));
3278
+ if (escTimeout) {
3279
+ clearTimeout(escTimeout);
3280
+ escTimeout = null;
3281
+ }
3282
+ const rawStr = typeof data === "string" ? data : data.toString("utf-8");
3283
+ let str = escBuffer + stripTerminalControlNoise(rawStr);
3284
+ escBuffer = "";
3036
3285
  if (!str) return;
3037
3286
  if (str.includes("\x1B[200~")) {
3038
3287
  inBracketedPaste = true;
@@ -3089,7 +3338,7 @@ async function readInlinePrompt(options) {
3089
3338
  process.stdout.write(chalk4.yellow("Exiting IUB CLI...\n"));
3090
3339
  process.exit(0);
3091
3340
  }
3092
- if (rest.startsWith(" ") || rest.startsWith("\x1BOQ") || rest.startsWith("\x1B[12~")) {
3341
+ if (rest.startsWith(" ")) {
3093
3342
  cleanup();
3094
3343
  const linesUpToTop = cursorLine + 1;
3095
3344
  process.stdout.write(`\x1B[${linesUpToTop}A\r\x1B[J`);
@@ -3108,44 +3357,103 @@ async function readInlinePrompt(options) {
3108
3357
  idx += 1;
3109
3358
  continue;
3110
3359
  }
3111
- if (rest.startsWith("\x1B\r") || rest.startsWith("\x1B[13;2u") || rest.startsWith("\x1B[13;5u")) {
3112
- const seqLen = rest.startsWith("\x1B\r") ? 2 : 7;
3113
- const cur2 = bufferLines[cursorLine];
3114
- const prevLine = cursorLine;
3115
- bufferLines[cursorLine] = cur2.slice(0, cursorCol);
3116
- bufferLines.splice(cursorLine + 1, 0, cur2.slice(cursorCol));
3117
- cursorLine++;
3118
- cursorCol = 0;
3119
- redrawFrom(prevLine);
3120
- idx += seqLen;
3121
- continue;
3122
- }
3123
- if (rest.startsWith("\r\n") || rest.startsWith("\n")) {
3124
- if (bufferLines.join("\n").trim().length > 0) {
3125
- submit();
3360
+ if (rest[0] === "\x1B") {
3361
+ const parsed = parseTerminalEscape(rest);
3362
+ if (parsed === null) {
3363
+ escBuffer = rest;
3364
+ escTimeout = setTimeout(() => {
3365
+ escBuffer = "";
3366
+ escTimeout = null;
3367
+ cancel();
3368
+ }, 60);
3126
3369
  return;
3127
3370
  }
3128
- idx += rest.startsWith("\r\n") ? 2 : 1;
3371
+ if (parsed.type === "ctrl_c") {
3372
+ cleanup();
3373
+ const down = bufferLines.length - 1 - cursorLine + 2;
3374
+ process.stdout.write(`\x1B[${down}B\r
3375
+ `);
3376
+ process.stdout.write(chalk4.yellow("Exiting IUB CLI...\n"));
3377
+ process.exit(0);
3378
+ }
3379
+ if (parsed.type === "tab" || parsed.type === "f2") {
3380
+ cleanup();
3381
+ const linesUpToTop = cursorLine + 1;
3382
+ process.stdout.write(`\x1B[${linesUpToTop}A\r\x1B[J`);
3383
+ resolve("/model");
3384
+ return;
3385
+ }
3386
+ if (parsed.type === "newline") {
3387
+ insertNewline();
3388
+ } else if (parsed.type === "enter") {
3389
+ if (bufferLines.join("\n").trim().length > 0) {
3390
+ submit();
3391
+ return;
3392
+ }
3393
+ } else if (parsed.type === "escape") {
3394
+ cancel();
3395
+ } else if (parsed.type === "home") {
3396
+ cursorCol = 0;
3397
+ updateActiveLine();
3398
+ } else if (parsed.type === "end") {
3399
+ cursorCol = bufferLines[cursorLine].length;
3400
+ updateActiveLine();
3401
+ } else if (parsed.type === "delete") {
3402
+ deleteForward();
3403
+ } else if (parsed.type === "backspace") {
3404
+ deleteBackward();
3405
+ } else if (parsed.type === "left") {
3406
+ moveLeft();
3407
+ } else if (parsed.type === "right") {
3408
+ moveRight();
3409
+ } else if (parsed.type === "up") {
3410
+ moveUp();
3411
+ } else if (parsed.type === "down") {
3412
+ moveDown();
3413
+ } else if (parsed.type === "page_up") {
3414
+ if (_commandHistory.length > 0) {
3415
+ if (historyIdx === -1) savedDraft = bufferLines.join("\n");
3416
+ historyIdx = _commandHistory.length - 1;
3417
+ bufferLines = _commandHistory[0].split("\n");
3418
+ cursorLine = 0;
3419
+ cursorCol = bufferLines[0].length;
3420
+ redrawFrom(0);
3421
+ }
3422
+ } else if (parsed.type === "page_down") {
3423
+ if (historyIdx !== -1) {
3424
+ historyIdx = -1;
3425
+ bufferLines = savedDraft.split("\n");
3426
+ cursorLine = 0;
3427
+ cursorCol = bufferLines[0].length;
3428
+ redrawFrom(0);
3429
+ }
3430
+ } else if (parsed.type === "char") {
3431
+ const cur2 = bufferLines[cursorLine];
3432
+ bufferLines[cursorLine] = cur2.slice(0, cursorCol) + parsed.char + cur2.slice(cursorCol);
3433
+ cursorCol += parsed.char.length;
3434
+ updateActiveLine();
3435
+ }
3436
+ idx += parsed.length;
3129
3437
  continue;
3130
3438
  }
3131
- if (rest.startsWith("\r") || rest.startsWith("\x1BOM") || rest.startsWith("\x1B[13~")) {
3439
+ if (rest.startsWith("\r\n") || rest.startsWith("\n") || rest.startsWith("\r")) {
3132
3440
  if (bufferLines.join("\n").trim().length > 0) {
3133
3441
  submit();
3134
3442
  return;
3135
3443
  }
3136
- idx += rest.startsWith("\r") ? 1 : rest.startsWith("\x1BOM") ? 3 : 5;
3444
+ idx += rest.startsWith("\r\n") ? 2 : 1;
3137
3445
  continue;
3138
3446
  }
3139
- if (rest.startsWith("") || rest.startsWith("\x1B[H") || rest.startsWith("\x1B[1~") || rest.startsWith("\x1BOH")) {
3447
+ if (rest.startsWith("")) {
3140
3448
  cursorCol = 0;
3141
3449
  updateActiveLine();
3142
- idx += rest.startsWith("") ? 1 : rest.startsWith("\x1B[1~") ? 4 : 3;
3450
+ idx += 1;
3143
3451
  continue;
3144
3452
  }
3145
- if (rest.startsWith("") || rest.startsWith("\x1B[F") || rest.startsWith("\x1B[4~") || rest.startsWith("\x1BOF")) {
3453
+ if (rest.startsWith("")) {
3146
3454
  cursorCol = bufferLines[cursorLine].length;
3147
3455
  updateActiveLine();
3148
- idx += rest.startsWith("") ? 1 : rest.startsWith("\x1B[4~") ? 4 : 3;
3456
+ idx += 1;
3149
3457
  continue;
3150
3458
  }
3151
3459
  if (rest.startsWith("")) {
@@ -3155,142 +3463,18 @@ async function readInlinePrompt(options) {
3155
3463
  idx += 1;
3156
3464
  continue;
3157
3465
  }
3158
- if (rest.startsWith("\x1B[3~")) {
3159
- const cur2 = bufferLines[cursorLine];
3160
- if (cursorCol < cur2.length) {
3161
- bufferLines[cursorLine] = cur2.slice(0, cursorCol) + cur2.slice(cursorCol + 1);
3162
- updateActiveLine();
3163
- } else if (cursorLine < bufferLines.length - 1) {
3164
- bufferLines[cursorLine] = cur2 + bufferLines[cursorLine + 1];
3165
- bufferLines.splice(cursorLine + 1, 1);
3166
- redrawFrom(cursorLine);
3167
- }
3168
- idx += 4;
3169
- continue;
3170
- }
3171
- if (rest.startsWith("\b") || rest.startsWith("\x7F")) {
3172
- if (cursorCol > 0) {
3173
- const cur2 = bufferLines[cursorLine];
3174
- bufferLines[cursorLine] = cur2.slice(0, cursorCol - 1) + cur2.slice(cursorCol);
3175
- cursorCol--;
3176
- updateActiveLine();
3177
- } else if (cursorLine > 0) {
3178
- const cur2 = bufferLines[cursorLine];
3179
- const prev = bufferLines[cursorLine - 1];
3180
- cursorCol = prev.length;
3181
- bufferLines[cursorLine - 1] = prev + cur2;
3182
- bufferLines.splice(cursorLine, 1);
3183
- cursorLine--;
3184
- process.stdout.write("\x1B[1A");
3185
- redrawFrom(cursorLine);
3186
- }
3466
+ if (rest.startsWith("")) {
3467
+ moveUp();
3187
3468
  idx += 1;
3188
3469
  continue;
3189
3470
  }
3190
- if (rest.startsWith("\x1B[D") || rest.startsWith("\x1BOD")) {
3191
- if (cursorCol > 0) {
3192
- cursorCol--;
3193
- updateActiveLine();
3194
- } else if (cursorLine > 0) {
3195
- cursorLine--;
3196
- cursorCol = bufferLines[cursorLine].length;
3197
- process.stdout.write("\x1B[1A");
3198
- updateActiveLine();
3199
- }
3200
- idx += 3;
3201
- continue;
3202
- }
3203
- if (rest.startsWith("\x1B[C") || rest.startsWith("\x1BOC")) {
3204
- if (cursorCol < bufferLines[cursorLine].length) {
3205
- cursorCol++;
3206
- updateActiveLine();
3207
- } else if (cursorLine < bufferLines.length - 1) {
3208
- cursorLine++;
3209
- cursorCol = 0;
3210
- process.stdout.write("\x1B[1B");
3211
- updateActiveLine();
3212
- }
3213
- idx += 3;
3214
- continue;
3215
- }
3216
- if (rest.startsWith("\x1B[A") || rest.startsWith("\x1BOA") || rest.startsWith("")) {
3217
- const seqLen = rest.startsWith("") ? 1 : 3;
3218
- if (cursorLine > 0) {
3219
- cursorLine--;
3220
- cursorCol = Math.min(cursorCol, bufferLines[cursorLine].length);
3221
- process.stdout.write("\x1B[1A");
3222
- updateActiveLine();
3223
- } else if (_commandHistory.length > 0) {
3224
- if (historyIdx === -1) savedDraft = bufferLines.join("\n");
3225
- if (historyIdx < _commandHistory.length - 1) {
3226
- historyIdx++;
3227
- const histItem = _commandHistory[_commandHistory.length - 1 - historyIdx];
3228
- bufferLines = histItem.split("\n");
3229
- cursorLine = bufferLines.length - 1;
3230
- cursorCol = bufferLines[cursorLine].length;
3231
- redrawFrom(0);
3232
- }
3233
- }
3234
- idx += seqLen;
3235
- continue;
3236
- }
3237
- if (rest.startsWith("\x1B[B") || rest.startsWith("\x1BOB") || rest.startsWith("")) {
3238
- const seqLen = rest.startsWith("") ? 1 : 3;
3239
- if (cursorLine < bufferLines.length - 1) {
3240
- cursorLine++;
3241
- cursorCol = Math.min(cursorCol, bufferLines[cursorLine].length);
3242
- process.stdout.write("\x1B[1B");
3243
- updateActiveLine();
3244
- } else if (historyIdx > 0) {
3245
- historyIdx--;
3246
- const histItem = _commandHistory[_commandHistory.length - 1 - historyIdx];
3247
- bufferLines = histItem.split("\n");
3248
- cursorLine = 0;
3249
- cursorCol = bufferLines[0].length;
3250
- redrawFrom(0);
3251
- } else if (historyIdx === 0) {
3252
- historyIdx = -1;
3253
- bufferLines = savedDraft.split("\n");
3254
- cursorLine = 0;
3255
- cursorCol = bufferLines[0].length;
3256
- redrawFrom(0);
3257
- }
3258
- idx += seqLen;
3259
- continue;
3260
- }
3261
- if (rest.startsWith("\x1B[5~")) {
3262
- if (_commandHistory.length > 0) {
3263
- if (historyIdx === -1) savedDraft = bufferLines.join("\n");
3264
- historyIdx = _commandHistory.length - 1;
3265
- bufferLines = _commandHistory[0].split("\n");
3266
- cursorLine = 0;
3267
- cursorCol = bufferLines[0].length;
3268
- redrawFrom(0);
3269
- }
3270
- idx += 4;
3271
- continue;
3272
- }
3273
- if (rest.startsWith("\x1B[6~")) {
3274
- if (historyIdx !== -1) {
3275
- historyIdx = -1;
3276
- bufferLines = savedDraft.split("\n");
3277
- cursorLine = 0;
3278
- cursorCol = bufferLines[0].length;
3279
- redrawFrom(0);
3280
- }
3281
- idx += 4;
3471
+ if (rest.startsWith("")) {
3472
+ moveDown();
3473
+ idx += 1;
3282
3474
  continue;
3283
3475
  }
3284
- if (rest[0] === "\x1B") {
3285
- if (idx === str.length - 1) {
3286
- cancel();
3287
- return;
3288
- }
3289
- const csiMatch = rest.match(/^\x1b\[[0-9;]*[a-zA-Z~]/);
3290
- if (csiMatch) {
3291
- idx += csiMatch[0].length;
3292
- continue;
3293
- }
3476
+ if (rest.startsWith("\b") || rest.startsWith("\x7F")) {
3477
+ deleteBackward();
3294
3478
  idx += 1;
3295
3479
  continue;
3296
3480
  }
@@ -3313,7 +3497,7 @@ async function readInlinePrompt(options) {
3313
3497
  idx = printableEnd;
3314
3498
  }
3315
3499
  };
3316
- process.stdin.on("data", onData);
3500
+ process.stdin.prependListener("data", onData);
3317
3501
  });
3318
3502
  }
3319
3503
  async function readOpenCodePrompt(options) {
@@ -8345,41 +8529,73 @@ function attachEscapeListener(onEscape) {
8345
8529
  return () => {
8346
8530
  };
8347
8531
  }
8348
- readline.emitKeypressEvents(process.stdin);
8349
- const wasRaw = process.stdin.isRaw;
8350
- try {
8351
- process.stdin.setRawMode(true);
8352
- process.stdin.resume();
8353
- } catch {
8354
- }
8532
+ const wasRaw = enterRawStdin();
8355
8533
  let triggered = false;
8534
+ let escBuffer = "";
8535
+ let escTimer = null;
8356
8536
  const triggerEscape = () => {
8357
8537
  if (triggered) return;
8358
8538
  triggered = true;
8539
+ escBuffer = "";
8540
+ if (escTimer) {
8541
+ clearTimeout(escTimer);
8542
+ escTimer = null;
8543
+ }
8359
8544
  onEscape();
8360
8545
  };
8361
8546
  const onData = (chunk) => {
8362
- if (isInteractivePromptActive()) return;
8363
- const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
8364
- if (buf.length === 1 && buf[0] === 27) {
8365
- triggerEscape();
8366
- } else if (buf.length === 1 && buf[0] === 3) {
8367
- process.exit(0);
8547
+ if (escTimer) {
8548
+ clearTimeout(escTimer);
8549
+ escTimer = null;
8368
8550
  }
8369
- };
8370
- const keypressHandler = (str, key) => {
8371
- if (isInteractivePromptActive()) return;
8372
- if (key && key.name === "escape" || str === "\x1B" || str === "\x1B" || key && key.sequence === "\x1B") {
8373
- triggerEscape();
8374
- } else if (key && key.ctrl && key.name === "c") {
8375
- process.exit(0);
8551
+ if (isInteractivePromptActive() || wasInteractivePromptRecentlyActive(600)) {
8552
+ escBuffer = "";
8553
+ return;
8554
+ }
8555
+ const incoming = typeof chunk === "string" ? chunk : chunk.toString("utf-8");
8556
+ const str = escBuffer + incoming;
8557
+ escBuffer = "";
8558
+ if (!str) return;
8559
+ let idx = 0;
8560
+ while (idx < str.length) {
8561
+ const rest = str.slice(idx);
8562
+ if (rest.startsWith("")) {
8563
+ process.exit(0);
8564
+ }
8565
+ if (rest[0] === "\x1B") {
8566
+ const parsed = parseTerminalEscape(rest);
8567
+ if (parsed === null) {
8568
+ escBuffer = rest;
8569
+ escTimer = setTimeout(() => {
8570
+ escBuffer = "";
8571
+ escTimer = null;
8572
+ if (!isInteractivePromptActive() && !wasInteractivePromptRecentlyActive(600)) {
8573
+ triggerEscape();
8574
+ }
8575
+ }, 60);
8576
+ return;
8577
+ }
8578
+ if (parsed.type === "ctrl_c") {
8579
+ process.exit(0);
8580
+ }
8581
+ if (parsed.type === "escape") {
8582
+ triggerEscape();
8583
+ return;
8584
+ }
8585
+ idx += parsed.length;
8586
+ continue;
8587
+ }
8588
+ idx += 1;
8376
8589
  }
8377
8590
  };
8378
8591
  process.stdin.on("data", onData);
8379
- process.stdin.on("keypress", keypressHandler);
8380
8592
  return () => {
8593
+ escBuffer = "";
8594
+ if (escTimer) {
8595
+ clearTimeout(escTimer);
8596
+ escTimer = null;
8597
+ }
8381
8598
  process.stdin.removeListener("data", onData);
8382
- process.stdin.removeListener("keypress", keypressHandler);
8383
8599
  try {
8384
8600
  if (!wasRaw && process.stdin.isTTY) {
8385
8601
  process.stdin.setRawMode(false);