diff --git a/final/binary b/final/binary index 9e656ce..62a1f15 100755 Binary files a/final/binary and b/final/binary differ diff --git a/final/dynstr.c b/final/dynstr.c index d535986..6d28f07 100644 --- a/final/dynstr.c +++ b/final/dynstr.c @@ -214,7 +214,13 @@ char* to_chars(String_t* s) { // printf("%s\n", to_chars(&s)); +// int linen = 0; +// String_t* lines = str_lines(&s, &linen); +// for (int i=0; i < linen; i++) { +// printf("%s", to_chars(&lines[i])); +// } + // str_dealloc(&s); // return 0; -// } \ No newline at end of file +// } diff --git a/final/editor.c b/final/editor.c index 7a91c46..dd8bd48 100644 --- a/final/editor.c +++ b/final/editor.c @@ -1,13 +1,12 @@ #include #include -#include #include "dynstr.h" #include typedef struct { - int lines; - int screen_line; - int screen_col; + uint32_t lines; + uint32_t screen_line; + uint32_t screen_col; bool editmode; String_t* buffer; } Editor; @@ -20,7 +19,9 @@ Editor new_editor() { e.editmode = false; String_t lines = str_from_chars(""); - e.buffer = str_lines(&lines, &e.lines); + int linenum = 0; + e.buffer = str_lines(&lines, &linenum); + e.lines = linenum; str_dealloc(&lines); addstr(""); @@ -29,25 +30,39 @@ Editor new_editor() { } Editor editor_from(char* input_string) { - // TODO: fix this function Editor e; e.lines = 0; e.screen_line = 0; e.screen_col = 0; e.editmode = false; - - String_t lines = str_from_chars(input_string); - e.buffer = str_lines(&lines, &e.lines); - str_dealloc(&lines); + int linenum = 0; + 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); return e; } 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 - && self->screen_line + y >= 0 + && (int)(self->screen_line) + y >= 0 && self->screen_line + y <= self->lines) { self->screen_line += y; @@ -55,7 +70,8 @@ void move_cursor(Editor* self, int x, int y) { if (self->screen_col > line_width) { self->screen_col = line_width; } - } else if (self->screen_col + x < 0) { + } else if ((int)(self->screen_col) + x < 0) { + fprintf(stderr, "going off left side"); if (self->screen_line - 1 >= 0) { self->screen_line -= 1; self->screen_col = str_len(&self->buffer[self->screen_line]); @@ -67,8 +83,8 @@ 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); } diff --git a/final/editor.h b/final/editor.h index 5e678dd..727bceb 100644 --- a/final/editor.h +++ b/final/editor.h @@ -1,8 +1,10 @@ #include "dynstr.h" +#include typedef struct { - int screen_line; - int screen_col; + uint32_t lines; + uint32_t screen_line; + uint32_t screen_col; bool editmode; String_t* buffer; } Editor; @@ -15,4 +17,4 @@ void move_cursor(Editor* self, int x, int y); void delchar(Editor* self); void addchar(Editor* self, char c); -String_t* to_string(Editor* self); \ No newline at end of file +String_t* to_string(Editor* self); diff --git a/final/log.txt b/final/log.txt new file mode 100644 index 0000000..d3f768c --- /dev/null +++ b/final/log.txt @@ -0,0 +1 @@ +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 diff --git a/final/main.c b/final/main.c index 0111961..a0c3f25 100644 --- a/final/main.c +++ b/final/main.c @@ -26,10 +26,12 @@ int open_editor() { 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); + while (true) { refresh(); int c = getch(); - if (editor.editmode) { + if (editor.editmode == true) { switch (c) { case 27: editor.editmode = false; diff --git a/prototype/src/main.rs b/prototype/src/main.rs index 281db9b..118f743 100644 --- a/prototype/src/main.rs +++ b/prototype/src/main.rs @@ -7,7 +7,6 @@ use std::{ io::Write, }; - fn somefunc() { let c = "hello"; @@ -16,7 +15,6 @@ fn somefunc() { let c2 = String::from("hello world"); } - struct EditorData { buffer: Vec>, // outer vec is the line, inner is col; file_line: i32, @@ -60,16 +58,30 @@ impl EditorData { && self.screen_line + dy <= self.buffer.len() as i32 { self.screen_line += dy; - let line_width = self.buffer.get(self.screen_line as usize).unwrap_or(&Vec::::new()).len() as i32; + let line_width = self + .buffer + .get(self.screen_line as usize) + .unwrap_or(&Vec::::new()) + .len() as i32; if self.screen_col > line_width { self.screen_col = line_width; } } else if self.screen_col + dx < 0 { if self.screen_line - 1 >= 0 { self.screen_line -= 1; - self.screen_col = self.buffer.get(self.screen_line as usize).unwrap_or(&Vec::::new()).len() as i32; + self.screen_col = self + .buffer + .get(self.screen_line as usize) + .unwrap_or(&Vec::::new()) + .len() as i32; } - } else if self.screen_col + dx > self.buffer.get(self.screen_line as usize).unwrap_or(&Vec::::new()).len() as i32 { + } else if self.screen_col + dx + > self + .buffer + .get(self.screen_line as usize) + .unwrap_or(&Vec::::new()) + .len() as i32 + { if self.screen_line + 1 <= self.buffer.len() as i32 { self.screen_col = 0; self.screen_line += 1; @@ -107,7 +119,10 @@ impl EditorData { line.insert(self.screen_col as usize, c); } else { self.buffer.push(Vec::new()); - self.buffer.get_mut(self.screen_line as usize).unwrap().push(c); + self.buffer + .get_mut(self.screen_line as usize) + .unwrap() + .push(c); } self.mv_cursor(0, 1); } @@ -226,9 +241,7 @@ fn open(filename: &str) -> Result<(), &'static str> { ncurses::KEY_RIGHT => data.mv_cursor(0, 1), ncurses::KEY_UP => data.mv_cursor(-1, 0), ncurses::KEY_DOWN => data.mv_cursor(1, 0), - ncurses::KEY_ENTER => { - - } + ncurses::KEY_ENTER => {} _ => { data.addchar(char::from_u32(keystroke as u32).unwrap()); } @@ -240,7 +253,6 @@ fn open(filename: &str) -> Result<(), &'static str> { } 119 => { let buff_size = max_y * max_x; - let buff = data.to_string();