Load files
This commit is contained in:
parent
168a119064
commit
c54779f0ff
1 changed files with 60 additions and 12 deletions
72
kilo.c
72
kilo.c
|
@ -1,11 +1,16 @@
|
|||
/*** includes ***/
|
||||
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _BSD_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -30,10 +35,17 @@ enum editorKey {
|
|||
|
||||
/*** 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;
|
||||
};
|
||||
|
||||
|
@ -156,6 +168,32 @@ int getWindowSize(int *rows, int *cols) {
|
|||
}
|
||||
}
|
||||
|
||||
/*** 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 ***/
|
||||
|
||||
struct abuf {
|
||||
char *b;
|
||||
int len;
|
||||
|
@ -179,20 +217,26 @@ void abFree(struct abuf *ab) {
|
|||
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),
|
||||
"Kilo editor -- version %s", KILO_VERSION);
|
||||
if (welcomelen > E.screencols) welcomelen = E.screencols;
|
||||
int padding = (E.screencols - welcomelen) / 2;
|
||||
if (padding) {
|
||||
if (y >= E.numrows) {
|
||||
if (E.numrows == 0 && y == E.screenrows / 3) {
|
||||
char welcome[80];
|
||||
int welcomelen = snprintf(welcome, sizeof(welcome),
|
||||
"Kilo editor -- version %s", KILO_VERSION);
|
||||
if (welcomelen > E.screencols) 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);
|
||||
padding--;
|
||||
}
|
||||
while(padding--) abAppend(ab, " ", 1);
|
||||
abAppend(ab, welcome, welcomelen);
|
||||
} else {
|
||||
abAppend(ab, "~", 1);
|
||||
int len = E.row.size;
|
||||
if (len > E.screencols) len = E.screencols;
|
||||
abAppend(ab, E.row.chars, len);
|
||||
}
|
||||
|
||||
abAppend(ab, "\x1b[K", 3); // Erase one line
|
||||
|
@ -290,14 +334,18 @@ void editorProcessKeypress() {
|
|||
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();
|
||||
|
|
Loading…
Reference in a new issue