From 221a6569e89149022feb03f9538231e0f31e2d47 Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Thu, 21 Oct 2021 00:26:42 -0400 Subject: [PATCH] vertical scrolling --- README.md | 3 +++ tted.c | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8ffd262..bace049 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ Tyrel's Text Editor + + +https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#vertical-scrolling diff --git a/tted.c b/tted.c index 6585cad..035ada6 100644 --- a/tted.c +++ b/tted.c @@ -220,10 +220,20 @@ void abFree(struct abuf *ab) { /** output */ +void editorScroll() { + if (E.cy < E.rowoff){ + E.rowoff = E.cy; + } + if (E.cy >= E.rowoff + E.screenrows) { + E.rowoff = E.cy - E.screenrows +1; + } +} + void editorDrawRows(struct abuf *ab) { int y; for (y = 0; y < E.screenrows; y++) { - if (y >= E.numrows) { // Draw rows after buffer + int filerow = y + E.rowoff; + if (filerow >= E.numrows) { // Draw rows after buffer if (E.numrows == 0 && y == E.screenrows / 3) { char welcome[80]; int welcomelen = snprintf(welcome, sizeof welcome, "Tyrel Text Editor Deluxe -- version %s", @@ -240,9 +250,9 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "~", 1); } } else { // draw buffer - int len = E.row[y].size; + int len = E.row[filerow].size; if (len > E.screencols) len = E.screencols; // truncate if further than columns - abAppend(ab, E.row[y].chars, len); + abAppend(ab, E.row[filerow].chars, len); } abAppend(ab, "\x1b[K", 3); @@ -253,13 +263,15 @@ void editorDrawRows(struct abuf *ab) { } void editorRefreshScreen() { + editorScroll(); + struct abuf ab = ABUF_INIT; abAppend(&ab, "\x1b[?25l", 6); abAppend(&ab, "\x1b[H", 3); // Cursor Position home editorDrawRows(&ab); char buf[32]; - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E.cy + 1, E.cx + 1); + snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) +1, E.cx + 1); abAppend(&ab, buf, strlen(buf)); abAppend(&ab, "\x1b[?25h", 6); @@ -287,7 +299,7 @@ void editorMoveCursor(int key) { } break; case ARROW_DOWN: - if (E.cy != E.screenrows - 1) { + if (E.cy != E.numrows) { E.cy++; } break;