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
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 */
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;