rename tte

This commit is contained in:
Tyrel Souza 2021-10-21 00:03:25 -04:00
parent 0d16dc668e
commit 7271dc323d
3 changed files with 27 additions and 13 deletions

View File

@ -3,4 +3,4 @@ project(editor)
set(CMAKE_CXX_STANDARD 17)
add_executable(editor kilo.c)
add_executable(editor tte.c)

1
README.md Normal file
View File

@ -0,0 +1 @@
Tyrel's Text Editor

View File

@ -1,4 +1,8 @@
/** Includes */
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
@ -37,7 +41,7 @@ struct editorConfig {
int screenrows;
int screencols;
int numrows;
erow row;
erow *row;
struct termios orig_termios;
};
struct editorConfig E;
@ -161,6 +165,16 @@ int getWindowSize(int *rows, int *cols) {
return 0;
}
}
/** Row operations */
void editorAppendRow(char *s, 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, s, len);
E.row[at].chars[len] = '\0';
E.numrows++;
}
/** file i/o */
@ -169,23 +183,21 @@ void editorOpen(char *filename) {
if (!fp) die("fopen");
char *line = NULL;
ssize_t linecap = 0;
size_t linecap = 0;
ssize_t linelen;
linelen = getline(&line, &linecap, fp);
if (linelen != -1) {
while (linelen > 0 && (line[linelen - 1] == '\r' ||
while((linelen = getline(&line, &linecap, fp)) != -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;
editorAppendRow(line, linelen);
}
free(line);
fclose(fp);
}
/** append buffer */
struct abuf {
char *b;
@ -212,7 +224,7 @@ void editorDrawRows(struct abuf *ab) {
int y;
for (y = 0; y < E.screenrows; y++) {
if (y >= E.numrows) { // Draw rows after buffer
if (y == E.screenrows / 3) {
if (E.numrows == 0 && 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;
@ -227,9 +239,9 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1);
}
} else { // draw buffer
int len = E.row.size;
int len = E.row[y].size;
if (len > E.screencols) len = E.screencols; // truncate if further than columns
abAppend(ab, E.row.chars, len);
abAppend(ab, E.row[y].chars, len);
}
abAppend(ab, "\x1b[K", 3);
@ -319,6 +331,7 @@ void initEditor() {
E.cx = 0;
E.cy = 0;
E.numrows = 0;
E.row = NULL;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
}