textui/src/demo.ts

35 lines
918 B
TypeScript
Raw Normal View History

2021-09-06 22:35:15 +00:00
import { UIConfig } from "./config.ts";
import { createTextUI, UI_DISPLAY_TYPES } from "./main.ts";
2021-07-19 22:48:00 +00:00
2021-09-06 22:35:15 +00:00
export async function runUIDemo(
display_type: keyof typeof UI_DISPLAY_TYPES = "autodetect",
): Promise<void> {
2021-08-26 18:12:36 +00:00
let x = 0;
2021-09-06 22:35:15 +00:00
const config: Partial<UIConfig> = {
2021-07-19 22:48:00 +00:00
palette: [
{ r: 0, g: 0, b: 0 },
{ r: 1, g: 1, b: 1 },
{ r: 0, g: 1, b: 1 },
],
onResize: draw,
2021-08-26 18:12:36 +00:00
onKeyStroke: (key) => {
ui.drawing.color(1, 0).text(key, { x, y: 7 });
x += key.length + 1;
},
onMouseClick: (loc) => {
const text = `${loc.x}:${loc.y}`;
ui.drawing.color(1, 0).text(text, { x, y: 7 });
x += text.length + 1;
},
2021-09-06 22:35:15 +00:00
};
const ui = await createTextUI(config, display_type);
await ui.init();
draw();
await ui.loop();
2021-07-19 22:48:00 +00:00
function draw() {
ui.drawing.color(2, 0).text("hello", { x: 10, y: 3 });
ui.drawing.color(0, 1).text("world", { x: 10, y: 5 });
}
}