2023-11-14 15:53:45 -05:00
|
|
|
import { strlen, truncate } from './utils.ts';
|
2023-11-16 16:00:03 -05:00
|
|
|
import { getRuntime, importForRuntime } from './runtime.ts';
|
2023-11-13 15:33:56 -05:00
|
|
|
|
|
|
|
class Buffer {
|
|
|
|
#b = '';
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
public append(s: string, maxLen?: number): void {
|
|
|
|
this.#b += (maxLen === undefined) ? s : truncate(s, maxLen);
|
2023-11-13 15:33:56 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
public appendLine(s = ''): void {
|
2023-11-16 16:00:03 -05:00
|
|
|
this.#b += s + '\r\n';
|
2023-11-13 15:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public clear(): void {
|
|
|
|
this.#b = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the contents of the buffer into stdout
|
|
|
|
*/
|
|
|
|
public async flush() {
|
2023-11-16 16:00:03 -05:00
|
|
|
const term = await importForRuntime('terminal_io');
|
2023-11-13 15:33:56 -05:00
|
|
|
await term.writeStdout(this.#b);
|
|
|
|
this.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public strlen(): number {
|
|
|
|
return strlen(this.#b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Buffer;
|