vertical scrolling

This commit is contained in:
Tyrel Souza 2021-10-21 00:26:42 -04:00
parent 1713acd5e8
commit 221a6569e8
2 changed files with 20 additions and 5 deletions

View File

@ -1 +1,4 @@
Tyrel's Text Editor Tyrel's Text Editor
https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#vertical-scrolling

22
tted.c
View File

@ -220,10 +220,20 @@ void abFree(struct abuf *ab) {
/** output */ /** 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) { void editorDrawRows(struct abuf *ab) {
int y; int y;
for (y = 0; y < E.screenrows; 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) { if (E.numrows == 0 && y == E.screenrows / 3) {
char welcome[80]; char welcome[80];
int welcomelen = snprintf(welcome, sizeof welcome, "Tyrel Text Editor Deluxe -- version %s", int welcomelen = snprintf(welcome, sizeof welcome, "Tyrel Text Editor Deluxe -- version %s",
@ -240,9 +250,9 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
} }
} else { // draw buffer } 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 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); abAppend(ab, "\x1b[K", 3);
@ -253,13 +263,15 @@ void editorDrawRows(struct abuf *ab) {
} }
void editorRefreshScreen() { void editorRefreshScreen() {
editorScroll();
struct abuf ab = ABUF_INIT; struct abuf ab = ABUF_INIT;
abAppend(&ab, "\x1b[?25l", 6); abAppend(&ab, "\x1b[?25l", 6);
abAppend(&ab, "\x1b[H", 3); // Cursor Position home abAppend(&ab, "\x1b[H", 3); // Cursor Position home
editorDrawRows(&ab); editorDrawRows(&ab);
char buf[32]; 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, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6); abAppend(&ab, "\x1b[?25h", 6);
@ -287,7 +299,7 @@ void editorMoveCursor(int key) {
} }
break; break;
case ARROW_DOWN: case ARROW_DOWN:
if (E.cy != E.screenrows - 1) { if (E.cy != E.numrows) {
E.cy++; E.cy++;
} }
break; break;