summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-27 10:09:52 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-27 10:09:52 -0300
commita8b879b9e91d29eb8d3172a445b7501ffd3a83c2 (patch)
treee6fffe37fb3fefd3a86cf2957f23f2c940e25852
parent85b84f23d22a84f9961a66beed99df489478dd86 (diff)
downloadjet-a8b879b9e91d29eb8d3172a445b7501ffd3a83c2.tar.gz
jet-a8b879b9e91d29eb8d3172a445b7501ffd3a83c2.zip
Add basic editing capabilities
Cursor is now visible and can insert, delete and move across the buffer.
-rw-r--r--jet.c33
-rw-r--r--point.c4
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;
}