started working on scrolling, enter key works now

This commit is contained in:
FantasyPvP
2024-11-08 10:36:52 +00:00
parent be32741af2
commit e4d6a971c1
9 changed files with 427 additions and 83 deletions
+31 -8
View File
@@ -22,16 +22,40 @@ int open_editor() {
int max_y, max_x;
getmaxyx(stdscr, max_y, max_x);
Editor editor = editor_from("this is some text to edit\nthis is the second line\nand this is the third lol");
move(0, 5);
FILE* file = fopen("somecode.c", "r");
if (file == NULL) {
endwin();
printf("Failed to open file\n");
return 1;
}
String_t text = str_new();
// read from file char by char
while (true) {
char line[1024];
if (fgets(line, 1024, file) == NULL) {
break;
}
String_t new_text = str_from_chars(line);
text = str_merge(&text, &new_text);
str_dealloc(&new_text);
}
fclose(file);
Editor editor = editor_from(text);
move(0, 5);
while (true) {
refresh();
int c = getch();
if (editor.editmode == true) {
switch (c) {
case 27:
editor.editmode = false;
switch_mode(&editor);
break;
case KEY_BACKSPACE:
move_cursor(&editor, -1, 0);
@@ -40,10 +64,8 @@ int open_editor() {
case KEY_DC:
delchar(&editor);
break;
case KEY_ENTER:
//pressed_enter(&editor);
move_cursor(&editor, 1, 0);
//TODO: next line
case '\n':
pressed_enter(&editor);
break;
case KEY_UP:
move_cursor(&editor, 0, -1);
@@ -58,6 +80,7 @@ int open_editor() {
move_cursor(&editor, 1, 0);
break;
default:
fprintf(stderr, "%d\n", c);
addchar(&editor, c);
break;
}
@@ -67,7 +90,7 @@ int open_editor() {
endwin();
return 0;
case 'i':
editor.editmode = true;
switch_mode(&editor);
break;
case 'w':
// TODO: write function to save the data to a file