textui/web.ts

62 lines
1.5 KiB
TypeScript

import { BufferLocation, BufferSize, Char, Color } from "./base.ts";
import { Display } from "./display.ts";
/**
* Base for all web-based terminal displays
*/
class WebDisplay implements Display {
readonly document = (window as any).document;
async getSize(): Promise<BufferSize> {
return { w: 40, h: 20 };
}
async setupPalette(colors: readonly Color[]): Promise<readonly Color[]> {
return [];
}
async clear(): Promise<void> {
}
async setCursorVisibility(visible: boolean): Promise<void> {
}
async setChar(at: BufferLocation, char: Char): Promise<void> {
}
async getKeyStrokes(): Promise<string[]> {
return [];
}
}
/**
* Basic terminal display using a single "pre" tag
*/
export class PreTerminalDisplay extends WebDisplay {
element: any;
async clear(): Promise<void> {
if (!this.element) {
this.element = this.document.createElement("pre");
this.document.body.appendChild(this.element);
}
const { w, h } = await this.getSize();
const line = Array(w).fill(" ").join("");
this.element.textContent = Array(h).fill(line).join("\n");
}
async setChar(at: BufferLocation, char: Char): Promise<void> {
const { w, h } = await this.getSize();
const offset = at.y * (w + 1) + at.x;
const text = this.element.textContent;
this.element.textContent = text.slice(0, offset) + char.ch +
text.slice(offset + 1);
}
}
/**
* DOM terminal display, using one div per char
*/
export class DOMTerminalDisplay extends WebDisplay {
}