diff --git a/justfile b/justfile index 6e93f6c..594782c 100644 --- a/justfile +++ b/justfile @@ -2,6 +2,9 @@ default: @just --list +# Test coverage +coverage: bun-test deno-coverage + # Typescript checking check: deno-check bun-check diff --git a/src/common/editor.ts b/src/common/editor.ts index 88114e3..0c428be 100644 --- a/src/common/editor.ts +++ b/src/common/editor.ts @@ -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 { + 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);