2023-11-08 18:07:34 -05:00
|
|
|
import Ansi from './ansi.ts';
|
|
|
|
import Buffer from './buffer.ts';
|
|
|
|
import { importDefaultForRuntime } from '../runtime.ts';
|
|
|
|
import { ctrl_key } from '../strings.ts';
|
2023-11-09 10:46:12 -05:00
|
|
|
import { ITerminalSize } from '../types.ts';
|
2023-11-08 11:11:19 -05:00
|
|
|
|
2023-11-03 12:26:09 -04:00
|
|
|
export class Editor {
|
2023-11-08 11:11:19 -05:00
|
|
|
#buffer: Buffer;
|
2023-11-09 10:46:12 -05:00
|
|
|
#screenRows: number;
|
|
|
|
#screenCols: number;
|
|
|
|
constructor(terminalSize: ITerminalSize) {
|
2023-11-08 11:11:19 -05:00
|
|
|
this.#buffer = new Buffer();
|
2023-11-09 10:46:12 -05:00
|
|
|
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'):
|
|
|
|
this.clearScreen();
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the screen and write out the buffer
|
|
|
|
*/
|
|
|
|
public async refreshScreen(): Promise<void> {
|
2023-11-08 18:07:34 -05:00
|
|
|
const { write } = await importDefaultForRuntime('terminal_io');
|
2023-11-08 11:11:19 -05:00
|
|
|
|
|
|
|
this.clearScreen();
|
2023-11-08 17:02:59 -05:00
|
|
|
this.drawRows();
|
|
|
|
|
2023-11-08 11:11:19 -05:00
|
|
|
await write(this.#buffer.getBuffer());
|
|
|
|
this.#buffer.clear();
|
2023-11-03 12:26:09 -04:00
|
|
|
}
|
2023-11-08 11:11:19 -05:00
|
|
|
|
2023-11-08 17:02:59 -05:00
|
|
|
private drawRows(): void {
|
2023-11-09 10:46:12 -05:00
|
|
|
for (let y = 0; y <= this.#screenRows; y++) {
|
2023-11-08 17:02:59 -05:00
|
|
|
this.#buffer.appendLine('~');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 11:11:19 -05:00
|
|
|
private clearScreen(): void {
|
|
|
|
this.#buffer.append(Ansi.ClearScreen);
|
|
|
|
this.#buffer.append(Ansi.ResetCursor);
|
2023-11-03 12:26:09 -04:00
|
|
|
}
|
|
|
|
}
|