Basic vertical scrolling

This commit is contained in:
Timothy Warren 2023-11-20 11:12:22 -05:00
parent a1aa189e11
commit 9b95850e86
2 changed files with 23 additions and 1 deletions

View File

@ -2,6 +2,9 @@
default:
@just --list
# Test coverage
coverage: bun-test deno-coverage
# Typescript checking
check: deno-check bun-check

View File

@ -20,6 +20,10 @@ export class Editor {
* @private
*/
#cursor: IPoint;
/**
* The current scrolling offset
*/
#offset: IPoint;
/**
* The document being edited
* @private
@ -32,6 +36,10 @@ export class Editor {
x: 0,
y: 0,
};
this.#offset = {
x: 0,
y: 0,
};
this.#document = Document.empty();
}
@ -114,6 +122,15 @@ export class Editor {
}
}
private scroll(): void {
if (this.#cursor.y < this.#offset.y) {
this.#offset.y = this.#cursor.y;
}
if (this.#cursor.y >= this.#offset.y + this.#screen.rows) {
this.#offset.y = this.#cursor.y - this.#screen.rows + 1;
}
}
// --------------------------------------------------------------------------
// Terminal Output / Drawing
// --------------------------------------------------------------------------
@ -122,6 +139,7 @@ export class Editor {
* Clear the screen and write out the buffer
*/
public async refreshScreen(): Promise<void> {
this.scroll();
this.#buffer.append(Ansi.HideCursor);
this.#buffer.append(Ansi.ResetCursor);
this.drawRows();
@ -142,7 +160,8 @@ export class Editor {
private drawRows(): void {
for (let y = 0; y < this.#screen.rows; y++) {
if (y >= this.#document.numRows) {
let filerow = y + this.#offset.y;
if (filerow >= this.#document.numRows) {
this.drawPlaceholderRow(y);
} else {
this.drawFileRow(y);