2021-05-13 22:04:47 +00:00
|
|
|
import { BufferLocation, BufferSize, Char, Color } from "./base.ts";
|
2021-06-28 18:21:32 +00:00
|
|
|
import { readKeypress } from "./deps.ts";
|
2021-05-13 22:04:47 +00:00
|
|
|
import { Display } from "./display.ts";
|
2021-05-11 21:20:33 +00:00
|
|
|
|
2021-06-27 21:11:49 +00:00
|
|
|
export enum AnsiColorMode {
|
|
|
|
AUTODETECT,
|
|
|
|
COLORS256,
|
|
|
|
TRUECOLOR,
|
|
|
|
}
|
|
|
|
|
2021-05-11 21:20:33 +00:00
|
|
|
/**
|
|
|
|
* ANSI terminal display
|
|
|
|
*/
|
2021-08-26 18:12:36 +00:00
|
|
|
export class AnsiTerminalDisplay extends Display {
|
2021-06-24 22:41:34 +00:00
|
|
|
private palette_bg: readonly Uint8Array[] = [];
|
|
|
|
private palette_fg: readonly Uint8Array[] = [];
|
2021-06-24 23:09:05 +00:00
|
|
|
private width = 1;
|
|
|
|
private state = { x: -1, y: -1, f: -1, b: -1 }; // current location and color
|
2021-06-24 22:41:34 +00:00
|
|
|
|
2021-05-13 22:04:47 +00:00
|
|
|
constructor(
|
|
|
|
private writer: Deno.Writer = Deno.stdout,
|
2021-06-28 20:42:42 +00:00
|
|
|
reader: Deno.Reader = Deno.stdin,
|
2021-05-13 22:04:47 +00:00
|
|
|
) {
|
2021-08-26 18:12:36 +00:00
|
|
|
super();
|
|
|
|
|
2021-06-28 18:21:32 +00:00
|
|
|
if (hasRawMode(reader)) {
|
|
|
|
this.readKeyPresses(reader); // purposefully not awaited
|
|
|
|
}
|
2021-05-13 22:04:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 22:48:00 +00:00
|
|
|
async init(): Promise<void> {
|
|
|
|
await this.writer.write(CLEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
async uninit(): Promise<void> {
|
|
|
|
await this.writer.write(CLEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
async flush(): Promise<void> {
|
|
|
|
}
|
|
|
|
|
2021-05-13 22:04:47 +00:00
|
|
|
async getSize(): Promise<BufferSize> {
|
|
|
|
const size = Deno.consoleSize(Deno.stdout.rid);
|
2021-06-24 23:09:05 +00:00
|
|
|
this.width = size.columns;
|
2021-05-13 22:04:47 +00:00
|
|
|
return {
|
|
|
|
w: size.columns,
|
|
|
|
h: size.rows,
|
|
|
|
};
|
2021-05-11 21:20:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:11:49 +00:00
|
|
|
async setupPalette(
|
|
|
|
colors: readonly Color[],
|
|
|
|
mode = AnsiColorMode.AUTODETECT,
|
|
|
|
): Promise<readonly Color[]> {
|
|
|
|
if (mode == AnsiColorMode.AUTODETECT) {
|
|
|
|
const colorterm = Deno.env.get("COLORTERM");
|
|
|
|
if (colorterm?.search(/truecolor|24bit/)) {
|
|
|
|
mode = AnsiColorMode.TRUECOLOR;
|
|
|
|
} else {
|
|
|
|
mode = AnsiColorMode.COLORS256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == AnsiColorMode.TRUECOLOR) {
|
|
|
|
// True color is supported, use the request palette as-is
|
|
|
|
const cr = (x: number) => Math.round(x * 255);
|
|
|
|
this.palette_bg = colors.map((col) =>
|
|
|
|
escape(`[48;2;${cr(col.r)};${cr(col.g)};${cr(col.b)}m`)
|
|
|
|
);
|
|
|
|
this.palette_fg = colors.map((col) =>
|
|
|
|
escape(`[38;2;${cr(col.r)};${cr(col.g)};${cr(col.b)}m`)
|
|
|
|
);
|
|
|
|
return colors;
|
|
|
|
} else {
|
|
|
|
// True color not supported, fallback to 256-colors
|
|
|
|
const result = get256Colors();
|
|
|
|
this.palette_bg = result.map((_, idx) => escape(`[48;5;${idx}m`));
|
|
|
|
this.palette_fg = result.map((_, idx) => escape(`[38;5;${idx}m`));
|
|
|
|
return result;
|
|
|
|
}
|
2021-05-11 21:20:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 18:21:32 +00:00
|
|
|
async setCursorVisibility(visible: boolean): Promise<void> {
|
|
|
|
await this.writer.write(visible ? escape("[?25h") : escape("[?25l"));
|
|
|
|
}
|
|
|
|
|
2021-05-13 22:04:47 +00:00
|
|
|
async setChar(at: BufferLocation, char: Char): Promise<void> {
|
2021-06-24 23:09:05 +00:00
|
|
|
let { x, y, f, b } = this.state;
|
|
|
|
|
|
|
|
if (f != char.fg) {
|
|
|
|
f = char.fg;
|
2021-06-27 17:46:27 +00:00
|
|
|
const col = this.palette_fg[f];
|
|
|
|
if (col) {
|
|
|
|
await this.writer.write(col);
|
|
|
|
}
|
2021-06-24 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (b != char.bg) {
|
|
|
|
b = char.bg;
|
2021-06-27 17:46:27 +00:00
|
|
|
const col = this.palette_bg[b];
|
|
|
|
if (col) {
|
|
|
|
await this.writer.write(col);
|
|
|
|
}
|
2021-06-24 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (x != at.x || y != at.y) {
|
|
|
|
x = at.x;
|
|
|
|
y = at.y;
|
2021-06-27 17:46:27 +00:00
|
|
|
await this.writer.write(escape(`[${y + 1};${x + 1}H`));
|
2021-06-24 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.writer.write(new TextEncoder().encode(char.ch));
|
|
|
|
|
|
|
|
x += 1;
|
2021-06-27 17:46:27 +00:00
|
|
|
if (x >= this.width) {
|
2021-06-24 23:09:05 +00:00
|
|
|
x = 0;
|
|
|
|
y += 1;
|
|
|
|
}
|
|
|
|
this.state = { x, y, f, b };
|
2021-05-13 22:04:47 +00:00
|
|
|
}
|
2021-06-27 17:46:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Force the display size for subsequent prints
|
|
|
|
*/
|
|
|
|
forceSize(size: BufferSize) {
|
|
|
|
this.width = size.w;
|
|
|
|
}
|
2021-06-28 18:21:32 +00:00
|
|
|
|
|
|
|
private async readKeyPresses(reader: Deno.Reader & { rid: number }) {
|
|
|
|
for await (const keypress of readKeypress(reader)) {
|
|
|
|
let key = keypress.key;
|
|
|
|
if (key) {
|
|
|
|
if (keypress.shiftKey) {
|
|
|
|
key = "shift+" + (key.length == 1 ? key.toLocaleLowerCase() : key);
|
|
|
|
}
|
|
|
|
if (keypress.metaKey) {
|
|
|
|
key = "alt+" + key;
|
|
|
|
}
|
|
|
|
if (keypress.ctrlKey) {
|
|
|
|
key = "ctrl+" + key;
|
|
|
|
}
|
2021-08-26 18:12:36 +00:00
|
|
|
await this.pushEvent({ key });
|
2021-06-28 18:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 21:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function escape(sequence: string): Uint8Array {
|
|
|
|
return new Uint8Array([0x1B, ...new TextEncoder().encode(sequence)]);
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:11:49 +00:00
|
|
|
function get256Colors(): readonly Color[] {
|
|
|
|
const result: Color[] = [
|
|
|
|
{ r: 0, g: 0, b: 0 },
|
|
|
|
{ r: 0.5, g: 0, b: 0 },
|
|
|
|
{ r: 0, g: 0.5, b: 0 },
|
|
|
|
{ r: 0.5, g: 0.5, b: 0 },
|
|
|
|
{ r: 0, g: 0, b: 0.5 },
|
|
|
|
{ r: 0.5, g: 0, b: 0.5 },
|
|
|
|
{ r: 0, g: 0.5, b: 0.5 },
|
|
|
|
{ r: 0.75, g: 0.75, b: 0.75 },
|
|
|
|
{ r: 0.5, g: 0.5, b: 0.5 },
|
|
|
|
{ r: 1, g: 0, b: 0 },
|
|
|
|
{ r: 0, g: 1, b: 0 },
|
|
|
|
{ r: 1, g: 1, b: 0 },
|
|
|
|
{ r: 0, g: 0, b: 1 },
|
|
|
|
{ r: 1, g: 0, b: 1 },
|
|
|
|
{ r: 0, g: 1, b: 1 },
|
|
|
|
{ r: 1, g: 1, b: 1 },
|
|
|
|
];
|
|
|
|
for (let r = 0; r < 6; r++) {
|
|
|
|
for (let g = 0; g < 6; g++) {
|
|
|
|
for (let b = 0; b < 6; b++) {
|
|
|
|
result.push({ r: r / 5, g: g / 5, b: b / 5 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let l = 0; l < 24; l++) {
|
|
|
|
result.push({ r: (l + 1) / 25, g: (l + 1) / 25, b: (l + 1) / 25 });
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-11 21:20:33 +00:00
|
|
|
const CLEAR = escape("[2J");
|
2021-06-28 18:21:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a reader will be compatible with raw mode
|
|
|
|
*/
|
|
|
|
function hasRawMode(
|
|
|
|
reader: Deno.Reader,
|
|
|
|
): reader is Deno.Reader & { rid: number } {
|
|
|
|
return typeof (<any> reader).rid == "number";
|
|
|
|
}
|