swc/crates/swc_bundler/tests/.cache/deno/0889b0a101f4e90270d90a7834853dcb46bf71bc.ts
2021-11-09 20:42:49 +09:00

44 lines
1.1 KiB
TypeScript

// Loaded from https://deno.land/x/cliffy@v0.18.0/ansi/cursor_position.ts
import { cursorPosition } from "./ansi_escapes.ts";
/** Cursor position. */
export interface Cursor {
x: number;
y: number;
}
/** Cursor position options. */
export interface CursorPositionOptions {
stdout?: Deno.WriterSync;
stdin?: Deno.ReaderSync & { rid: number };
}
/**
* Get cursor position.
* @param options Options.
* ```
* const cursor: Cursor = getCursorPosition();
* console.log(cursor); // { x: 0, y: 14}
* ```
*/
export function getCursorPosition(options: CursorPositionOptions): Cursor {
const stdout: Deno.WriterSync = options?.stdout ?? Deno.stdout;
const stdin: Deno.ReaderSync & { rid: number } = options?.stdin ?? Deno.stdin;
const data = new Uint8Array(8);
Deno.setRaw(stdin.rid, true);
stdout.writeSync(new TextEncoder().encode(cursorPosition));
stdin.readSync(data);
Deno.setRaw(stdin.rid, false);
const [y, x] = new TextDecoder()
.decode(data)
.match(/\[(\d+);(\d+)R/)
?.slice(1, 3)
.map(Number) ?? [0, 0];
return { x, y };
}