Add refresh loop

This commit is contained in:
Michaël Lemaire 2021-05-19 15:00:52 +02:00
parent 55238b5065
commit c9292c0199
2 changed files with 29 additions and 1 deletions

11
mod.ts Normal file
View file

@ -0,0 +1,11 @@
import { AnsiTerminalDisplay } from "./ansi.ts";
import { TextUI } from "./ui.ts";
export { TextUI } from "./ui.ts";
export async function createTextUI(): Promise<TextUI> {
// TODO detect platform
var display = new AnsiTerminalDisplay();
var ui = new TextUI(display);
await ui.init();
return ui;
}

19
ui.ts
View file

@ -1,4 +1,4 @@
import { BufferDrawing, CharBuffer } from "./base.ts";
import { BufferDrawing, BufferSize, CharBuffer } from "./base.ts";
import { Display } from "./display.ts";
/**
@ -23,6 +23,13 @@ export class TextUI {
await this.display.clear();
}
/**
* Get the current display size
*/
getSize(): BufferSize {
return this.screen.getSize();
}
/**
* Flush the internal buffer to the display
*/
@ -35,4 +42,14 @@ export class TextUI {
}
}
}
/**
* Start the event loop, waiting for input
*/
async loop(refresh = 1000): Promise<void> {
while (true) {
await this.flush();
await new Promise((resolve) => setTimeout(resolve, refresh));
}
}
}