({
input = stdin,
output = stdout,
overwrite = true,
hideCursor = true,
}: BlockOptions = {})
| 32 | } |
| 33 | |
| 34 | export function block({ |
| 35 | input = stdin, |
| 36 | output = stdout, |
| 37 | overwrite = true, |
| 38 | hideCursor = true, |
| 39 | }: BlockOptions = {}) { |
| 40 | const rl = readline.createInterface({ |
| 41 | input, |
| 42 | output, |
| 43 | prompt: '', |
| 44 | tabSize: 1, |
| 45 | }); |
| 46 | readline.emitKeypressEvents(input, rl); |
| 47 | |
| 48 | if (input instanceof ReadStream && input.isTTY) { |
| 49 | input.setRawMode(true); |
| 50 | } |
| 51 | |
| 52 | const clear = (data: Buffer, { name, sequence }: Key) => { |
| 53 | const str = String(data); |
| 54 | if (isActionKey([str, name, sequence], 'cancel')) { |
| 55 | if (hideCursor) output.write(cursor.show); |
| 56 | process.exit(0); |
| 57 | return; |
| 58 | } |
| 59 | if (!overwrite) return; |
| 60 | const dx = name === 'return' ? 0 : -1; |
| 61 | const dy = name === 'return' ? -1 : 0; |
| 62 | |
| 63 | readline.moveCursor(output, dx, dy, () => { |
| 64 | readline.clearLine(output, 1, () => { |
| 65 | input.once('keypress', clear); |
| 66 | }); |
| 67 | }); |
| 68 | }; |
| 69 | if (hideCursor) output.write(cursor.hide); |
| 70 | input.once('keypress', clear); |
| 71 | |
| 72 | return () => { |
| 73 | input.off('keypress', clear); |
| 74 | if (hideCursor) output.write(cursor.show); |
| 75 | |
| 76 | // Prevent Windows specific issues: https://github.com/bombshell-dev/clack/issues/176 |
| 77 | if (input instanceof ReadStream && input.isTTY && !isWindows) { |
| 78 | input.setRawMode(false); |
| 79 | } |
| 80 | |
| 81 | // @ts-expect-error fix for https://github.com/nodejs/node/issues/31762#issuecomment-1441223907 |
| 82 | rl.terminal = false; |
| 83 | rl.close(); |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | export const getColumns = (output: Writable): number => { |
| 88 | if ('columns' in output && typeof output.columns === 'number') { |
no test coverage detected