From a8b879b9e91d29eb8d3172a445b7501ffd3a83c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Mon, 27 Apr 2020 10:09:52 -0300 Subject: Add basic editing capabilities Cursor is now visible and can insert, delete and move across the buffer. --- jet.c | 33 +++++++++++++++++++++++++-------- point.c | 4 ++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/jet.c b/jet.c index 290b95e..94804fd 100644 --- a/jet.c +++ b/jet.c @@ -25,12 +25,13 @@ int main(int argc, char *argv[]) { while ((c = fgetc(f)) != EOF) { insert_at_point(&cursor, c); } + cursor.page = page; + cursor.index = 0; fclose(f); } int window_height = getmaxy(stdscr); int current_line = 0; - int old_line = -1; int number_of_lines = 0; for (struct point i = {page, 0}; !at_eof(&i); move_point_forward(&i)) { @@ -38,17 +39,21 @@ int main(int argc, char *argv[]) { } while (1) { - if (old_line != current_line) { - clear(); + clear(); - int iter_line = 0; - for (struct point i = {page, 0}; !at_eof(&i) && iter_line < window_height + current_line; move_point_forward(&i)) { - if (iter_line >= current_line) addch(element(&i)); - if (element(&i) == '\n') iter_line++; + int x = -1, y = -1; + int iter_line = 0; + for (struct point i = {page, 0}; !at_eof(&i) && iter_line < window_height + current_line; move_point_forward(&i)) { + if (same_location(&i, &cursor)) { + getyx(stdscr, y, x); } + if (iter_line >= current_line) addch(element(&i)); + if (element(&i) == '\n') iter_line++; + } + if (x > -1 && y > -1) { + move(y, x); } - old_line = current_line; int input = getch(); switch (input) { @@ -57,6 +62,18 @@ int main(int argc, char *argv[]) { break; case KEY_DOWN: if (current_line < number_of_lines - window_height) current_line++; + break; + case KEY_LEFT: + move_point_backward(&cursor); + break; + case KEY_RIGHT: + move_point_forward(&cursor); + break; + case KEY_BACKSPACE: + delete_at_point(&cursor); + break; + default: + insert_at_point(&cursor, input); } } diff --git a/point.c b/point.c index 23ce492..8ef0c41 100644 --- a/point.c +++ b/point.c @@ -22,6 +22,10 @@ uint8_t element(struct point *point) { } } +bool same_location(struct point *a, struct point *b) { + return a->page == b->page && a->index == b->index; +} + bool at_eof(struct point *point) { return point->index == point->page->element_count && !point->page->next; } -- cgit v1.2.3