feat: Read whole file, display up to E.screenrows lines.

This commit is contained in:
jmug
2026-10-30 20:50:55 -07:00
parent d12f61f7c4
commit 691da8ec8c
+20 -10
View File
@@ -44,7 +44,7 @@ struct editorConfig {
int screenrows; int screenrows;
int screencols; int screencols;
int numrows; int numrows;
erow row; erow *row;
struct termios orig_termios; struct termios orig_termios;
}; };
@@ -194,6 +194,20 @@ int getWindowSize(int *rows, int *cols) {
return 0; 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 ***/ /*** file i/o ***/
void editorOpen(char *filename) { void editorOpen(char *filename) {
@@ -204,16 +218,11 @@ void editorOpen(char *filename) {
char *line = NULL; char *line = NULL;
size_t linecap = 0; size_t linecap = 0;
ssize_t linelen; ssize_t linelen;
linelen = getline(&line, &linecap, fp); while ((linelen = getline(&line, &linecap, fp)) != -1) {
if (linelen != -1) {
while (linelen > 0 && (line[linelen-1] == '\n' || while (linelen > 0 && (line[linelen-1] == '\n' ||
line[linelen-1] == '\r')) line[linelen-1] == '\r'))
linelen--; linelen--;
E.row.size = linelen; editorAppendRow(line, linelen);
E.row.chars = malloc(linelen+1);
memcpy(E.row.chars, line, linelen);
E.row.chars[linelen] = '\0';
E.numrows = 1;
} }
free(line); free(line);
fclose(fp); fclose(fp);
@@ -270,10 +279,10 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
} }
} else { } else {
int len = E.row.size; int len = E.row[y].size;
if (len > E.screencols) if (len > E.screencols)
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. // Erase the line content to the right of the cursor.
@@ -377,6 +386,7 @@ void editorProcessKeypress() {
void initEditor() { void initEditor() {
E.cx = 0; E.cx = 0;
E.cy = 0; E.cy = 0;
E.row = NULL;
E.numrows = 0; E.numrows = 0;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) { if (getWindowSize(&E.screenrows, &E.screencols) == -1) {
die("getWindowSize"); die("getWindowSize");