textui/ansi.ts

41 lines
962 B
TypeScript
Raw Normal View History

2021-05-13 22:04:47 +00:00
import { BufferLocation, BufferSize, Char, Color } from "./base.ts";
import { Display } from "./display.ts";
2021-05-11 21:20:33 +00:00
/**
* ANSI terminal display
*/
export class AnsiTerminalDisplay implements Display {
2021-05-13 22:04:47 +00:00
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,
};
2021-05-11 21:20:33 +00:00
}
async setupPalette(colors: readonly Color[]): Promise<readonly Color[]> {
return colors;
}
async clear(): Promise<void> {
await this.writer.write(CLEAR);
}
2021-05-13 22:04:47 +00:00
async setChar(at: BufferLocation, char: Char): Promise<void> {
// TODO colors
await this.writer.write(escape(`[${at.y};${at.x}H${char.ch}`));
}
2021-05-11 21:20:33 +00:00
}
function escape(sequence: string): Uint8Array {
return new Uint8Array([0x1B, ...new TextEncoder().encode(sequence)]);
}
const CLEAR = escape("[2J");