From d57f8ec7c591c71c172b1ede60b0085d09c7ec28 Mon Sep 17 00:00:00 2001 From: gurrium Date: Sat, 14 Jan 2023 20:59:03 +0900 Subject: [PATCH] next: https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#move-to-the-end-of-the-line-with-end#status-message --- kilo.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/kilo.c b/kilo.c index 3798f29..0b94470 100644 --- a/kilo.c +++ b/kilo.c @@ -51,6 +51,7 @@ struct editorConfig { int screencols; int numrows; erow *row; + char *filename; struct termios orig_termios; }; @@ -237,6 +238,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"); @@ -408,16 +412,26 @@ void editorDrawRows(struct abuf *ab) { } abAppend(ab, "\x1b[K", 3); - abAppend(ab, "\r\n", 2); + abAppend(ab, "\r\n", 2); } } void editorDrawStatusBar(struct abuf *ab) { abAppend(ab, "\x1b[7m", 4); - int len = 0; + 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); } @@ -453,6 +467,7 @@ void initEditor() { E.coloff = 0; E.numrows = 0; E.row = NULL; + E.filename = NULL; if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize"); E.screenrows -= 1;