| 24 | * ``` |
| 25 | */ |
| 26 | export function askForCursorPositionSync(): |
| 27 | | { row: number; column: number } |
| 28 | | null { |
| 29 | if (!Deno.stdin.isTerminal() || !Deno.stderr.isTerminal()) { |
| 30 | return null; |
| 31 | } |
| 32 | Deno.stdin.setRaw(true, { cbreak: true }); |
| 33 | Deno.stderr.writeSync(Uint8Array.from([0x1b, 0x5b, 0x36, 0x6e])); // \x1b[6n |
| 34 | const buffer = new Uint8Array(1024); |
| 35 | while (true) { |
| 36 | const x = Deno.stdin.readSync(buffer.subarray(32)); |
| 37 | const csi = findCSI(buffer.subarray(0, 32 + x!)); |
| 38 | if (csi?.final === 82) { // R |
| 39 | Deno.stdin.setRaw(false); |
| 40 | const [row, column] = new TextDecoder() |
| 41 | .decode(csi.parameters) |
| 42 | .split(";") |
| 43 | .map((x) => parseInt(x)); |
| 44 | return { row: row!, column: column! }; |
| 45 | } |
| 46 | buffer.set(buffer.subarray(buffer.length - 32)); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | function findCSI( |
| 51 | buffer: Uint8Array, |