creating new lines works now, fixed several segfaults and bugs
This commit is contained in:
Binary file not shown.
+5
-43
@@ -102,48 +102,6 @@ int str_remove(String_t* self, int index) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
String_t* str_lines(String_t* self, int* numlines) {
|
||||
|
||||
char* string = self->data;
|
||||
String_t* lines = NULL;
|
||||
|
||||
// find the number of lines in the file
|
||||
*numlines = 0;
|
||||
for (int i = 0; i < strlen(string); i++) {
|
||||
if (string[i] == '\n') {
|
||||
(*numlines)++;
|
||||
}
|
||||
}
|
||||
|
||||
// add one if the last char is not a newline
|
||||
// in this case there is one more line than newline symbols
|
||||
if (self->data[strlen(self->data) - 1] != '\n') {
|
||||
(*numlines)++;
|
||||
}
|
||||
|
||||
// allocate memory for an array of pointers to each line
|
||||
lines = (String_t*)malloc((*numlines + 1) * sizeof(String_t));
|
||||
|
||||
int i = 0;
|
||||
char* start = string;
|
||||
char* end = string;
|
||||
|
||||
while (*end != '\0') {
|
||||
if (*end == '\n') {
|
||||
lines[i] = str_from_slice(start, end - start);
|
||||
|
||||
end++;
|
||||
start = end;
|
||||
|
||||
i++;
|
||||
} else {
|
||||
end++;
|
||||
}
|
||||
}
|
||||
// returns an array of String_t
|
||||
return lines;
|
||||
}
|
||||
|
||||
/// splits a string into an array of strings based on a delimiter
|
||||
String_t* str_split(String_t* self, int* res_len, char c) {
|
||||
|
||||
@@ -198,6 +156,10 @@ String_t* str_split(String_t* self, int* res_len, char c) {
|
||||
return elements;
|
||||
}
|
||||
|
||||
String_t* str_lines(String_t* self, int* numlines) {
|
||||
return str_split(self, numlines, '\n');
|
||||
}
|
||||
|
||||
int str_len(String_t* s) {
|
||||
return s->size;
|
||||
}
|
||||
@@ -207,7 +169,7 @@ char* to_chars(String_t* s) {
|
||||
}
|
||||
|
||||
// int main() {
|
||||
// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n\0");
|
||||
// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n");
|
||||
|
||||
// str_remove(&s, 10);
|
||||
// str_insert(&s, 10, 'h');
|
||||
|
||||
+26
-37
@@ -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);
|
||||
}
|
||||
|
||||
+19
-1
File diff suppressed because one or more lines are too long
+2
-5
@@ -22,11 +22,8 @@ int open_editor() {
|
||||
int max_y, max_x;
|
||||
getmaxyx(stdscr, max_y, max_x);
|
||||
|
||||
move(0, 0);
|
||||
|
||||
Editor editor = editor_from("this is some text to edit\nthis is the second line\nand this is the third lol");
|
||||
|
||||
fprintf(stderr, "%d %d %d %d", editor.lines, editor.screen_line, editor.screen_col, editor.editmode);
|
||||
move(0, 5);
|
||||
|
||||
while (true) {
|
||||
refresh();
|
||||
@@ -37,8 +34,8 @@ int open_editor() {
|
||||
editor.editmode = false;
|
||||
break;
|
||||
case KEY_BACKSPACE:
|
||||
delchar(&editor);
|
||||
move_cursor(&editor, -1, 0);
|
||||
delchar(&editor);
|
||||
break;
|
||||
case KEY_DC:
|
||||
delchar(&editor);
|
||||
|
||||
@@ -223,6 +223,8 @@ fn open(filename: &str) -> Result<(), &'static str> {
|
||||
|
||||
ncurses::mv(0, 0);
|
||||
|
||||
|
||||
|
||||
loop {
|
||||
ncurses::refresh();
|
||||
let keystroke = ncurses::getch();
|
||||
|
||||
Reference in New Issue
Block a user