diff --git a/kilo.c b/kilo.c index 59aa7ad..e32e879 100644 --- a/kilo.c +++ b/kilo.c @@ -1,14 +1,15 @@ -/*** Includes ***/ +/** Includes */ #include #include #include #include #include #include +#include #include #include -/** defines ***/ +/** defines */ #define TYREL_VERSION "0.0.1" #define CTRL_KEY(k) ((k) & 0x1f) @@ -25,16 +26,23 @@ enum editorKey { }; -/*** data ***/ +/** data */ +typedef struct erow { + int size; + char *chars; +} erow; + struct editorConfig { int cx, cy; int screenrows; int screencols; + int numrows; + erow row; struct termios orig_termios; }; struct editorConfig E; -/*** terminal ***/ +/** terminal */ void die(const char *s) { write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[H", 3); @@ -154,7 +162,31 @@ int getWindowSize(int *rows, int *cols) { } } -/*** append buffer ***/ +/** file i/o */ + +void editorOpen(char *filename) { + FILE *fp = fopen(filename, "r"); + if (!fp) die("fopen"); + + char *line = NULL; + ssize_t linecap = 0; + ssize_t linelen; + linelen = getline(&line, &linecap, fp); + if (linelen != -1) { + while (linelen > 0 && (line[linelen - 1] == '\r' || + 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 */ struct abuf { char *b; int len; @@ -175,24 +207,31 @@ void abFree(struct abuf *ab) { } -/*** output ***/ +/** output */ void editorDrawRows(struct abuf *ab) { int y; for (y = 0; y < E.screenrows; y++) { - if (y == E.screenrows / 3) { - char welcome[80]; - int welcomelen = snprintf(welcome, sizeof welcome, "Tyrel Editor -- version %s", TYREL_VERSION); - int padding = (E.screencols - welcomelen) / 2; - if (padding) { + if (y >= E.numrows) { // Draw rows after buffer + if (y == E.screenrows / 3) { + char welcome[80]; + int welcomelen = snprintf(welcome, sizeof welcome, "Tyrel Editor -- version %s", TYREL_VERSION); + int padding = (E.screencols - welcomelen) / 2; + if (padding) { + abAppend(ab, "~", 1); + padding--; + } + while (padding--) abAppend(ab, " ", 1); + if (welcomelen > E.screencols) welcomelen = E.screencols; + abAppend(ab, welcome, welcomelen); + } else { abAppend(ab, "~", 1); - padding--; } - while (padding--) abAppend(ab, " ", 1); - if (welcomelen > E.screencols) welcomelen = E.screencols; - abAppend(ab, welcome, welcomelen); - } else { - abAppend(ab, "~", 1); + } else { // draw buffer + int len = E.row.size; + if (len > E.screencols) len = E.screencols; // truncate if further than columns + abAppend(ab, E.row.chars, len); } + abAppend(ab, "\x1b[K", 3); if (y < E.screenrows - 1) { abAppend(ab, "\r\n", 2); @@ -216,7 +255,7 @@ void editorRefreshScreen() { } -/*** input ***/ +/** input */ void editorMoveCursor(int key) { switch (key) { case ARROW_LEFT: @@ -275,16 +314,20 @@ void editorProcessKeypress() { } } -/*** init ***/ +/** init */ void initEditor() { E.cx = 0; E.cy = 0; + E.numrows = 0; if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize"); } -int main() { +int main(int argc, char *argv[]) { enableRawMode(); initEditor(); + if (argc >= 2) { + editorOpen(argv[1]); + } while (1) { editorRefreshScreen();