pretty much working, need to fix horizontal scrolling & file saving

This commit is contained in:
FantasyPvP
2024-11-09 17:29:01 +00:00
parent c4d7da083a
commit 380f3e1ff7
4 changed files with 308 additions and 228 deletions
+50 -6
View File
@@ -23,6 +23,8 @@ void add_toolbar(Editor* self) {
addstr(mode);
}
#define min(x, y) ((x) < (y) ? (x) : (y))
void refresh_buffer(Editor* self) {
move(0, 0);
@@ -36,24 +38,62 @@ void refresh_buffer(Editor* self) {
for (size_t i = self->y_offset; i < self->lines && i < self->y_offset + max_y; i++) {
// for (size_t i = 0; i < self->lines; i++) {
// adding the line number
char line_no[5];
char line_no[6];
snprintf(line_no, 5, "%-5d", i + 1);
// adding the line of text to the buffer
addstr(line_no);
addch(' ');
addstr(to_chars(&self->buffer[i]));
String_t line_segment = str_slice(
&self->buffer[i],
self->x_offset,
min(str_len(&self->buffer[i]), self->x_offset + max_x - 5)
);
addstr(to_chars(&line_segment));
str_dealloc(&line_segment);
addstr("\n");
}
add_toolbar(self);
move(self->buffer_line, self->buffer_col + 5);
move(self->buffer_line - self->y_offset, self->buffer_col - self->x_offset + 5);
}
void move_cursor_on_screen(Editor* editor, int x, int y) {
int newx = x + 5; // account for the line number
move(y, newx);
int max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
bool flag = false;
while (newx + 3 > max_x + editor->x_offset) {
editor->x_offset += 1;
flag = true;
}
while (newx - 3 < editor->x_offset) {
editor->x_offset -= 1;
flag = true;
}
while (y + 3 > max_y + editor->y_offset) {
editor->y_offset += 1;
flag = true;
}
while (y - 3 < editor->y_offset) {
editor->y_offset -= 1;
flag = true;
}
if (flag) {
refresh_buffer(editor);
}
move(y - editor->y_offset, newx - editor->x_offset);
}
void switch_mode(Editor* self) {
@@ -63,12 +103,15 @@ void switch_mode(Editor* self) {
void newline(Editor* self) {
move(self->buffer_line, 0);
char line_no[5];
char line_no[6];
snprintf(line_no, 5, "%-5d", self->buffer_line + 1);
fprintf(stderr, "ln: %d, col: %d", self->buffer_line, self->buffer_col);
// adding the line of text to the buffer
addstr(line_no);
addch(' ');
move(self->buffer_line, self->buffer_col + 5);
move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
}
Editor editor_from(String_t input_string) {
@@ -120,6 +163,7 @@ void move_cursor(Editor* self, int x, int y) {
// moving the cursor left or right in any other case
self->buffer_col += x;
}
fprintf(stderr, "ln: %d, col: %d xoff: %d yoff: %d\n", self->buffer_line, self->buffer_col, self->x_offset, self->y_offset);
move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
}