feat: Read an actual file and display its first line.

This commit is contained in:
jmug
2026-10-30 20:36:16 -07:00
parent b06cab286c
commit d12f61f7c4
+67 -19
View File
@@ -1,12 +1,16 @@
/*** includes ***/ /*** includes ***/
#include <asm-generic/ioctls.h> #define _DEFAULT_SOURCE
#define _BSD_SOURCE
#define _GNU_SOURCE
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
@@ -30,10 +34,17 @@ enum editorKey {
/*** data ***/ /*** data ***/
typedef struct erow {
int size;
char *chars;
} erow;
struct editorConfig { struct editorConfig {
int cx, cy; int cx, cy;
int screenrows; int screenrows;
int screencols; int screencols;
int numrows;
erow row;
struct termios orig_termios; struct termios orig_termios;
}; };
@@ -183,6 +194,31 @@ int getWindowSize(int *rows, int *cols) {
return 0; return 0;
} }
/*** file i/o ***/
void editorOpen(char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp)
die("fopen");
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
linelen = getline(&line, &linecap, fp);
if (linelen != -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;
}
free(line);
fclose(fp);
}
/*** append buffer ***/ /*** append buffer ***/
struct abuf { struct abuf {
@@ -210,26 +246,34 @@ void abFree(struct abuf *ab) {
void editorDrawRows(struct abuf *ab) { void editorDrawRows(struct abuf *ab) {
for (int y = 0; y < E.screenrows; y++) { for (int y = 0; y < E.screenrows; y++) {
if (y == E.screenrows / 3) { if (y >= E.numrows) {
char welcome[80]; // We spilled over the file content.
int welcomelen = snprintf( if (E.numrows == 0 && y == E.screenrows / 3) {
welcome, char welcome[80];
sizeof(welcome), int welcomelen = snprintf(
"Kilo editor -- version %s", welcome,
KILO_VERSION sizeof(welcome),
); "Kilo editor -- version %s",
if (welcomelen > E.screencols) KILO_VERSION
welcomelen = E.screencols; );
int padding = (E.screencols - welcomelen) / 2; if (welcomelen > E.screencols)
if (padding) { welcomelen = E.screencols;
int padding = (E.screencols - welcomelen) / 2;
if (padding) {
abAppend(ab, "~", 1);
padding--;
}
while (padding--)
abAppend(ab, " ", 1);
abAppend(ab, welcome, welcomelen);
} else {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
padding--;
} }
while (padding--)
abAppend(ab, " ", 1);
abAppend(ab, welcome, welcomelen);
} else { } else {
abAppend(ab, "~", 1); int len = E.row.size;
if (len > E.screencols)
len = E.screencols;
abAppend(ab, E.row.chars, len);
} }
// Erase the line content to the right of the cursor. // Erase the line content to the right of the cursor.
@@ -333,14 +377,18 @@ void editorProcessKeypress() {
void initEditor() { void initEditor() {
E.cx = 0; E.cx = 0;
E.cy = 0; E.cy = 0;
E.numrows = 0;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) { if (getWindowSize(&E.screenrows, &E.screencols) == -1) {
die("getWindowSize"); die("getWindowSize");
} }
} }
int main() { int main(int argc, char *argv[]) {
enableRawMode(); enableRawMode();
initEditor(); initEditor();
if (argc > 1) {
editorOpen(argv[1]);
}
while(1) { while(1) {
editorRefreshScreen(); editorRefreshScreen();