added documentation, almost done with code

This commit is contained in:
FantasyPvP
2024-12-03 09:47:13 +00:00
parent af08485d27
commit ad96bcc0c8
6 changed files with 619 additions and 807 deletions
+135 -62
View File
@@ -3,8 +3,12 @@
#include "dynstr.h"
#include <ncurses.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
typedef struct {
uint32_t lines;
uint32_t capacity;
uint32_t buffer_line;
uint32_t buffer_col;
uint32_t y_offset;
@@ -12,6 +16,8 @@ typedef struct {
bool unsaved_changes;
bool editmode;
String_t* buffer;
String_t* original;
String_t filename;
} Editor;
void add_toolbar(Editor* self) {
@@ -22,7 +28,7 @@ void add_toolbar(Editor* self) {
// add an entry for the current mode
move(max_y - 1, 0);
char mode[8];
char mode[9];
snprintf(mode, 9, "[%6s]", self->editmode ? "Insert" : "Normal");
addstr(mode);
@@ -40,80 +46,94 @@ void add_toolbar(Editor* self) {
attroff(COLOR_PAIR(1));
}
#define min(x, y) ((x) < (y) ? (x) : (y))
void refresh_buffer(Editor* self) {
move(0, 0);
clear();
// get the screen size so we can figure out where to place characters
int max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
max_y--; // Reserve bottom line for toolbar
// Ensure y_offset is valid
if (self->buffer_line >= self->y_offset + max_y) {
self->y_offset = self->buffer_line - max_y + 1;
}
if (self->buffer_line < self->y_offset) {
self->y_offset = self->buffer_line;
}
// Display visible lines
for (size_t i = self->y_offset; i < self->lines && i < self->y_offset + max_y; i++) {
move(i - self->y_offset, 0);
// adding the line number
attron(COLOR_PAIR(1));
char line_no[6];
snprintf(line_no, 6, "%-6d", i + 1);
snprintf(line_no, 6, "%-4d]", i + 1);
addstr(line_no);
attroff(COLOR_PAIR(1));
// refreshing the text for the line
String_t line_segment = str_slice(
&self->buffer[i],
self->x_offset,
min(str_len(&self->buffer[i]), self->x_offset + max_x - 5)
);
// Only try to display the line if it's within bounds
if (i < self->lines) {
// Calculate visible portion of line
int visible_start = self->x_offset;
int line_len = str_len(&self->buffer[i]);
int visible_end = min(line_len, self->x_offset + max_x - 5);
if (visible_start < line_len) {
String_t line_segment = str_slice(
&self->buffer[i],
visible_start,
visible_end
);
addstr(to_chars(&line_segment));
str_dealloc(&line_segment);
addstr("\n");
char* data = to_chars(&line_segment);
addstr(data);
str_dealloc(&line_segment);
free(data);
}
}
}
add_toolbar(self);
move(self->buffer_line - self->y_offset, self->buffer_col + 5 - self->x_offset);
// Ensure cursor position is valid
int cursor_y = self->buffer_line - self->y_offset;
int cursor_x = self->buffer_col - self->x_offset + 5;
// Clamp cursor position to screen bounds
cursor_y = min(max(cursor_y, 0), max_y - 1);
cursor_x = min(max(cursor_x, 5), max_x - 1);
move(cursor_y, cursor_x);
}
void move_cursor_on_screen(Editor* editor, int x, int y) {
add_toolbar(editor);
int newx = x + 5; // account for the line number
int max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
max_y--; // Reserve bottom line for toolbar
max_x -= 5; // Account for line number margin
bool flag = false;
while (newx + 3 > max_x + editor->x_offset) {
editor->x_offset += 1;
flag = true;
// Adjust x_offset if cursor would be off screen
if (x + 5 >= max_x + editor->x_offset) {
editor->x_offset = x - max_x + 6;
}
if (x < editor->x_offset) {
editor->x_offset = x;
}
while (newx - 3 < editor->x_offset) {
editor->x_offset -= 1;
flag = true;
// Adjust y_offset if cursor would be off screen
if (y >= max_y + editor->y_offset) {
editor->y_offset = y - max_y + 1;
}
if (y < editor->y_offset) {
editor->y_offset = y;
}
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);
refresh_buffer(editor);
}
void switch_mode(Editor* self) {
@@ -125,7 +145,6 @@ void newline(Editor* self) {
move(self->buffer_line, 0);
char line_no[6];
snprintf(line_no, 6, "%-6d", self->buffer_line + 1);
fprintf(stderr, "ln: %d, col: %d", self->buffer_line, self->buffer_col);
// add the line number
attron(COLOR_PAIR(1));
@@ -134,7 +153,7 @@ void newline(Editor* self) {
move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
}
Editor editor_from(String_t input_string) {
Editor editor_from(String_t input_string, String_t filename) {
Editor e;
e.lines = 0;
e.buffer_line = 0;
@@ -143,24 +162,46 @@ Editor editor_from(String_t input_string) {
e.x_offset = 0;
e.editmode = false;
e.unsaved_changes = false;
e.filename = filename;
int linenum = 0;
e.buffer = str_lines(&input_string, &linenum);
e.lines = (size_t)linenum;
e.lines = (uint32_t)linenum;
e.capacity = e.lines;
str_dealloc(&input_string);
refresh_buffer(&e);
return e;
}
Editor new_editor() {
return editor_from(str_new());
Editor new_editor(String_t filename) {
return editor_from(str_new(), filename);
}
int save_file(Editor* self) {
char* name = to_chars(&self->filename);
// writes the content line by line to the file
FILE* file = fopen(name, "w");
for (size_t i = 0; i < self->lines; i++) {
if (str_to_file(&self->buffer[i], file) == -1) {
free(name);
fprintf(stderr, "Error writing to file\n");
return -1;
}
}
free(name);
fclose(file);
return 0;
}
void move_cursor(Editor* self, int x, int y) {
if (y != 0
&& (int)(self->buffer_line) + y >= 0
&& self->buffer_line + y <= self->lines)
&& self->buffer_line + y < self->lines)
{
// move the cursor up or down
self->buffer_line += y;
@@ -174,17 +215,19 @@ void move_cursor(Editor* self, int x, int y) {
self->buffer_line -= 1;
self->buffer_col = str_len(&self->buffer[self->buffer_line]);
}
} else if (self->buffer_col + x > str_len(&self->buffer[self->buffer_line])) {
// moving the cursor off the right hand side of the line
if (self->buffer_line < self->lines) {
self->buffer_col = 0;
self->buffer_line += 1;
} else if (x != 0 && self->buffer_line < self->lines) {
int line_width = str_len(&self->buffer[self->buffer_line]);
if (self->buffer_col + x > line_width) {
// moving the cursor off the right hand side of the line
if (self->buffer_line + 1 < self->lines) {
self->buffer_col = 0;
self->buffer_line += 1;
}
} else {
// moving the cursor left or right in any other case
self->buffer_col += x;
}
} else if (x != 0) {
// 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);
}
@@ -197,12 +240,22 @@ void delchar(Editor* self) {
return;
}
// obtain string objects that reference the old lines.
String_t old_curr_line = self->buffer[self->buffer_line];
String_t old_next_line = self->buffer[self->buffer_line + 1];
// assign the new line
self->buffer[self->buffer_line] = str_merge(&self->buffer[self->buffer_line], &self->buffer[self->buffer_line + 1]);
// deallocate the memory for the old lines
str_dealloc(&old_curr_line);
str_dealloc(&old_next_line);
for (size_t i = self->buffer_line + 2; i < self->lines; i++) {
self->buffer[i - 1] = self->buffer[i];
}
// replace the last line
self->buffer[self->lines - 1] = str_new();
self->lines--;
refresh_buffer(self);
@@ -219,7 +272,11 @@ void pressed_enter(Editor* self) {
// allocate memory immediately since we know a new line is being added
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines));
while (self->lines > self->capacity) {
self->capacity++;
}
self->buffer = (String_t*)realloc(self->buffer, sizeof(String_t) * (self->capacity));
if (self->buffer_line == self->lines -1) {
// if at end of file add an empty line
@@ -230,6 +287,8 @@ void pressed_enter(Editor* self) {
self->buffer[i] = self->buffer[i - 1];
}
String_t old_line = self->buffer[self->buffer_line];
self->buffer[self->buffer_line + 1] = str_slice(
&self->buffer[self->buffer_line],
self->buffer_col,
@@ -241,6 +300,8 @@ void pressed_enter(Editor* self) {
0,
self->buffer_col
);
str_dealloc(&old_line);
}
// refresh the screen with the new data
refresh_buffer(self);
@@ -257,7 +318,12 @@ void addchar(Editor* self, char c) {
// reallocate self->buffer to be 1 larger
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines));
while (self->lines > self->capacity) {
self->capacity++;
}
self->buffer = (String_t*)realloc(self->buffer, sizeof(String_t) * (self->capacity));
// allocate the memory space for the new line and add it to the buffer
self->buffer[self->buffer_line] = str_new();
@@ -269,7 +335,14 @@ void addchar(Editor* self, char c) {
move_cursor(self, 1, 0);
}
void shutdown_editor(Editor* self) {
for (size_t i = 0; i < self->capacity; i++) {
str_dealloc(&self->buffer[i]);
}
str_dealloc(&self->filename);
free(self->buffer);
}
String_t* to_string(Editor* self) {
}