scroll/src/common/buffer.ts

37 lines
632 B
JavaScript
Raw Normal View History

import { strlen, truncate } from './utils.ts';
2023-11-13 15:33:56 -05:00
import { getRuntime } from './runtime.ts';
class Buffer {
#b = '';
constructor() {
}
public append(s: string, maxLen?: number): void {
this.#b += (maxLen === undefined) ? s : truncate(s, maxLen);
2023-11-13 15:33:56 -05:00
}
public appendLine(s = ''): void {
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() {
const { term } = await getRuntime();
await term.writeStdout(this.#b);
this.clear();
}
public strlen(): number {
return strlen(this.#b);
}
}
export default Buffer;