From ddbc8d6346f383da636bea540163868cda5f786a Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Thu, 21 Oct 2021 01:13:33 -0400 Subject: [PATCH] statusbars --- README.md | 2 ++ tted.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f994af7..cb62a4d 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,5 @@ https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#vertical-scrolling https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#horizontal-scrolling https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#tabs-and-the-cursor https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#scrolling-with-page-up-and-page-down + +https://viewsourcecode.org/snaptoken/kilo/05.aTextEditor.html diff --git a/tted.c b/tted.c index c2380e2..79c4946 100644 --- a/tted.c +++ b/tted.c @@ -6,11 +6,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include /** defines */ @@ -48,6 +50,9 @@ struct editorConfig { int screencols; int numrows; erow *row; + char *filename; + char statusmsg[80]; + time_t statusmsg_time; struct termios orig_termios; }; struct editorConfig E; @@ -176,14 +181,15 @@ int getWindowSize(int *rows, int *cols) { int editorRowCxToRx(erow *row, int cx) { int rx = 0; int j; - for (j=0; j < cx; j++){ + for (j = 0; j < cx; j++) { if (row->chars[j] == '\t') { - rx += (TTED_TAB_STOP -1) - (rx % TTED_TAB_STOP); + rx += (TTED_TAB_STOP - 1) - (rx % TTED_TAB_STOP); } rx++; } return rx; } + void editorUpdateRow(erow *row) { int tabs = 0; int j; @@ -194,7 +200,7 @@ void editorUpdateRow(erow *row) { } free(row->render); - row->render = malloc(row->size + tabs*(TTED_TAB_STOP - 1) + 1); + row->render = malloc(row->size + tabs * (TTED_TAB_STOP - 1) + 1); int idx = 0; for (j = 0; j < row->size; j++) { @@ -226,6 +232,8 @@ 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"); @@ -267,7 +275,7 @@ void abFree(struct abuf *ab) { /** output */ void editorScroll() { E.rx = 0; - if (E.cy < E.numrows){ + if (E.cy < E.numrows) { E.rx = editorRowCxToRx(&E.row[E.cy], E.cx); } @@ -314,12 +322,42 @@ void editorDrawRows(struct abuf *ab) { } abAppend(ab, "\x1b[K", 3); - 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() { editorScroll(); @@ -327,6 +365,8 @@ void editorRefreshScreen() { abAppend(&ab, "\x1b[?25l", 6); abAppend(&ab, "\x1b[H", 3); // Cursor Position home editorDrawRows(&ab); + editorDrawStatusBar(&ab); + editorDrawMessageBar(&ab); char buf[32]; snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1); @@ -337,6 +377,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) { @@ -393,11 +441,21 @@ 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); @@ -421,7 +479,12 @@ void initEditor() { 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[]) { @@ -431,6 +494,8 @@ int main(int argc, char *argv[]) { editorOpen(argv[1]); } + editorSetStatusMessage("HELP: Ctrl-Q = quit"); + while (1) { editorRefreshScreen(); editorProcessKeypress();