()
| 29 | * Only captures if stdin is a TTY (interactive terminal). |
| 30 | */ |
| 31 | export function startCapturingEarlyInput(): void { |
| 32 | // Only capture in interactive mode: stdin must be a TTY, and we must not |
| 33 | // be in print mode. Raw mode disables ISIG (terminal Ctrl+C → SIGINT), |
| 34 | // which would make -p uninterruptible. |
| 35 | if ( |
| 36 | !process.stdin.isTTY || |
| 37 | isCapturing || |
| 38 | process.argv.includes('-p') || |
| 39 | process.argv.includes('--print') |
| 40 | ) { |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | isCapturing = true |
| 45 | earlyInputBuffer = '' |
| 46 | |
| 47 | // Set stdin to raw mode and use 'readable' event like Ink does |
| 48 | // This ensures compatibility with how the REPL will handle stdin later |
| 49 | try { |
| 50 | process.stdin.setEncoding('utf8') |
| 51 | process.stdin.setRawMode(true) |
| 52 | process.stdin.ref() |
| 53 | |
| 54 | readableHandler = () => { |
| 55 | let chunk = process.stdin.read() |
| 56 | while (chunk !== null) { |
| 57 | if (typeof chunk === 'string') { |
| 58 | processChunk(chunk) |
| 59 | } |
| 60 | chunk = process.stdin.read() |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | process.stdin.on('readable', readableHandler) |
| 65 | } catch { |
| 66 | // If we can't set raw mode, just silently continue without early capture |
| 67 | isCapturing = false |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Process a chunk of input data |
no test coverage detected