textui/ansi.ts

40 lines
962 B
TypeScript

import { BufferLocation, BufferSize, Char, Color } from "./base.ts";
import { Display } from "./display.ts";
/**
* ANSI terminal display
*/
export class AnsiTerminalDisplay implements Display {
constructor(
private writer: Deno.Writer = Deno.stdout,
private reader: Deno.Reader = Deno.stdin,
) {
}
async getSize(): Promise<BufferSize> {
const size = Deno.consoleSize(Deno.stdout.rid);
return {
w: size.columns,
h: size.rows,
};
}
async setupPalette(colors: readonly Color[]): Promise<readonly Color[]> {
return colors;
}
async clear(): Promise<void> {
await this.writer.write(CLEAR);
}
async setChar(at: BufferLocation, char: Char): Promise<void> {
// TODO colors
await this.writer.write(escape(`[${at.y};${at.x}H${char.ch}`));
}
}
function escape(sequence: string): Uint8Array {
return new Uint8Array([0x1B, ...new TextEncoder().encode(sequence)]);
}
const CLEAR = escape("[2J");