iubcli 4.2.14 → 4.2.15

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
@@ -194,7 +194,8 @@ function createSpinner(text) {
194
194
  text: chalk.cyan(text),
195
195
  color: "cyan",
196
196
  spinner: "dots",
197
- stream: process.stdout
197
+ stream: process.stdout,
198
+ discardStdin: false
198
199
  });
199
200
  }
200
201
  function renderPriorityTaskBanner(task, model, mode) {
@@ -972,6 +973,9 @@ function parseDirectoryChangeCommand(subCommand, currentCwd) {
972
973
  return null;
973
974
  }
974
975
  async function executeSequentialCommand(command, signal) {
976
+ if (signal?.aborted) {
977
+ return "Action aborted by user (Escape).";
978
+ }
975
979
  if (!safetySession.autoApproveAll) {
976
980
  const summary = `${chalk2.gray("Shell Command:")} ${chalk2.magenta.bold(command)}
977
981
  ${chalk2.gray("Working Directory:")} ${process.cwd()}`;
@@ -2294,10 +2298,123 @@ function truncateWithAnsi(str, maxWidth) {
2294
2298
  function stripTerminalControlNoise(raw) {
2295
2299
  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
2300
  }
2301
+ function isIncompleteEscapePrefix(str) {
2302
+ if (str === "\x1B" || str === "\x1BO") return true;
2303
+ return /^\x1b\[[0-9;?:]*$/.test(str);
2304
+ }
2305
+ function parseTerminalEscape(rest) {
2306
+ const win32Match = rest.match(/^\x1b\[(\d+);(\d+);(\d+);([01]);(\d+);(\d+)_/);
2307
+ if (win32Match) {
2308
+ 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]);
2313
+ if (!isKeyDown) return { type: "noop", length };
2314
+ 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 };
2319
+ if (vk === 33) return { type: "page_up", length };
2320
+ if (vk === 34) return { type: "page_down", length };
2321
+ if (vk === 36) return { type: "home", length };
2322
+ if (vk === 35) return { type: "end", length };
2323
+ if (vk === 13) return { type: "enter", length };
2324
+ if (vk === 27) return { type: "escape", length };
2325
+ if (vk === 9) return { type: "tab", length };
2326
+ if (vk === 8) return { type: "backspace", length };
2327
+ if (vk === 46) return { type: "delete", length };
2328
+ if (uc >= 32) return { type: "char", char: String.fromCodePoint(uc), length };
2329
+ return { type: "noop", length };
2330
+ }
2331
+ if (rest.startsWith("\x1B\r") || rest.startsWith("\x1B\r")) {
2332
+ return { type: "newline", length: 2 };
2333
+ }
2334
+ const kittyEnterMatch = rest.match(/^\x1b\[13(?:;(\d+)(?::\d+)?)?u/);
2335
+ if (kittyEnterMatch) {
2336
+ const mod = kittyEnterMatch[1] ? Number(kittyEnterMatch[1]) : 1;
2337
+ return { type: mod > 1 ? "newline" : "enter", length: kittyEnterMatch[0].length };
2338
+ }
2339
+ const kittyEscMatch = rest.match(/^\x1b\[27(?:;[0-9:]+)?u/);
2340
+ if (kittyEscMatch) {
2341
+ return { type: "escape", length: kittyEscMatch[0].length };
2342
+ }
2343
+ const kittyTabMatch = rest.match(/^\x1b\[9(?:;1(?::\d+)?)?u/);
2344
+ if (kittyTabMatch) {
2345
+ return { type: "tab", length: kittyTabMatch[0].length };
2346
+ }
2347
+ const csiNavMatch = rest.match(/^\x1b\[[0-9;:]*([ABCDHF])/);
2348
+ if (csiNavMatch) {
2349
+ const dirMap = {
2350
+ A: "up",
2351
+ B: "down",
2352
+ C: "right",
2353
+ D: "left",
2354
+ H: "home",
2355
+ F: "end"
2356
+ };
2357
+ return { type: dirMap[csiNavMatch[1]], length: csiNavMatch[0].length };
2358
+ }
2359
+ const rxvtNavMatch = rest.match(/^\x1b\[([abcd])/);
2360
+ if (rxvtNavMatch) {
2361
+ const dirMap = {
2362
+ a: "up",
2363
+ b: "down",
2364
+ c: "right",
2365
+ d: "left"
2366
+ };
2367
+ return { type: dirMap[rxvtNavMatch[1]], length: 3 };
2368
+ }
2369
+ const tildeMatch = rest.match(/^\x1b\[(\d+)(?:;[0-9:]+)?~/);
2370
+ if (tildeMatch) {
2371
+ const code = tildeMatch[1];
2372
+ const length = tildeMatch[0].length;
2373
+ if (code === "5") return { type: "page_up", length };
2374
+ if (code === "6") return { type: "page_down", length };
2375
+ if (code === "1" || code === "7") return { type: "home", length };
2376
+ if (code === "4" || code === "8") return { type: "end", length };
2377
+ if (code === "3") return { type: "delete", length };
2378
+ if (code === "13") return { type: "enter", length };
2379
+ if (code === "12") return { type: "f2", length };
2380
+ return { type: "noop", length };
2381
+ }
2382
+ const ss3Match = rest.match(/^\x1bO([A-Za-z])/);
2383
+ if (ss3Match) {
2384
+ const ch = ss3Match[1];
2385
+ const ss3Map = {
2386
+ A: "up",
2387
+ a: "up",
2388
+ B: "down",
2389
+ b: "down",
2390
+ C: "right",
2391
+ c: "right",
2392
+ D: "left",
2393
+ d: "left",
2394
+ H: "home",
2395
+ F: "end",
2396
+ M: "enter",
2397
+ Q: "f2"
2398
+ };
2399
+ return { type: ss3Map[ch] || "noop", length: 3 };
2400
+ }
2401
+ const genericCsi = rest.match(/^\x1b\[[0-9;?:]*[a-zA-Z~_]/);
2402
+ if (genericCsi) {
2403
+ return { type: "noop", length: genericCsi[0].length };
2404
+ }
2405
+ if (isIncompleteEscapePrefix(rest)) {
2406
+ return null;
2407
+ }
2408
+ return { type: "escape", length: 1 };
2409
+ }
2297
2410
  var _interactiveSelectActive = false;
2411
+ var _lastInteractivePromptEndMs = 0;
2298
2412
  function isInteractivePromptActive() {
2299
2413
  return _interactiveSelectActive;
2300
2414
  }
2415
+ function wasInteractivePromptRecentlyActive(withinMs = 600) {
2416
+ return _interactiveSelectActive || Date.now() - _lastInteractivePromptEndMs < withinMs;
2417
+ }
2301
2418
  function isDockedPromptWaiting() {
2302
2419
  return _dockedPromptActive;
2303
2420
  }
@@ -2355,7 +2472,7 @@ async function interactiveSelect(config) {
2355
2472
  const renderFrame = () => {
2356
2473
  updateScroll();
2357
2474
  const maxVisible = getMaxVisible();
2358
- const safeWidth = Math.max(40, termCols - 4);
2475
+ const safeWidth = Math.max(16, termCols - 2);
2359
2476
  const lines = [];
2360
2477
  lines.push(truncateWithAnsi(chalk4.cyan.bold("\u25C6 ") + chalk4.bold.white(config.message), safeWidth));
2361
2478
  const visibleSlice = config.options.slice(scrollOffset, scrollOffset + maxVisible);
@@ -2407,11 +2524,17 @@ async function interactiveSelect(config) {
2407
2524
  process.stdout.write(frame + "\n");
2408
2525
  lastLineCount = frame.split("\n").length;
2409
2526
  };
2527
+ let numBuffer = "";
2528
+ let numTimeout = null;
2529
+ let escBuffer = "";
2530
+ let escTimeout = null;
2410
2531
  const cleanup = () => {
2411
2532
  _interactiveSelectActive = false;
2533
+ _lastInteractivePromptEndMs = Date.now();
2412
2534
  process.stdin.removeListener("data", onData);
2413
2535
  process.stdout.removeListener("resize", onResize);
2414
2536
  if (numTimeout) clearTimeout(numTimeout);
2537
+ if (escTimeout) clearTimeout(escTimeout);
2415
2538
  try {
2416
2539
  if (!wasRaw && process.stdin.isTTY) {
2417
2540
  process.stdin.setRawMode(false);
@@ -2446,10 +2569,48 @@ async function interactiveSelect(config) {
2446
2569
  termCols = process.stdout.columns || 80;
2447
2570
  draw();
2448
2571
  };
2449
- let numBuffer = "";
2450
- let numTimeout = null;
2572
+ const moveUp = () => {
2573
+ cursor = (cursor - 1 + config.options.length) % config.options.length;
2574
+ };
2575
+ const moveDown = () => {
2576
+ cursor = (cursor + 1) % config.options.length;
2577
+ };
2578
+ const applyChar = (ch) => {
2579
+ if (ch === "q" || ch === "Q") return "cancel";
2580
+ if (ch === "\r" || ch === "\n") return "confirm";
2581
+ if (ch === "k" || ch === "K" || ch === "w" || ch === "W") {
2582
+ moveUp();
2583
+ return "dirty";
2584
+ }
2585
+ if (ch === "j" || ch === "J" || ch === "s" || ch === "S") {
2586
+ moveDown();
2587
+ return "dirty";
2588
+ }
2589
+ if (/^[0-9]$/.test(ch)) {
2590
+ numBuffer += ch;
2591
+ const target = parseInt(numBuffer, 10);
2592
+ let changed = false;
2593
+ if (target >= 1 && target <= config.options.length) {
2594
+ cursor = target - 1;
2595
+ changed = true;
2596
+ }
2597
+ if (numTimeout) clearTimeout(numTimeout);
2598
+ numTimeout = setTimeout(() => {
2599
+ numBuffer = "";
2600
+ }, 700);
2601
+ return changed ? "dirty" : "none";
2602
+ }
2603
+ return "none";
2604
+ };
2451
2605
  const onData = (buf) => {
2452
- const str = stripTerminalControlNoise(buf.toString("utf-8"));
2606
+ if (escTimeout) {
2607
+ clearTimeout(escTimeout);
2608
+ escTimeout = null;
2609
+ }
2610
+ const raw = typeof buf === "string" ? buf : buf.toString("utf-8");
2611
+ const incoming = stripTerminalControlNoise(raw);
2612
+ const str = escBuffer + incoming;
2613
+ escBuffer = "";
2453
2614
  if (!str) return;
2454
2615
  let idx = 0;
2455
2616
  let dirty = false;
@@ -2460,109 +2621,74 @@ async function interactiveSelect(config) {
2460
2621
  process.stdout.write("\r\n" + chalk4.yellow("Exiting IUB CLI...\n"));
2461
2622
  process.exit(0);
2462
2623
  }
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;
2624
+ if (rest[0] === "\x1B") {
2625
+ const parsed = parseTerminalEscape(rest);
2626
+ if (parsed === null) {
2627
+ escBuffer = rest;
2628
+ escTimeout = setTimeout(() => {
2629
+ escBuffer = "";
2630
+ escTimeout = null;
2631
+ cancel();
2632
+ }, 60);
2633
+ break;
2529
2634
  }
2530
- cancel();
2531
- return;
2635
+ if (parsed.type === "ctrl_c") {
2636
+ cleanup();
2637
+ process.stdout.write("\r\n" + chalk4.yellow("Exiting IUB CLI...\n"));
2638
+ process.exit(0);
2639
+ }
2640
+ if (parsed.type === "escape") {
2641
+ cancel();
2642
+ return;
2643
+ }
2644
+ if (parsed.type === "enter") {
2645
+ confirm();
2646
+ return;
2647
+ }
2648
+ if (parsed.type === "up" || parsed.type === "left") {
2649
+ moveUp();
2650
+ dirty = true;
2651
+ } else if (parsed.type === "down" || parsed.type === "right") {
2652
+ moveDown();
2653
+ dirty = true;
2654
+ } else if (parsed.type === "page_up") {
2655
+ cursor = Math.max(0, cursor - getMaxVisible());
2656
+ dirty = true;
2657
+ } else if (parsed.type === "page_down") {
2658
+ cursor = Math.min(config.options.length - 1, cursor + getMaxVisible());
2659
+ dirty = true;
2660
+ } else if (parsed.type === "home") {
2661
+ cursor = 0;
2662
+ dirty = true;
2663
+ } else if (parsed.type === "end") {
2664
+ cursor = config.options.length - 1;
2665
+ dirty = true;
2666
+ } else if (parsed.type === "char") {
2667
+ const res2 = applyChar(parsed.char);
2668
+ if (res2 === "cancel") {
2669
+ cancel();
2670
+ return;
2671
+ }
2672
+ if (res2 === "confirm") {
2673
+ confirm();
2674
+ return;
2675
+ }
2676
+ if (res2 === "dirty") dirty = true;
2677
+ }
2678
+ idx += parsed.length;
2679
+ continue;
2532
2680
  }
2533
- if (ch === "q" || ch === "Q") {
2681
+ const res = applyChar(str[idx]);
2682
+ if (res === "cancel") {
2534
2683
  cancel();
2535
2684
  return;
2536
2685
  }
2537
- if (ch === "\r" || ch === "\n") {
2686
+ if (res === "confirm") {
2538
2687
  confirm();
2539
2688
  return;
2540
2689
  }
2541
- if (ch === "k" || ch === "K" || ch === "w" || ch === "W") {
2542
- cursor = (cursor - 1 + config.options.length) % config.options.length;
2543
- idx += 1;
2690
+ if (res === "dirty") {
2544
2691
  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
2692
  }
2567
2693
  idx += 1;
2568
2694
  }
@@ -2631,6 +2757,7 @@ async function interactiveInput(config) {
2631
2757
  };
2632
2758
  const cleanup = () => {
2633
2759
  _interactiveSelectActive = false;
2760
+ _lastInteractivePromptEndMs = Date.now();
2634
2761
  try {
2635
2762
  process.stdout.write("\x1B[?2004l");
2636
2763
  } catch {
@@ -2970,8 +3097,11 @@ async function readInlinePrompt(options) {
2970
3097
  const linesUp = lines.length - 1 - (cursorLine - startLine);
2971
3098
  process.stdout.write(`\x1B[${linesUp}A\r\x1B[${getActiveCol()}G`);
2972
3099
  };
3100
+ let escBuffer = "";
3101
+ let escTimeout = null;
2973
3102
  const cleanup = () => {
2974
3103
  _dockedPromptActive = false;
3104
+ if (escTimeout) clearTimeout(escTimeout);
2975
3105
  try {
2976
3106
  process.stdout.write("\x1B[?2004l");
2977
3107
  } catch {
@@ -3031,8 +3161,112 @@ async function readInlinePrompt(options) {
3031
3161
  redrawFrom(prevLine);
3032
3162
  }
3033
3163
  };
3164
+ const insertNewline = () => {
3165
+ const cur = bufferLines[cursorLine];
3166
+ const prevLine = cursorLine;
3167
+ bufferLines[cursorLine] = cur.slice(0, cursorCol);
3168
+ bufferLines.splice(cursorLine + 1, 0, cur.slice(cursorCol));
3169
+ cursorLine++;
3170
+ cursorCol = 0;
3171
+ redrawFrom(prevLine);
3172
+ };
3173
+ const moveLeft = () => {
3174
+ if (cursorCol > 0) {
3175
+ cursorCol--;
3176
+ updateActiveLine();
3177
+ } else if (cursorLine > 0) {
3178
+ cursorLine--;
3179
+ cursorCol = bufferLines[cursorLine].length;
3180
+ process.stdout.write("\x1B[1A");
3181
+ updateActiveLine();
3182
+ }
3183
+ };
3184
+ const moveRight = () => {
3185
+ if (cursorCol < bufferLines[cursorLine].length) {
3186
+ cursorCol++;
3187
+ updateActiveLine();
3188
+ } else if (cursorLine < bufferLines.length - 1) {
3189
+ cursorLine++;
3190
+ cursorCol = 0;
3191
+ process.stdout.write("\x1B[1B");
3192
+ updateActiveLine();
3193
+ }
3194
+ };
3195
+ const moveUp = () => {
3196
+ if (cursorLine > 0) {
3197
+ cursorLine--;
3198
+ cursorCol = Math.min(cursorCol, bufferLines[cursorLine].length);
3199
+ process.stdout.write("\x1B[1A");
3200
+ updateActiveLine();
3201
+ } else if (_commandHistory.length > 0) {
3202
+ if (historyIdx === -1) savedDraft = bufferLines.join("\n");
3203
+ if (historyIdx < _commandHistory.length - 1) {
3204
+ historyIdx++;
3205
+ const histItem = _commandHistory[_commandHistory.length - 1 - historyIdx];
3206
+ bufferLines = histItem.split("\n");
3207
+ cursorLine = bufferLines.length - 1;
3208
+ cursorCol = bufferLines[cursorLine].length;
3209
+ redrawFrom(0);
3210
+ }
3211
+ }
3212
+ };
3213
+ const moveDown = () => {
3214
+ if (cursorLine < bufferLines.length - 1) {
3215
+ cursorLine++;
3216
+ cursorCol = Math.min(cursorCol, bufferLines[cursorLine].length);
3217
+ process.stdout.write("\x1B[1B");
3218
+ updateActiveLine();
3219
+ } else if (historyIdx > 0) {
3220
+ historyIdx--;
3221
+ const histItem = _commandHistory[_commandHistory.length - 1 - historyIdx];
3222
+ bufferLines = histItem.split("\n");
3223
+ cursorLine = 0;
3224
+ cursorCol = bufferLines[0].length;
3225
+ redrawFrom(0);
3226
+ } else if (historyIdx === 0) {
3227
+ historyIdx = -1;
3228
+ bufferLines = savedDraft.split("\n");
3229
+ cursorLine = 0;
3230
+ cursorCol = bufferLines[0].length;
3231
+ redrawFrom(0);
3232
+ }
3233
+ };
3234
+ const deleteForward = () => {
3235
+ const cur = bufferLines[cursorLine];
3236
+ if (cursorCol < cur.length) {
3237
+ bufferLines[cursorLine] = cur.slice(0, cursorCol) + cur.slice(cursorCol + 1);
3238
+ updateActiveLine();
3239
+ } else if (cursorLine < bufferLines.length - 1) {
3240
+ bufferLines[cursorLine] = cur + bufferLines[cursorLine + 1];
3241
+ bufferLines.splice(cursorLine + 1, 1);
3242
+ redrawFrom(cursorLine);
3243
+ }
3244
+ };
3245
+ const deleteBackward = () => {
3246
+ if (cursorCol > 0) {
3247
+ const cur = bufferLines[cursorLine];
3248
+ bufferLines[cursorLine] = cur.slice(0, cursorCol - 1) + cur.slice(cursorCol);
3249
+ cursorCol--;
3250
+ updateActiveLine();
3251
+ } else if (cursorLine > 0) {
3252
+ const cur = bufferLines[cursorLine];
3253
+ const prev = bufferLines[cursorLine - 1];
3254
+ cursorCol = prev.length;
3255
+ bufferLines[cursorLine - 1] = prev + cur;
3256
+ bufferLines.splice(cursorLine, 1);
3257
+ cursorLine--;
3258
+ process.stdout.write("\x1B[1A");
3259
+ redrawFrom(cursorLine);
3260
+ }
3261
+ };
3034
3262
  const onData = (data) => {
3035
- let str = stripTerminalControlNoise(data.toString("utf-8"));
3263
+ if (escTimeout) {
3264
+ clearTimeout(escTimeout);
3265
+ escTimeout = null;
3266
+ }
3267
+ const rawStr = typeof data === "string" ? data : data.toString("utf-8");
3268
+ let str = escBuffer + stripTerminalControlNoise(rawStr);
3269
+ escBuffer = "";
3036
3270
  if (!str) return;
3037
3271
  if (str.includes("\x1B[200~")) {
3038
3272
  inBracketedPaste = true;
@@ -3089,7 +3323,7 @@ async function readInlinePrompt(options) {
3089
3323
  process.stdout.write(chalk4.yellow("Exiting IUB CLI...\n"));
3090
3324
  process.exit(0);
3091
3325
  }
3092
- if (rest.startsWith(" ") || rest.startsWith("\x1BOQ") || rest.startsWith("\x1B[12~")) {
3326
+ if (rest.startsWith(" ")) {
3093
3327
  cleanup();
3094
3328
  const linesUpToTop = cursorLine + 1;
3095
3329
  process.stdout.write(`\x1B[${linesUpToTop}A\r\x1B[J`);
@@ -3108,44 +3342,103 @@ async function readInlinePrompt(options) {
3108
3342
  idx += 1;
3109
3343
  continue;
3110
3344
  }
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();
3345
+ if (rest[0] === "\x1B") {
3346
+ const parsed = parseTerminalEscape(rest);
3347
+ if (parsed === null) {
3348
+ escBuffer = rest;
3349
+ escTimeout = setTimeout(() => {
3350
+ escBuffer = "";
3351
+ escTimeout = null;
3352
+ cancel();
3353
+ }, 60);
3126
3354
  return;
3127
3355
  }
3128
- idx += rest.startsWith("\r\n") ? 2 : 1;
3356
+ if (parsed.type === "ctrl_c") {
3357
+ cleanup();
3358
+ const down = bufferLines.length - 1 - cursorLine + 2;
3359
+ process.stdout.write(`\x1B[${down}B\r
3360
+ `);
3361
+ process.stdout.write(chalk4.yellow("Exiting IUB CLI...\n"));
3362
+ process.exit(0);
3363
+ }
3364
+ if (parsed.type === "tab" || parsed.type === "f2") {
3365
+ cleanup();
3366
+ const linesUpToTop = cursorLine + 1;
3367
+ process.stdout.write(`\x1B[${linesUpToTop}A\r\x1B[J`);
3368
+ resolve("/model");
3369
+ return;
3370
+ }
3371
+ if (parsed.type === "newline") {
3372
+ insertNewline();
3373
+ } else if (parsed.type === "enter") {
3374
+ if (bufferLines.join("\n").trim().length > 0) {
3375
+ submit();
3376
+ return;
3377
+ }
3378
+ } else if (parsed.type === "escape") {
3379
+ cancel();
3380
+ } else if (parsed.type === "home") {
3381
+ cursorCol = 0;
3382
+ updateActiveLine();
3383
+ } else if (parsed.type === "end") {
3384
+ cursorCol = bufferLines[cursorLine].length;
3385
+ updateActiveLine();
3386
+ } else if (parsed.type === "delete") {
3387
+ deleteForward();
3388
+ } else if (parsed.type === "backspace") {
3389
+ deleteBackward();
3390
+ } else if (parsed.type === "left") {
3391
+ moveLeft();
3392
+ } else if (parsed.type === "right") {
3393
+ moveRight();
3394
+ } else if (parsed.type === "up") {
3395
+ moveUp();
3396
+ } else if (parsed.type === "down") {
3397
+ moveDown();
3398
+ } else if (parsed.type === "page_up") {
3399
+ if (_commandHistory.length > 0) {
3400
+ if (historyIdx === -1) savedDraft = bufferLines.join("\n");
3401
+ historyIdx = _commandHistory.length - 1;
3402
+ bufferLines = _commandHistory[0].split("\n");
3403
+ cursorLine = 0;
3404
+ cursorCol = bufferLines[0].length;
3405
+ redrawFrom(0);
3406
+ }
3407
+ } else if (parsed.type === "page_down") {
3408
+ if (historyIdx !== -1) {
3409
+ historyIdx = -1;
3410
+ bufferLines = savedDraft.split("\n");
3411
+ cursorLine = 0;
3412
+ cursorCol = bufferLines[0].length;
3413
+ redrawFrom(0);
3414
+ }
3415
+ } else if (parsed.type === "char") {
3416
+ const cur2 = bufferLines[cursorLine];
3417
+ bufferLines[cursorLine] = cur2.slice(0, cursorCol) + parsed.char + cur2.slice(cursorCol);
3418
+ cursorCol += parsed.char.length;
3419
+ updateActiveLine();
3420
+ }
3421
+ idx += parsed.length;
3129
3422
  continue;
3130
3423
  }
3131
- if (rest.startsWith("\r") || rest.startsWith("\x1BOM") || rest.startsWith("\x1B[13~")) {
3424
+ if (rest.startsWith("\r\n") || rest.startsWith("\n") || rest.startsWith("\r")) {
3132
3425
  if (bufferLines.join("\n").trim().length > 0) {
3133
3426
  submit();
3134
3427
  return;
3135
3428
  }
3136
- idx += rest.startsWith("\r") ? 1 : rest.startsWith("\x1BOM") ? 3 : 5;
3429
+ idx += rest.startsWith("\r\n") ? 2 : 1;
3137
3430
  continue;
3138
3431
  }
3139
- if (rest.startsWith("") || rest.startsWith("\x1B[H") || rest.startsWith("\x1B[1~") || rest.startsWith("\x1BOH")) {
3432
+ if (rest.startsWith("")) {
3140
3433
  cursorCol = 0;
3141
3434
  updateActiveLine();
3142
- idx += rest.startsWith("") ? 1 : rest.startsWith("\x1B[1~") ? 4 : 3;
3435
+ idx += 1;
3143
3436
  continue;
3144
3437
  }
3145
- if (rest.startsWith("") || rest.startsWith("\x1B[F") || rest.startsWith("\x1B[4~") || rest.startsWith("\x1BOF")) {
3438
+ if (rest.startsWith("")) {
3146
3439
  cursorCol = bufferLines[cursorLine].length;
3147
3440
  updateActiveLine();
3148
- idx += rest.startsWith("") ? 1 : rest.startsWith("\x1B[4~") ? 4 : 3;
3441
+ idx += 1;
3149
3442
  continue;
3150
3443
  }
3151
3444
  if (rest.startsWith("")) {
@@ -3155,142 +3448,18 @@ async function readInlinePrompt(options) {
3155
3448
  idx += 1;
3156
3449
  continue;
3157
3450
  }
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
- }
3451
+ if (rest.startsWith("")) {
3452
+ moveUp();
3187
3453
  idx += 1;
3188
3454
  continue;
3189
3455
  }
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;
3456
+ if (rest.startsWith("")) {
3457
+ moveDown();
3458
+ idx += 1;
3282
3459
  continue;
3283
3460
  }
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
- }
3461
+ if (rest.startsWith("\b") || rest.startsWith("\x7F")) {
3462
+ deleteBackward();
3294
3463
  idx += 1;
3295
3464
  continue;
3296
3465
  }
@@ -8353,31 +8522,61 @@ function attachEscapeListener(onEscape) {
8353
8522
  } catch {
8354
8523
  }
8355
8524
  let triggered = false;
8525
+ let escTimer = null;
8356
8526
  const triggerEscape = () => {
8357
8527
  if (triggered) return;
8358
8528
  triggered = true;
8529
+ if (escTimer) {
8530
+ clearTimeout(escTimer);
8531
+ escTimer = null;
8532
+ }
8359
8533
  onEscape();
8360
8534
  };
8361
8535
  const onData = (chunk) => {
8362
- if (isInteractivePromptActive()) return;
8536
+ if (isInteractivePromptActive()) {
8537
+ if (escTimer) {
8538
+ clearTimeout(escTimer);
8539
+ escTimer = null;
8540
+ }
8541
+ return;
8542
+ }
8363
8543
  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) {
8544
+ if (buf.length === 1 && buf[0] === 3) {
8367
8545
  process.exit(0);
8368
8546
  }
8547
+ if (wasInteractivePromptRecentlyActive(600)) return;
8548
+ if (buf.length === 1 && buf[0] === 27) {
8549
+ if (escTimer) {
8550
+ triggerEscape();
8551
+ return;
8552
+ }
8553
+ escTimer = setTimeout(() => {
8554
+ escTimer = null;
8555
+ if (!isInteractivePromptActive() && !wasInteractivePromptRecentlyActive(600)) {
8556
+ triggerEscape();
8557
+ }
8558
+ }, 60);
8559
+ } else if (escTimer) {
8560
+ clearTimeout(escTimer);
8561
+ escTimer = null;
8562
+ }
8369
8563
  };
8370
8564
  const keypressHandler = (str, key) => {
8371
- if (isInteractivePromptActive()) return;
8565
+ if (key && key.ctrl && key.name === "c") {
8566
+ process.exit(0);
8567
+ }
8568
+ if (isInteractivePromptActive() || wasInteractivePromptRecentlyActive(600)) return;
8372
8569
  if (key && key.name === "escape" || str === "\x1B" || str === "\x1B" || key && key.sequence === "\x1B") {
8373
8570
  triggerEscape();
8374
- } else if (key && key.ctrl && key.name === "c") {
8375
- process.exit(0);
8376
8571
  }
8377
8572
  };
8378
8573
  process.stdin.on("data", onData);
8379
8574
  process.stdin.on("keypress", keypressHandler);
8380
8575
  return () => {
8576
+ if (escTimer) {
8577
+ clearTimeout(escTimer);
8578
+ escTimer = null;
8579
+ }
8381
8580
  process.stdin.removeListener("data", onData);
8382
8581
  process.stdin.removeListener("keypress", keypressHandler);
8383
8582
  try {