scroll/src/common/editor/editor.ts

82 lines
1.9 KiB
JavaScript

import Ansi from './ansi.ts';
import Buffer from './buffer.ts';
import {
ctrl_key,
importDefaultForRuntime,
ITerminalSize,
truncate,
VERSION,
} from '../mod.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().then(() => {});
return false;
default:
return true;
}
}
// -------------------------------------------------------------------------------------------------------------------
// Terminal Output / Drawing
// -------------------------------------------------------------------------------------------------------------------
/**
* Clear the screen and write out the buffer
*/
public async refreshScreen(): Promise<void> {
this.#buffer.append(Ansi.HideCursor);
this.#buffer.append(Ansi.ResetCursor);
this.drawRows();
this.#buffer.append(Ansi.ShowCursor);
await this.writeToScreen();
}
private async clearScreen(): Promise<void> {
this.#buffer.append(Ansi.ClearScreen);
this.#buffer.append(Ansi.ResetCursor);
await this.writeToScreen();
}
private drawRows(): void {
for (let y = 0; y < this.#screenRows; y++) {
if (y === this.#screenRows / 3) {
const message = `Kilo editor -- version ${VERSION}`;
this.#buffer.append(truncate(message, this.#screenCols));
} else {
this.#buffer.append('~');
}
this.#buffer.append(Ansi.ClearLine);
if (y < this.#screenRows - 1) {
this.#buffer.appendLine('');
}
}
}
private async writeToScreen(): Promise<void> {
const io = await importDefaultForRuntime('terminal_io');
await io.write(this.#buffer.getBuffer());
this.#buffer.clear();
}
}