creating new lines works now, fixed several segfaults and bugs

This commit is contained in:
FantasyPvP
2024-11-07 14:50:17 +00:00
parent 214dfc66a5
commit bef5547d50
6 changed files with 54 additions and 86 deletions
+26 -37
View File
@@ -11,24 +11,6 @@ typedef struct {
String_t* buffer;
} Editor;
Editor new_editor() {
Editor e;
e.lines = 0;
e.screen_line = 0;
e.screen_col = 0;
e.editmode = false;
String_t lines = str_from_chars("");
int linenum = 0;
e.buffer = str_lines(&lines, &linenum);
e.lines = linenum;
str_dealloc(&lines);
addstr("");
return e;
}
Editor editor_from(char* input_string) {
Editor e;
e.lines = 0;
@@ -40,27 +22,26 @@ Editor editor_from(char* input_string) {
String_t lines = str_from_chars(input_string);
e.buffer = str_lines(&lines, &linenum);
e.lines = (size_t)linenum;
fprintf(stderr, "BUFFER: %s", to_chars(&e.buffer[2]));
str_dealloc(&lines);
// String_t s = str_from_chars(input_string);
// int linen = 0;
// String_t* lines = str_lines(&s, &linen);
// for (int i=0; i < linen; i++) {
// fprintf(stderr, "%s", to_chars(&lines[i]));
// }
// e.lines = (size_t)linen;
// e.buffer = lines;
// str_dealloc(&s);
addstr(input_string);
for (size_t i = 0; i < e.lines; i++) {
// adding the line number
char line_no[5];
snprintf(line_no, 5, "%-5d", i + 1);
// adding the line of text to the buffer
addstr(line_no);
addch(' ');
addstr(to_chars(&e.buffer[i]));
addstr("\n");
}
return e;
}
Editor new_editor() {
return editor_from("");
}
void move_cursor(Editor* self, int x, int y) {
fprintf(stderr, "lns %d, line %d, col %d, mode %d", self->lines, self->screen_line, self->screen_col, self->editmode);
fprintf(stderr, "line: %s", to_chars(&self->buffer[self->screen_line]));
if (y != 0
&& (int)(self->screen_line) + y >= 0
&& self->screen_line + y <= self->lines)
@@ -71,8 +52,7 @@ void move_cursor(Editor* self, int x, int y) {
self->screen_col = line_width;
}
} else if ((int)(self->screen_col) + x < 0) {
fprintf(stderr, "going off left side");
if (self->screen_line - 1 >= 0) {
if ((int)self->screen_line - 1 >= 0) {
self->screen_line -= 1;
self->screen_col = str_len(&self->buffer[self->screen_line]);
}
@@ -84,8 +64,7 @@ void move_cursor(Editor* self, int x, int y) {
} else if (x != 0) {
self->screen_col += x;
}
fprintf(stderr, "lns %d, line %d, col %d, mode %d", self->lines, self->screen_line, self->screen_col, self->editmode);
move(self->screen_line, self->screen_col);
move(self->screen_line, self->screen_col + 5);
}
void delchar(Editor* self) {
@@ -97,10 +76,20 @@ void addchar(Editor* self, char c) {
insch(c);
// insert the character into the string at the given index
if (self->screen_line == self->lines) {
fprintf(stderr, "buffer full\n");
// reallocate self->buffer to be 1 larger
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines + 1));
// allocate the memory space for the new line and add it to the buffer
self->buffer[self->screen_line] = str_new();
for (size_t i = 0; i < self->lines; i++) {
fprintf(stderr, "size: %d\n", (&self->buffer[i])->size);
}
fprintf(stderr, "ok!");
}
str_insert(&self->buffer[self->screen_line], self->screen_col, c);
move_cursor(self, 1, 0);
}