diff --git a/final/binary b/final/binary index 62a1f15..c5a21f5 100755 Binary files a/final/binary and b/final/binary differ diff --git a/final/dynstr.c b/final/dynstr.c index 6d28f07..1823e5c 100644 --- a/final/dynstr.c +++ b/final/dynstr.c @@ -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'); diff --git a/final/editor.c b/final/editor.c index dd8bd48..2b438b1 100644 --- a/final/editor.c +++ b/final/editor.c @@ -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); } diff --git a/final/log.txt b/final/log.txt index d3f768c..ca0ed9d 100644 --- a/final/log.txt +++ b/final/log.txt @@ -1 +1,19 @@ -BUFFER: (null)3 0 0 0lns 3, line 0, col 0, mode 1line: this is some text to editlns 3, line 0, col 1, mode 1lns 3, line 0, col 1, mode 1line: this is some text to editlns 3, line 0, col 2, mode 1lns 3, line 0, col 2, mode 1line: this is some text to editlns 3, line 0, col 3, mode 1lns 3, line 0, col 3, mode 1line: this is some text to editlns 3, line 0, col 4, mode 1lns 3, line 0, col 4, mode 1line: this is some text to editlns 3, line 0, col 5, mode 1lns 3, line 0, col 5, mode 1line: this is some text to editlns 3, line 1, col 5, mode 1lns 3, line 1, col 5, mode 1line: this is the second linelns 3, line 1, col 4, mode 1lns 3, line 1, col 4, mode 1line: this is the second linelns 3, line 1, col 3, mode 1lns 3, line 1, col 3, mode 1line: this is the second linelns 3, line 1, col 2, mode 1lns 3, line 1, col 2, mode 1line: this is the second linelns 3, line 1, col 1, mode 1lns 3, line 1, col 1, mode 1line: this is the second linelns 3, line 1, col 0, mode 1lns 3, line 1, col 0, mode 1line: this is the second linegoing off left sidelns 3, line 0, col 25, mode 1lns 3, line 0, col 25, mode 1line: this is some text to editlns 3, line 1, col 0, mode 1lns 3, line 1, col 0, mode 1line: this is the second linelns 3, line 1, col 1, mode 1lns 3, line 1, col 1, mode 1line: this is the second linelns 3, line 1, col 2, mode 1lns 3, line 1, col 2, mode 1line: this is the second linelns 3, line 1, col 3, mode 1lns 3, line 1, col 3, mode 1line: this is the second linelns 3, line 1, col 4, mode 1lns 3, line 1, col 4, mode 1line: this is the second linelns 3, line 1, col 5, mode 1lns 3, line 1, col 5, mode 1line: this is the second linelns 3, line 1, col 6, mode 1lns 3, line 1, col 6, mode 1line: this is the second linelns 3, line 1, col 7, mode 1lns 3, line 1, col 7, mode 1line: this is the second linelns 3, line 1, col 8, mode 1lns 3, line 1, col 8, mode 1line: this is the second linelns 3, line 1, col 9, mode 1lns 3, line 1, col 9, mode 1line: this is the second linelns 3, line 1, col 10, mode 1lns 3, line 1, col 10, mode 1line: this is the second linelns 3, line 1, col 11, mode 1lns 3, line 1, col 11, mode 1line: this is the second linelns 3, line 1, col 12, mode 1lns 3, line 1, col 12, mode 1line: this is the second linelns 3, line 1, col 13, mode 1lns 3, line 1, col 13, mode 1line: this is the second linelns 3, line 1, col 14, mode 1lns 3, line 1, col 14, mode 1line: this is the second linelns 3, line 1, col 15, mode 1lns 3, line 1, col 15, mode 1line: this is the second linelns 3, line 1, col 16, mode 1lns 3, line 1, col 16, mode 1line: this is the second linelns 3, line 1, col 17, mode 1lns 3, line 1, col 17, mode 1line: this is the second linelns 3, line 1, col 18, mode 1lns 3, line 1, col 18, mode 1line: this is the second linelns 3, line 1, col 19, mode 1lns 3, line 1, col 19, mode 1line: this is the second linelns 3, line 1, col 20, mode 1lns 3, line 1, col 20, mode 1line: this is the second linelns 3, line 1, col 21, mode 1lns 3, line 1, col 21, mode 1line: this is the second linelns 3, line 1, col 22, mode 1lns 3, line 1, col 22, mode 1line: this is the second linelns 3, line 1, col 23, mode 1lns 3, line 1, col 23, mode 1line: this is the second linelns 3, line 2, col 0, mode 1lns 3, line 2, col 0, mode 1line: (null)lns 3, line 3, col 0, mode 1lns 3, line 3, col 0, mode 1line: (null)lns 3, line 3, col 0, mode 1lns 3, line 3, col 0, mode 1line: (null)lns 3, line 3, col 0, mode 1lns 3, line 3, col 0, mode 1line: (null)lns 3, line 3, col 0, mode 1lns 3, line 3, col 0, mode 1line: (null)going off left sidelns 3, line 2, col 0, mode 1lns 3, line 2, col 0, mode 1line: (null)going off left sidelns 3, line 1, col 23, mode 1lns 3, line 1, col 23, mode 1line: this is the second linelns 3, line 1, col 22, mode 1lns 3, line 1, col 22, mode 1line: this is the second linelns 3, line 1, col 21, mode 1lns 3, line 1, col 21, mode 1line: this is the second linelns 3, line 1, col 20, mode 1lns 3, line 1, col 20, mode 1line: this is the second linelns 3, line 1, col 19, mode 1lns 3, line 1, col 19, mode 1line: this is the second linelns 3, line 1, col 18, mode 1lns 3, line 1, col 18, mode 1line: this is the second linelns 3, line 1, col 17, mode 1lns 3, line 1, col 17, mode 1line: this is the second linelns 3, line 1, col 16, mode 1lns 3, line 1, col 16, mode 1line: this is the second linelns 3, line 1, col 15, mode 1lns 3, line 1, col 15, mode 1line: this is the second linelns 3, line 1, col 14, mode 1lns 3, line 1, col 14, mode 1line: this is the second linelns 3, line 1, col 13, mode 1lns 3, line 1, col 13, mode 1line: this is the second linelns 3, line 1, col 12, mode 1lns 3, line 1, col 12, mode 1line: this is the second linelns 3, line 1, col 11, mode 1lns 3, line 1, col 11, mode 1line: this is the second linelns 3, line 1, col 10, mode 1lns 3, line 1, col 10, mode 1line: this is the second linelns 3, line 1, col 9, mode 1lns 3, line 1, col 9, mode 1line: this is the second linelns 3, line 1, col 8, mode 1lns 3, line 1, col 8, mode 1line: this is the second linelns 3, line 1, col 7, mode 1lns 3, line 1, col 7, mode 1line: this is the second linelns 3, line 1, col 6, mode 1lns 3, line 1, col 6, mode 1line: this is the second linelns 3, line 1, col 5, mode 1lns 3, line 1, col 5, mode 1line: this is the second linelns 3, line 1, col 4, mode 1lns 3, line 1, col 4, mode 1line: this is the second linelns 3, line 1, col 3, mode 1lns 3, line 1, col 3, mode 1line: this is the second linelns 3, line 1, col 2, mode 1lns 3, line 1, col 2, mode 1line: this is the second linelns 3, line 1, col 1, mode 1lns 3, line 1, col 1, mode 1line: this is the second linelns 3, line 1, col 0, mode 1lns 3, line 1, col 0, mode 1line: this is the second linegoing off left sidelns 3, line 0, col 25, mode 1lns 3, line 0, col 25, mode 1line: this is some text to editlns 3, line 0, col 24, mode 1lns 3, line 0, col 24, mode 1line: this is some text to editlns 3, line 0, col 23, mode 1lns 3, line 0, col 23, mode 1line: this is some text to editlns 3, line 0, col 22, mode 1lns 3, line 0, col 22, mode 1line: this is some text to editlns 3, line 0, col 21, mode 1lns 3, line 0, col 21, mode 1line: this is some text to editlns 3, line 0, col 20, mode 1lns 3, line 0, col 20, mode 1line: this is some text to editlns 3, line 0, col 19, mode 1lns 3, line 0, col 19, mode 1line: this is some text to editlns 3, line 0, col 18, mode 1lns 3, line 0, col 18, mode 1line: this is some text to editlns 3, line 0, col 17, mode 1lns 3, line 0, col 17, mode 1line: this is some textš to editlns 3, line 0, col 18, mode 1lns 3, line 0, col 18, mode 1line: this is some textšš to editlns 3, line 0, col 19, mode 1 \ No newline at end of file +buffer full +size: 25 +size: 23 +size: 25 +size: 0 +ok!buffer full +size: 25 +size: 23 +size: 30 +size: 4 +size: 0 +ok!buffer full +size: 25 +size: 23 +size: 30 +size: 4 +size: 1 +size: 0 +ok! \ No newline at end of file diff --git a/final/main.c b/final/main.c index a0c3f25..9036838 100644 --- a/final/main.c +++ b/final/main.c @@ -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); diff --git a/prototype/src/main.rs b/prototype/src/main.rs index 118f743..299f890 100644 --- a/prototype/src/main.rs +++ b/prototype/src/main.rs @@ -223,6 +223,8 @@ fn open(filename: &str) -> Result<(), &'static str> { ncurses::mv(0, 0); + + loop { ncurses::refresh(); let keystroke = ncurses::getch();