(_str: string, key: { name?: string; ctrl?: boolean } | undefined)
| 170 | }; |
| 171 | |
| 172 | const onKey = (_str: string, key: { name?: string; ctrl?: boolean } | undefined): void => { |
| 173 | if (!key) return; |
| 174 | if (key.ctrl && key.name === 'c') { |
| 175 | if (numTimer) clearTimeout(numTimer); |
| 176 | cleanup(); |
| 177 | stdout.write('\n'); |
| 178 | process.exit(130); // SIGINT — user cancelled setup |
| 179 | } else if (key.name === 'return' || key.name === 'enter') { |
| 180 | if (numTimer) clearTimeout(numTimer); |
| 181 | commitNumBuf(); // a pending number jump lands on that row first |
| 182 | cleanup(); |
| 183 | resolve(options[idx]!.value); |
| 184 | } else if (key.name === 'escape') { |
| 185 | if (numTimer) clearTimeout(numTimer); |
| 186 | cleanup(); |
| 187 | resolve(defaultValue); |
| 188 | } else if (key.name && /^[0-9]$/.test(key.name)) { |
| 189 | // Build up a (possibly multi-digit) number; move the cursor there. Enter confirms. |
| 190 | // A short idle delay auto-moves so single-digit jumps still feel instant. |
| 191 | numBuf += key.name; |
| 192 | if (numTimer) clearTimeout(numTimer); |
| 193 | const asNum = parseInt(numBuf, 10); |
| 194 | // If no longer number could extend this into a valid index, commit immediately. |
| 195 | const couldGrow = asNum * 10 <= options.length; |
| 196 | if (!couldGrow) { commitNumBuf(); } |
| 197 | else { |
| 198 | if (asNum >= 1 && asNum <= options.length) { idx = asNum - 1; render(); } |
| 199 | numTimer = setTimeout(() => { numBuf = ''; }, 600); |
| 200 | } |
| 201 | } else { |
| 202 | const next = moveSelection(idx, key.name, options.length); |
| 203 | if (next !== idx) { idx = next; render(); } |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | readline.emitKeypressEvents(stdin); |
| 208 | if (stdin.isTTY) (stdin as any).setRawMode(true); |
nothing calls this directly
no test coverage detected