37 lines
657 B
JavaScript
37 lines
657 B
JavaScript
import { strlen, truncate } from './utils.ts';
|
|
import { getRuntime, importForRuntime } from './runtime.ts';
|
|
|
|
class Buffer {
|
|
#b = '';
|
|
|
|
constructor() {
|
|
}
|
|
|
|
public append(s: string, maxLen?: number): void {
|
|
this.#b += (maxLen === undefined) ? s : truncate(s, maxLen);
|
|
}
|
|
|
|
public appendLine(s = ''): void {
|
|
this.#b += s + '\r\n';
|
|
}
|
|
|
|
public clear(): void {
|
|
this.#b = '';
|
|
}
|
|
|
|
/**
|
|
* Output the contents of the buffer into stdout
|
|
*/
|
|
public async flush() {
|
|
const term = await importForRuntime('terminal_io');
|
|
await term.writeStdout(this.#b);
|
|
this.clear();
|
|
}
|
|
|
|
public strlen(): number {
|
|
return strlen(this.#b);
|
|
}
|
|
}
|
|
|
|
export default Buffer;
|