scroll/src/common/editor/editor.ts

95 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-11-08 18:07:34 -05:00
import Ansi from './ansi.ts';
import Buffer from './buffer.ts';
2023-11-09 12:05:30 -05:00
import {
ctrl_key,
importDefaultForRuntime,
ITerminalSize,
truncate,
VERSION,
} from '../mod.ts';
2023-11-08 11:11:19 -05:00
export class Editor {
2023-11-08 11:11:19 -05:00
#buffer: Buffer;
#screenRows: number;
#screenCols: number;
constructor(terminalSize: ITerminalSize) {
2023-11-08 11:11:19 -05:00
this.#buffer = new Buffer();
this.#screenRows = terminalSize.rows;
this.#screenCols = terminalSize.cols;
2023-11-08 11:11:19 -05:00
}
/**
* 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'):
2023-11-09 12:32:41 -05:00
this.clearScreen().then(() => {});
2023-11-08 11:11:19 -05:00
return false;
default:
return true;
}
}
2023-11-09 12:05:30 -05:00
// -------------------------------------------------------------------------------------------------------------------
// Terminal Output / Drawing
// -------------------------------------------------------------------------------------------------------------------
2023-11-08 11:11:19 -05:00
/**
* Clear the screen and write out the buffer
*/
public async refreshScreen(): Promise<void> {
2023-11-09 12:05:30 -05:00
this.#buffer.append(Ansi.HideCursor);
this.#buffer.append(Ansi.ResetCursor);
this.drawRows();
2023-11-09 12:05:30 -05:00
this.#buffer.append(Ansi.ShowCursor);
2023-11-09 12:32:41 -05:00
await this.writeToScreen();
}
2023-11-08 11:11:19 -05:00
2023-11-09 12:32:41 -05:00
private async clearScreen(): Promise<void> {
this.#buffer.append(Ansi.ClearScreen);
this.#buffer.append(Ansi.ResetCursor);
await this.writeToScreen();
}
2023-11-09 12:05:30 -05:00
private drawRows(): void {
this.drawPlaceholderRows();
}
private drawPlaceholderRows(): void {
2023-11-09 12:05:30 -05:00
for (let y = 0; y < this.#screenRows; y++) {
if (y === Math.trunc(this.#screenRows / 2)) {
2023-11-09 12:05:30 -05:00
const message = `Kilo editor -- version ${VERSION}`;
const messageLen = (message.length > this.#screenCols) ? this.#screenCols : message.length;
let padding = Math.trunc((this.#screenCols - messageLen) / 2);
if (padding > 0) {
this.#buffer.append('~');
padding -= 1;
this.#buffer.append(' '.repeat(padding));
}
this.#buffer.append(truncate(message, messageLen));
2023-11-09 12:05:30 -05:00
} else {
this.#buffer.append('~');
}
this.#buffer.append(Ansi.ClearLine);
if (y < this.#screenRows - 1) {
2023-11-09 12:32:41 -05:00
this.#buffer.appendLine('');
2023-11-09 12:05:30 -05:00
}
}
}
2023-11-09 12:32:41 -05:00
private async writeToScreen(): Promise<void> {
const io = await importDefaultForRuntime('terminal_io');
await io.write(this.#buffer.getBuffer());
this.#buffer.clear();
}
}