render files with tabs etc
This commit is contained in:
2 files changed
+128
-19
No files matched your search
@@ -1,2 +1,2 @@
|
|||||||
level: level.cpp
|
level: level.cpp
|
||||||
$(CC) level.cpp -o level -Wall -Wextra -pedantic
|
$(CC) level.cpp -o level -Wall -Wextra -pedantic
|
||||||
@@ -1,17 +1,25 @@
|
|||||||
/*** includes ***/
|
/*** includes ***/
|
||||||
|
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
#define _BSD_SOURCE
|
||||||
|
#ifndef _GNU_SOURCE
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#endif //_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>
|
||||||
|
|
||||||
/*** defines ***/
|
/*** defines ***/
|
||||||
|
|
||||||
#define LEVEL_VERSION "0.0.1"
|
#define LEVEL_VERSION "0.0.1"
|
||||||
|
#define LEVEL_TAB_STOP 8
|
||||||
|
|
||||||
#define CTRL_KEY(k) ((k) & 0x1f)
|
#define CTRL_KEY(k) ((k) & 0x1f)
|
||||||
|
|
||||||
@@ -29,10 +37,22 @@ enum editorKey {
|
|||||||
|
|
||||||
/*** data ***/
|
/*** data ***/
|
||||||
|
|
||||||
|
// erow stands for "editor row"
|
||||||
|
typedef struct erow {
|
||||||
|
int size;
|
||||||
|
int resize;
|
||||||
|
char *chars;
|
||||||
|
char *render;
|
||||||
|
} erow;
|
||||||
|
|
||||||
struct editorConfig {
|
struct editorConfig {
|
||||||
int cx, cy;
|
int cx, cy;
|
||||||
|
int rowoff;
|
||||||
|
int coloff;
|
||||||
int screenrows;
|
int screenrows;
|
||||||
int screencols;
|
int screencols;
|
||||||
|
int numrows;
|
||||||
|
erow *row;
|
||||||
struct termios orig_termios;
|
struct termios orig_termios;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -128,7 +148,7 @@ int editorReadKey() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int getCursorPosition(int* rows, int* cols) {
|
int getCursorPosition(int *rows, int *cols) {
|
||||||
char buf[32];
|
char buf[32];
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
|
|
||||||
@@ -160,10 +180,70 @@ int getWindowSize(int *rows, int *cols) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*** row operations ***/
|
||||||
|
|
||||||
|
void editorUpdateRow(erow *row) {
|
||||||
|
int tabs = 0;
|
||||||
|
for (int j = 0; j < row->size; j++) {
|
||||||
|
if (row->chars[j] == '\t') tabs++;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(row->render);
|
||||||
|
row->render = malloc(row->size + tabs*(LEVEL_TAB_STOP - 1) + 1);
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
|
for (int j = 0; j < row->size; j++) {
|
||||||
|
if (row->chars[j] == '\t') {
|
||||||
|
row->render[idx++] = ' ';
|
||||||
|
while (idx % LEVEL_TAB_STOP != 0) row->render(idx++) = ' ';
|
||||||
|
} else {
|
||||||
|
row->render[idx++] = row->chars[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
row->render[idx] = '\0';
|
||||||
|
row->rsize = idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
void editorAppendRow(char *s, size_t len) {
|
||||||
|
E.row = (erow*)realloc(E.row, sizeof(erow) * (E.numrows + 1));
|
||||||
|
|
||||||
|
int at = E.numrows;
|
||||||
|
E.row[at].size = len;
|
||||||
|
E.row[at].chars = (char*)malloc(len + 1);
|
||||||
|
memcpy(E.row[at].chars, s, len);
|
||||||
|
E.row[at].chars[len] = '\0';
|
||||||
|
|
||||||
|
E.row[at].rsize = 0;
|
||||||
|
E.row[at].render = NULL;
|
||||||
|
editorUpdateRow(&E.row[at]);
|
||||||
|
|
||||||
|
E.numrows++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*** 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;
|
||||||
|
while ((linelen = getline(&line, &linecap, fp)) != -1) {
|
||||||
|
while (linelen > 0 && (line[linelen - 1] == '\n' ||
|
||||||
|
line[linelen - 1] == '\r'))
|
||||||
|
linelen--;
|
||||||
|
editorAppendRow(line, linelen);
|
||||||
|
}
|
||||||
|
free(line);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/*** append buffer ***/
|
/*** append buffer ***/
|
||||||
|
|
||||||
struct abuf {
|
struct abuf {
|
||||||
char* b;
|
char *b;
|
||||||
int len;
|
int len;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -178,28 +258,43 @@ void abAppend(struct abuf *ab, const char *s, int len) {
|
|||||||
ab->len += len;
|
ab->len += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
void abFree(struct abuf* ab) {
|
void abFree(struct abuf *ab) {
|
||||||
free(ab->b);
|
free(ab->b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** output ***/
|
/*** output ***/
|
||||||
|
|
||||||
|
void editorScroll() {
|
||||||
|
if (E.cy < E.rowoff) E.rowoff = E.cy;
|
||||||
|
if (E.cy >= E.rowoff + E.screenrows) E.rowoff = E.cy - E.screenrows + 1;
|
||||||
|
if (E.cx < E.coloff) E.coloff = E.cx;
|
||||||
|
if (E.cx >= E.coloff + E.screencols) E.coloff = E.cx - E.screencols + 1;
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
int filerow = y + E.rowoff;
|
||||||
char welcome[80];
|
if (filerow >= E.numrows) {
|
||||||
int welcomelen = snprintf(welcome, sizeof(welcome),
|
if (E.numrows == 0 && y == E.screenrows / 3) {
|
||||||
"Level editor -- version %s", LEVEL_VERSION);
|
char welcome[80];
|
||||||
if (welcomelen > E.screencols) welcomelen = E.screencols;
|
int welcomelen = snprintf(welcome, sizeof(welcome),
|
||||||
int padding = (E.screencols - welcomelen) / 2;
|
"Level editor -- version %s", LEVEL_VERSION);
|
||||||
if (padding) {
|
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);
|
abAppend(ab, "~", 1);
|
||||||
padding--;
|
|
||||||
}
|
}
|
||||||
while (padding--) abAppend(ab, " ", 1);
|
|
||||||
abAppend(ab, welcome, welcomelen);
|
|
||||||
} else {
|
} else {
|
||||||
abAppend(ab, "~", 1);
|
int len = E.row[filerow].rsize - E.coloff;
|
||||||
|
if (len < 0) len = 0;
|
||||||
|
if (len > E.screencols) len = E.screencols;
|
||||||
|
abAppend(ab, &E.row[filerow].render[E.coloff], len);
|
||||||
}
|
}
|
||||||
|
|
||||||
abAppend(ab, "\x1b[K", 3);
|
abAppend(ab, "\x1b[K", 3);
|
||||||
@@ -208,6 +303,8 @@ void editorDrawRows(struct abuf *ab) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void editorRefreshScreen() {
|
void editorRefreshScreen() {
|
||||||
|
editorScroll();
|
||||||
|
|
||||||
struct abuf ab = ABUF_INIT;
|
struct abuf ab = ABUF_INIT;
|
||||||
|
|
||||||
abAppend(&ab, "\x1b[?25l", 6);
|
abAppend(&ab, "\x1b[?25l", 6);
|
||||||
@@ -217,7 +314,8 @@ void editorRefreshScreen() {
|
|||||||
editorDrawRows(&ab);
|
editorDrawRows(&ab);
|
||||||
|
|
||||||
char buf[32];
|
char buf[32];
|
||||||
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E.cy + 1, E.cx + 1);
|
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1,
|
||||||
|
(E.cx - E.coloff) + 1);
|
||||||
abAppend(&ab, buf, strlen(buf));
|
abAppend(&ab, buf, strlen(buf));
|
||||||
|
|
||||||
abAppend(&ab, "\x1b[?25h", 6);
|
abAppend(&ab, "\x1b[?25h", 6);
|
||||||
@@ -229,20 +327,26 @@ void editorRefreshScreen() {
|
|||||||
/*** input ***/
|
/*** input ***/
|
||||||
|
|
||||||
void editorMoveCursor(int key) {
|
void editorMoveCursor(int key) {
|
||||||
|
erow* row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
|
||||||
|
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case ARROW_LEFT:
|
case ARROW_LEFT:
|
||||||
if (E.cx != 0) E.cx--;
|
if (E.cx != 0) E.cx--;
|
||||||
break;
|
break;
|
||||||
case ARROW_RIGHT:
|
case ARROW_RIGHT:
|
||||||
if (E.cx != E.screencols -1) E.cx++;
|
if (row && E.cx < row->size) E.cx++;
|
||||||
break;
|
break;
|
||||||
case ARROW_UP:
|
case ARROW_UP:
|
||||||
if (E.cy != 0) E.cy--;
|
if (E.cy != 0) E.cy--;
|
||||||
break;
|
break;
|
||||||
case ARROW_DOWN:
|
case ARROW_DOWN:
|
||||||
if (E.cy != E.screenrows - 1) E.cy++;
|
if (E.cy < E.numrows - 1) E.cy++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
|
||||||
|
int rowlen = row ? row->size : 0;
|
||||||
|
if (E.cx > rowlen) E.cx = rowlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
void editorProcessKeypress() {
|
void editorProcessKeypress() {
|
||||||
@@ -287,13 +391,18 @@ void editorProcessKeypress() {
|
|||||||
void initEditor() {
|
void initEditor() {
|
||||||
E.cx = 0;
|
E.cx = 0;
|
||||||
E.cy = 0;
|
E.cy = 0;
|
||||||
|
E.rowoff = 0;
|
||||||
|
E.coloff = 0;
|
||||||
|
E.numrows = 0;
|
||||||
|
E.row = NULL;
|
||||||
|
|
||||||
if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
|
if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char *argv[]) {
|
||||||
enableRawMode();
|
enableRawMode();
|
||||||
initEditor();
|
initEditor();
|
||||||
|
if (argc >= 2) editorOpen(argv[1]);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
editorRefreshScreen();
|
editorRefreshScreen();
|
||||||
|
|||||||
Reference in new issue
Block a user