@@ -112,6 +112,11 @@ function runBrowserCommand(action: () => Promise<void>) {
|
112 | 112 | }); |
113 | 113 | } |
114 | 114 | |
| 115 | +function parseTabIndex(value: string): number { |
| 116 | +const trimmed = value.trim(); |
| 117 | +return /^\d+$/.test(trimmed) ? Number(trimmed) : Number.NaN; |
| 118 | +} |
| 119 | + |
115 | 120 | function logBrowserTabs(tabs: BrowserTab[], json?: boolean) { |
116 | 121 | if (json) { |
117 | 122 | defaultRuntime.writeJson({ tabs }); |
@@ -490,41 +495,40 @@ export function registerBrowserManageCommands(
|
490 | 495 | tab |
491 | 496 | .command("select") |
492 | 497 | .description("Focus tab by index (1-based)") |
493 | | -.argument("<index>", "Tab index (1-based)", (v: string) => Number(v)) |
| 498 | +.argument("<index>", "Tab index (1-based)", parseTabIndex) |
494 | 499 | .action(async (index: number, _opts, cmd) => { |
495 | 500 | const parent = parentOpts(cmd); |
496 | 501 | const profile = parent?.browserProfile; |
497 | | -if (!Number.isFinite(index) || index < 1) { |
498 | | -defaultRuntime.error(danger("index must be a positive number")); |
| 502 | +if (!Number.isSafeInteger(index) || index < 1) { |
| 503 | +defaultRuntime.error(danger("index must be a positive integer")); |
499 | 504 | defaultRuntime.exit(1); |
500 | 505 | return; |
501 | 506 | } |
502 | 507 | await runBrowserCommand(async () => { |
503 | 508 | const result = await callTabAction(parent, profile, { |
504 | 509 | action: "select", |
505 | | -index: Math.floor(index) - 1, |
| 510 | +index: index - 1, |
506 | 511 | }); |
507 | 512 | if (printJsonResult(parent, result)) { |
508 | 513 | return; |
509 | 514 | } |
510 | | -defaultRuntime.log(`selected tab ${Math.floor(index)}`); |
| 515 | +defaultRuntime.log(`selected tab ${index}`); |
511 | 516 | }); |
512 | 517 | }); |
513 | 518 | |
514 | 519 | tab |
515 | 520 | .command("close") |
516 | 521 | .description("Close tab by index (1-based); default: first tab") |
517 | | -.argument("[index]", "Tab index (1-based)", (v: string) => Number(v)) |
| 522 | +.argument("[index]", "Tab index (1-based)", parseTabIndex) |
518 | 523 | .action(async (index: number | undefined, _opts, cmd) => { |
519 | 524 | const parent = parentOpts(cmd); |
520 | 525 | const profile = parent?.browserProfile; |
521 | | -const idx = |
522 | | -typeof index === "number" && Number.isFinite(index) ? Math.floor(index) - 1 : undefined; |
523 | | -if (typeof idx === "number" && idx < 0) { |
524 | | -defaultRuntime.error(danger("index must be >= 1")); |
| 526 | +if (typeof index === "number" && (!Number.isSafeInteger(index) || index < 1)) { |
| 527 | +defaultRuntime.error(danger("index must be a positive integer")); |
525 | 528 | defaultRuntime.exit(1); |
526 | 529 | return; |
527 | 530 | } |
| 531 | +const idx = typeof index === "number" ? index - 1 : undefined; |
528 | 532 | await runBrowserCommand(async () => { |
529 | 533 | const result = await callTabAction(parent, profile, { action: "close", index: idx }); |
530 | 534 | if (printJsonResult(parent, result)) { |
|