2021-05-19 13:00:52 +00:00
|
|
|
import { AnsiTerminalDisplay } from "./ansi.ts";
|
2021-06-28 18:21:32 +00:00
|
|
|
import { UIConfig } from "./config.ts";
|
|
|
|
import { Display } from "./display.ts";
|
|
|
|
import { TextUI } from "./ui.ts";
|
2021-08-29 21:56:46 +00:00
|
|
|
import { CanvasTerminalDisplay, DivTerminalDisplay } from "./web.ts";
|
2021-07-19 22:48:00 +00:00
|
|
|
|
|
|
|
export { TextUI };
|
2021-05-19 13:00:52 +00:00
|
|
|
|
2021-06-28 18:21:32 +00:00
|
|
|
export const UI_DISPLAY_TYPES = {
|
|
|
|
autodetect: undefined,
|
|
|
|
ansi: AnsiTerminalDisplay,
|
2021-07-19 22:48:00 +00:00
|
|
|
web_div: DivTerminalDisplay,
|
|
|
|
web_canvas: CanvasTerminalDisplay,
|
2021-06-28 18:21:32 +00:00
|
|
|
dummy: Display,
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
export async function createTextUI(
|
|
|
|
config: Partial<UIConfig>,
|
|
|
|
display_type: keyof typeof UI_DISPLAY_TYPES = "autodetect",
|
|
|
|
): Promise<TextUI> {
|
|
|
|
if (display_type == "autodetect") {
|
2021-03-05 23:02:04 +00:00
|
|
|
if (typeof (window as any).document != "undefined") {
|
2021-07-19 22:48:00 +00:00
|
|
|
display_type = "web_canvas";
|
2021-08-29 21:56:46 +00:00
|
|
|
// TODO if canvas is not available, fall back to div
|
|
|
|
} else if (typeof (Deno as any) != "undefined") {
|
2021-03-05 23:02:04 +00:00
|
|
|
display_type = "ansi";
|
2021-08-29 21:56:46 +00:00
|
|
|
} else {
|
|
|
|
const message = "Cannot initialize display";
|
|
|
|
if (typeof alert == "function") {
|
|
|
|
alert(message);
|
|
|
|
}
|
|
|
|
throw new Error(message);
|
2021-03-05 23:02:04 +00:00
|
|
|
}
|
2021-06-28 18:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var display = new UI_DISPLAY_TYPES[display_type]();
|
|
|
|
|
|
|
|
var ui = new TextUI(display, config);
|
|
|
|
await ui.init();
|
2021-06-24 23:09:05 +00:00
|
|
|
|
2021-05-19 13:00:52 +00:00
|
|
|
return ui;
|
|
|
|
}
|