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: default:
@just --list @just --list
# Test coverage
coverage: bun-test deno-coverage
# Typescript checking # Typescript checking
check: deno-check bun-check check: deno-check bun-check

View File

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