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