scroll/src/common/editor/editor.ts

164 lines
3.7 KiB
JavaScript
Raw Normal View History

import Ansi, { KeyCommand } from './ansi.ts';
2023-11-08 18:07:34 -05:00
import Buffer from './buffer.ts';
2023-11-13 14:46:04 -05:00
import Document from './document.ts';
import { ctrl_key, IPoint, 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;
2023-11-10 08:36:18 -05:00
#screen: ITerminalSize;
#cursor: IPoint;
2023-11-13 14:46:04 -05:00
#document: Document;
constructor(terminalSize: ITerminalSize) {
2023-11-08 11:11:19 -05:00
this.#buffer = new Buffer();
2023-11-10 08:36:18 -05:00
this.#screen = terminalSize;
this.#cursor = {
x: 0,
y: 0,
};
2023-11-13 14:46:04 -05:00
this.#document = Document.open();
2023-11-08 11:11:19 -05:00
}
2023-11-10 08:36:18 -05:00
// --------------------------------------------------------------------------
// Command/input mapping
// --------------------------------------------------------------------------
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;
case KeyCommand.Home:
this.#cursor.x = 0;
break;
case KeyCommand.End:
this.#cursor.x = this.#screen.cols - 1;
break;
case KeyCommand.PageUp:
case KeyCommand.PageDown:
{
let times = this.#screen.rows;
while (times--) {
this.moveCursor(
input === KeyCommand.PageUp
? KeyCommand.ArrowUp
: KeyCommand.ArrowDown,
);
}
}
break;
case KeyCommand.ArrowUp:
case KeyCommand.ArrowDown:
case KeyCommand.ArrowRight:
case KeyCommand.ArrowLeft:
this.moveCursor(input);
break;
2023-11-10 08:36:18 -05:00
}
return true;
}
private moveCursor(char: string): void {
switch (char) {
case KeyCommand.ArrowLeft:
if (this.#cursor.x > 0) {
this.#cursor.x--;
}
2023-11-10 08:36:18 -05:00
break;
case KeyCommand.ArrowRight:
if (this.#cursor.x < this.#screen.cols) {
this.#cursor.x++;
}
2023-11-10 08:36:18 -05:00
break;
case KeyCommand.ArrowUp:
if (this.#cursor.y > 0) {
this.#cursor.y--;
}
2023-11-10 08:36:18 -05:00
break;
case KeyCommand.ArrowDown:
if (this.#cursor.y < this.#screen.rows) {
this.#cursor.y++;
}
2023-11-10 08:36:18 -05:00
break;
2023-11-08 11:11:19 -05:00
}
}
2023-11-10 08:36:18 -05:00
// --------------------------------------------------------------------------
2023-11-09 12:05:30 -05:00
// Terminal Output / Drawing
2023-11-10 08:36:18 -05:00
// --------------------------------------------------------------------------
2023-11-09 12:05:30 -05:00
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-10 08:36:18 -05:00
this.#buffer.append(
Ansi.moveCursor(this.#cursor.y, this.#cursor.x),
2023-11-10 08:36:18 -05:00
);
2023-11-09 12:05:30 -05:00
this.#buffer.append(Ansi.ShowCursor);
await this.#buffer.flush();
}
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.#buffer.flush();
}
2023-11-09 12:05:30 -05:00
private drawRows(): void {
2023-11-13 14:46:04 -05:00
for (let y = 0; y < this.#screen.rows; y++) {
if (y >= this.#document.numrows) {
this.drawPlaceholderRow(y);
} else {
this.drawFileRow(y);
}
}
}
2023-11-13 14:46:04 -05:00
private drawFileRow(_y: number): void {
const row = this.#document.getRow(0);
let len = row?.chars.length ?? 0;
if (len > this.#screen.cols) {
len = this.#screen.cols;
}
2023-11-13 14:46:04 -05:00
this.#buffer.appendLine(truncate(row!.toString(), len));
}
private drawPlaceholderRow(y: number): void {
if (y === Math.trunc(this.#screen.rows / 2)) {
const message = `Kilo editor -- version ${VERSION}`;
const messageLen = (message.length > this.#screen.cols)
? this.#screen.cols
: message.length;
let padding = Math.trunc((this.#screen.cols - messageLen) / 2);
if (padding > 0) {
2023-11-09 12:05:30 -05:00
this.#buffer.append('~');
2023-11-13 14:46:04 -05:00
padding -= 1;
2023-11-09 12:05:30 -05:00
2023-11-13 14:46:04 -05:00
this.#buffer.append(' '.repeat(padding));
2023-11-09 12:05:30 -05:00
}
2023-11-13 14:46:04 -05:00
this.#buffer.append(truncate(message, messageLen));
} else {
this.#buffer.append('~');
}
this.#buffer.append(Ansi.ClearLine);
if (y < this.#screen.rows - 1) {
this.#buffer.appendLine('');
2023-11-09 12:05:30 -05:00
}
}
}