scroll/src/common/buffer.ts

37 lines
626 B
JavaScript
Raw Normal View History

2023-11-29 16:09:58 -05:00
import { strlen, truncate } from './fns.ts';
2024-07-12 10:58:23 -04:00
import { getRuntime } from './runtime/mod.ts';
2023-11-13 15:33:56 -05:00
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 {
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() {
const { term } = await getRuntime();
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;