From 7e76609326c853aefad981c948c24f9f14136e3b Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Tue, 2 Oct 2018 10:30:17 +0200 Subject: [PATCH] Finish the text viewer --- kilo.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 87 insertions(+), 8 deletions(-) diff --git a/kilo.c b/kilo.c index 8666576..a3f872c 100644 --- a/kilo.c +++ b/kilo.c @@ -7,11 +7,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include /*** defines ***/ @@ -45,12 +47,16 @@ typedef struct erow { struct editorConfig { int cx, cy; + int rx; // The x position in the rendered line (for tabs) int rowoff; int coloff; int screenrows; int screencols; int numrows; erow *row; + char *filename; + char statusmsg[80]; + time_t statusmsg_time; struct termios orig_termios; }; @@ -175,6 +181,18 @@ int getWindowSize(int *rows, int *cols) { /*** row operations ***/ +int editorRowCxToRx(erow *row, int cx) { + int rx = 0; + int j; + for (j=0; jchars[j] == '\t') { + rx += (KILO_TAB_STOP - 1) - (rx % KILO_TAB_STOP); + } + rx++; + } + return rx; +} + void editorUpdateRow(erow *row) { int tabs = 0; int j; @@ -217,6 +235,9 @@ void editorAppendRow(char *s, size_t len) { /*** file i/o ***/ void editorOpen(char *filename) { + free(E.filename); + E.filename = strdup(filename); + FILE *fp = fopen(filename, "r"); if (!fp) die("fopen"); @@ -256,17 +277,22 @@ void abFree(struct abuf *ab) { /*** output ***/ void editorScroll() { + E.rx = E.cx; + if (E.cy < E.numrows) { + E.rx = editorRowCxToRx(&E.row[E.cy], E.cx); + } + if (E.cy < E.rowoff) { E.rowoff = E.cy; } if (E.cy >= E.rowoff + E.screenrows) { E.rowoff = E.cy - E.screenrows + 1; } - if (E.cx < E.coloff) { - E.coloff = E.cx; + if (E.rx < E.coloff) { + E.coloff = E.rx; } - if (E.cx >= E.coloff + E.screencols) { - E.coloff = E.cx - E.screencols + 1; + if (E.rx >= E.coloff + E.screencols) { + E.coloff = E.rx - E.screencols + 1; } } @@ -298,10 +324,38 @@ void editorDrawRows(struct abuf *ab) { } abAppend(ab, "\x1b[K", 3); // Erase one line - if (y < E.screenrows - 1) { - abAppend(ab, "\r\n", 2); + abAppend(ab, "\r\n", 2); + } +} + +void editorDrawStatusBar(struct abuf *ab) { + abAppend(ab, "\x1b[7m", 4); + char status[80], rstatus[80]; + int len = snprintf(status, sizeof(status), "%.20s - %d lines", + E.filename ? E.filename : "[No Name]", E.numrows); + int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d", + E.cy + 1, E.numrows); + if (len > E.screencols) len = E.screencols; + abAppend(ab, status, len); + while (len < E.screencols) { + if (E.screencols - len == rlen) { + abAppend(ab, rstatus, rlen); + break; + } else { + abAppend(ab, " ", 1); + len++; } } + abAppend(ab, "\x1b[m", 3); + abAppend(ab, "\r\n", 2); +} + +void editorDrawMessageBar(struct abuf *ab) { + abAppend(ab, "\x1b[K", 3); + int msglen = strlen(E.statusmsg); + if (msglen > E.screencols) msglen = E.screencols; + if (msglen && time(NULL) - E.statusmsg_time < 5) + abAppend(ab, E.statusmsg, msglen); } void editorRefreshScreen() { @@ -313,10 +367,12 @@ void editorRefreshScreen() { abAppend(&ab, "\x1b[H", 3); // CUP (cursor position) editorDrawRows(&ab); + editorDrawStatusBar(&ab); + editorDrawMessageBar(&ab); // Update cursor position char buf[32]; - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.cx - E.coloff) + 1); + snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1); abAppend(&ab, buf, strlen(buf)); abAppend(&ab, "\x1b[?25h", 6); // Show cursor @@ -325,6 +381,14 @@ void editorRefreshScreen() { abFree(&ab); } +void editorSetStatusMessage(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vsnprintf(E.statusmsg, sizeof(E.statusmsg), fmt, ap); + va_end(ap); + E.statusmsg_time = time(NULL); +} + /*** input ***/ void editorMoveCursor(int key) { @@ -382,12 +446,20 @@ void editorProcessKeypress() { break; case END_KEY: - E.cx = E.screencols -1; + if (E.cy < E.numrows) { + E.cx = E.row[E.cy].size; + } break; case PAGE_UP: case PAGE_DOWN: { + if (c == PAGE_UP) { + E.cy = E.rowoff; + } else if (c == PAGE_DOWN) { + E.cy = E.rowoff + E.screenrows - 1; + if (E.cy > E.numrows) E.cy = E.numrows; + } int times = E.screenrows; while(times--) editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN); @@ -408,13 +480,18 @@ void editorProcessKeypress() { void initEditor() { E.cx = 0; E.cy = 0; + E.rx = 0; E.rowoff = 0; E.coloff = 0; E.numrows = 0; E.row = NULL; + E.filename = NULL; + E.statusmsg[0] = '\0'; + E.statusmsg_time = 0; if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize"); + E.screenrows -= 2; } int main(int argc, char *argv[]) { @@ -424,6 +501,8 @@ int main(int argc, char *argv[]) { editorOpen(argv[1]); } + editorSetStatusMessage("HELP: Ctrl-Q = Quit"); + while (1) { editorRefreshScreen(); editorProcessKeypress();