import Ansi from './ansi.ts'; import Buffer from './buffer.ts'; import { importDefaultForRuntime } from '../runtime.ts'; import { ctrl_key } from '../strings.ts'; import { ITerminalSize } from '../types.ts'; export class Editor { #buffer: Buffer; #screenRows: number; #screenCols: number; constructor(terminalSize: ITerminalSize) { this.#buffer = new Buffer(); this.#screenRows = terminalSize.rows; this.#screenCols = terminalSize.cols; } /** * 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 { const { write } = await importDefaultForRuntime('terminal_io'); this.clearScreen(); this.drawRows(); await write(this.#buffer.getBuffer()); this.#buffer.clear(); } private drawRows(): void { for (let y = 0; y <= this.#screenRows; y++) { this.#buffer.appendLine('~'); } } private clearScreen(): void { this.#buffer.append(Ansi.ClearScreen); this.#buffer.append(Ansi.ResetCursor); } }