Add vertical scolling

This commit is contained in:
Aaron Fischer 2018-09-21 17:05:22 +02:00
parent 98c53586d1
commit b9f9aec88b

24
kilo.c
View file

@ -42,6 +42,7 @@ typedef struct erow {
struct editorConfig {
int cx, cy;
int rowoff;
int screenrows;
int screencols;
int numrows;
@ -222,10 +223,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) {
int filerow = y + E.rowoff;
if (filerow >= E.numrows) {
if (E.numrows == 0 && y == E.screenrows / 3) {
char welcome[80];
int welcomelen = snprintf(welcome, sizeof(welcome),
@ -242,9 +253,9 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1);
}
} else {
int len = E.row[y].size;
int len = E.row[filerow].size;
if (len > E.screencols) len = E.screencols;
abAppend(ab, E.row[y].chars, len);
abAppend(ab, E.row[filerow].chars, len);
}
abAppend(ab, "\x1b[K", 3); // Erase one line
@ -255,6 +266,8 @@ void editorDrawRows(struct abuf *ab) {
}
void editorRefreshScreen() {
editorScroll();
struct abuf ab = ABUF_INIT;
abAppend(&ab, "\x1b[?25l", 6); // Hide cursor
@ -264,7 +277,7 @@ void editorRefreshScreen() {
// Update cursor position
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); // Show cursor
@ -293,7 +306,7 @@ void editorMoveCursor(int key) {
}
break;
case ARROW_DOWN:
if (E.cy != E.screenrows -1) {
if (E.cy < E.numrows) {
E.cy++;
}
break;
@ -342,6 +355,7 @@ void editorProcessKeypress() {
void initEditor() {
E.cx = 0;
E.cy = 0;
E.rowoff = 0;
E.numrows = 0;
E.row = NULL;