fixed some bugs in c versiosn

This commit is contained in:
FantasyPvP
2024-11-05 09:32:36 +00:00
parent 83a71ae0a1
commit b40ecc5bb1
9 changed files with 136 additions and 76 deletions
+19 -6
View File
@@ -9,7 +9,7 @@ typedef struct {
int screen_line;
int screen_col;
bool editmode;
String* buffer;
String_t* buffer;
} Editor;
Editor new_editor() {
@@ -18,7 +18,13 @@ Editor new_editor() {
e.screen_line = 0;
e.screen_col = 0;
e.editmode = false;
e.buffer = NULL;
String_t lines = str_from_chars("");
e.buffer = str_lines(&lines, &e.lines);
str_dealloc(&lines);
addstr("");
return e;
}
@@ -29,12 +35,18 @@ Editor editor_from(char* input_string) {
e.screen_line = 0;
e.screen_col = 0;
e.editmode = false;
e.buffer = NULL;
String_t lines = str_from_chars(input_string);
e.buffer = str_lines(&lines, &e.lines);
str_dealloc(&lines);
addstr(input_string);
return e;
}
void move_cursor(Editor* self, int x, int y) {
if (x != 0
if (y != 0
&& self->screen_line + y >= 0
&& self->screen_line + y <= self->lines)
{
@@ -45,6 +57,7 @@ void move_cursor(Editor* self, int x, int y) {
}
} else if (self->screen_col + x < 0) {
if (self->screen_line - 1 >= 0) {
self->screen_line -= 1;
self->screen_col = str_len(&self->buffer[self->screen_line]);
}
} else if (self->screen_col + x > str_len(&self->buffer[self->screen_line])) {
@@ -70,13 +83,13 @@ void addchar(Editor* self, char c) {
if (self->screen_line == self->lines) {
// reallocate self->buffer to be 1 larger
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String) * (self->lines + 1));
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines + 1));
}
str_insert(&self->buffer[self->screen_line], self->screen_col, c);
move_cursor(self, 1, 0);
}
String* to_string(Editor* self) {
String_t* to_string(Editor* self) {
}