scroll/src/common/editor.ts

73 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-11-08 11:11:19 -05:00
import { Ansi } from './ansi.ts';
import { importForRuntime } from './runtime.ts';
import { ctrl_key } from './strings.ts';
class Buffer {
#b = '';
constructor() {
}
append(s: string): void {
this.#b += s;
}
appendLine(s: string): void {
this.#b += s + '\r\n';
}
2023-11-08 11:11:19 -05:00
clear(): void {
this.#b = '';
}
getBuffer(): string {
return this.#b;
}
}
export class Editor {
2023-11-08 11:11:19 -05:00
#buffer: Buffer;
constructor() {
2023-11-08 11:11:19 -05:00
this.#buffer = new Buffer();
}
/**
* Determine what to do based on input
* @param input - the decoded chunk of stdin
*/
public processKeyPress(input: string): boolean {
switch (input) {
case ctrl_key('q'):
this.clearScreen();
return false;
default:
return true;
}
}
/**
* Clear the screen and write out the buffer
*/
public async refreshScreen(): Promise<void> {
const { write } = await importForRuntime('terminal_io');
this.clearScreen();
this.drawRows();
2023-11-08 11:11:19 -05:00
await write(this.#buffer.getBuffer());
this.#buffer.clear();
}
2023-11-08 11:11:19 -05:00
private drawRows(): void {
for (let y = 0; y <= 24; y++) {
this.#buffer.appendLine('~');
}
}
2023-11-08 11:11:19 -05:00
private clearScreen(): void {
this.#buffer.append(Ansi.ClearScreen);
this.#buffer.append(Ansi.ResetCursor);
}
}