From 691da8ec8c67253215a3a0374bb1cdd74f95e930 Mon Sep 17 00:00:00 2001 From: jmug Date: Fri, 30 Oct 2026 20:50:55 -0700 Subject: [PATCH] feat: Read whole file, display up to E.screenrows lines. --- kilo.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/kilo.c b/kilo.c index 5d9573a..285c6fe 100644 --- a/kilo.c +++ b/kilo.c @@ -44,7 +44,7 @@ struct editorConfig { int screenrows; int screencols; int numrows; - erow row; + erow *row; struct termios orig_termios; }; @@ -194,6 +194,20 @@ int getWindowSize(int *rows, int *cols) { return 0; } +/*** row operations ***/ + +void editorAppendRow(char *r, size_t len) { + E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1)); + + int at = E.numrows; + E.row[at].size = len; + E.row[at].chars = malloc(len+1); + memcpy(E.row[at].chars, r, len); + E.row[at].chars[len] = '\0'; + + E.numrows++; +} + /*** file i/o ***/ void editorOpen(char *filename) { @@ -204,16 +218,11 @@ void editorOpen(char *filename) { char *line = NULL; size_t linecap = 0; ssize_t linelen; - linelen = getline(&line, &linecap, fp); - if (linelen != -1) { + while ((linelen = getline(&line, &linecap, fp)) != -1) { while (linelen > 0 && (line[linelen-1] == '\n' || line[linelen-1] == '\r')) linelen--; - E.row.size = linelen; - E.row.chars = malloc(linelen+1); - memcpy(E.row.chars, line, linelen); - E.row.chars[linelen] = '\0'; - E.numrows = 1; + editorAppendRow(line, linelen); } free(line); fclose(fp); @@ -270,10 +279,10 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "~", 1); } } else { - int len = E.row.size; + int len = E.row[y].size; if (len > E.screencols) len = E.screencols; - abAppend(ab, E.row.chars, len); + abAppend(ab, E.row[y].chars, len); } // Erase the line content to the right of the cursor. @@ -377,6 +386,7 @@ void editorProcessKeypress() { void initEditor() { E.cx = 0; E.cy = 0; + E.row = NULL; E.numrows = 0; if (getWindowSize(&E.screenrows, &E.screencols) == -1) { die("getWindowSize");