logging works ( but it's buggy 😭 )

This commit is contained in:
FantasyPvP
2024-12-05 01:42:50 +00:00
parent ad8496ca68
commit 3f35a00526
12 changed files with 786 additions and 44 deletions
+191 -30
View File
@@ -1,6 +1,8 @@
#include <stdbool.h>
#include <stdlib.h>
#include "dynstr.h"
#include "log.h"
#include "state.h"
#include <ncurses.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
@@ -15,9 +17,13 @@ typedef struct {
uint32_t x_offset;
bool unsaved_changes;
bool editmode;
String_t* buffer;
String_t* original;
String_t filename;
String_t* buffer;
// variables needed for the changelog functionality
int original_len; // length of the original buffer
String_t* original; // original buffer
StateArray diff; // changelog
} Editor;
void add_toolbar(Editor* self) {
@@ -46,6 +52,23 @@ void add_toolbar(Editor* self) {
attroff(COLOR_PAIR(1));
}
void refresh_cursor(Editor* self) {
int max_x,max_y;
getmaxyx(stdscr, max_y, max_x);
// Clamp cursor position to screen bounds
int cursor_y = min(
max(self->buffer_line - self->y_offset, 0),
max_y - 1
);
int cursor_x = min(
max(self->buffer_col - self->x_offset + 5, 5),
max_x - 1
);
move(cursor_y, cursor_x);
}
void refresh_buffer(Editor* self) {
move(0, 0);
@@ -99,41 +122,42 @@ void refresh_buffer(Editor* self) {
}
add_toolbar(self);
refresh_cursor(self);
}
// 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) {
void move_cursor_on_screen(Editor* self, int x, int y) {
bool offset_changed = false;
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
// Adjust x_offset if cursor would be off screen
if (x + 5 >= max_x + editor->x_offset) {
editor->x_offset = x - max_x + 6;
// Adjust x_offset if cursor would be off screen
if (x + 5 >= max_x + self->x_offset) {
self->x_offset = x - max_x + 6;
offset_changed = true;
}
if (x < editor->x_offset) {
editor->x_offset = x;
if (x < self->x_offset) {
self->x_offset = x;
offset_changed = 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 >= max_y + self->y_offset) {
self->y_offset = y - max_y + 1;
offset_changed = true;
}
if (y < editor->y_offset) {
editor->y_offset = y;
if (y < self->y_offset) {
self->y_offset = y;
offset_changed = true;
}
if (offset_changed) {
refresh_buffer(self);
} else {
refresh_cursor(self);
}
refresh_buffer(editor);
}
void switch_mode(Editor* self) {
@@ -144,7 +168,7 @@ void switch_mode(Editor* self) {
void newline(Editor* self) {
move(self->buffer_line, 0);
char line_no[6];
snprintf(line_no, 6, "%-6d", self->buffer_line + 1);
snprintf(line_no, 6, "%-5d", self->buffer_line + 1);
// add the line number
attron(COLOR_PAIR(1));
@@ -166,9 +190,13 @@ Editor editor_from(String_t input_string, String_t filename) {
int linenum = 0;
e.buffer = str_lines(&input_string, &linenum);
e.original = str_clone_all(e.buffer, linenum);
e.lines = (uint32_t)linenum;
e.original_len = linenum;
e.capacity = e.lines;
e.diff = arr_init(e.lines);
str_dealloc(&input_string);
refresh_buffer(&e);
@@ -179,22 +207,129 @@ Editor new_editor(String_t filename) {
return editor_from(str_new(), filename);
}
int print_diff(Editor* self) {
char buffer[512];
String_t log_string = str_new();
int original_offset;
int modified_offset;
for (size_t i = 0; i < self->diff.size; i++) {
LineState state = self->diff.data[i];
switch (state) {
// case UNMODIFIED:
// original_offset = arr_original_offset(&self->diff, i);
// snprintf(
// buffer,
// 512,
// "UNMODIFIED %s \n",
// to_chars(&self->buffer[original_offset + i])
// );
// str_push_str(&log_string, buffer);
// break;
case MODIFIED:
original_offset = arr_original_offset(&self->diff, i);
modified_offset = arr_modified_offset(&self->diff, i);
snprintf(
buffer,
512,
"MODIFIED %-6d | %s\n -> %-6d | %s \n",
original_offset + i + 1,
to_chars(&self->original[original_offset + i]),
modified_offset + i + 1,
to_chars(&self->buffer[modified_offset + i])
);
str_push_str(&log_string, buffer);
break;
case REMOVED:
original_offset = arr_original_offset(&self->diff, i);
snprintf(
buffer,
512,
"REMOVED %-6d | %s \n",
original_offset + i + 1,
to_chars(&self->original[original_offset + i])
);
str_push_str(&log_string, buffer);
break;
case ADDED:
modified_offset = arr_modified_offset(&self->diff, i);
snprintf(
buffer,
512,
"ADDED %-6d | %s \n",
modified_offset + i + 1,
to_chars(&self->buffer[modified_offset + i])
);
str_push_str(&log_string, buffer);
break;
default:
break;
}
}
if (write_log(&log_string) == -1) {
fprintf(stderr, "Error writing log\n");
return -1;
}
str_dealloc(&log_string);
}
int save_file(Editor* self) {
String_t log_string = str_new();
char log_message[64];
char* name = to_chars(&self->filename);
FILE* file = fopen(name, "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file %s\n", name);
return -1;
}
// 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;
}
}
arr_print(&self->diff);
print_diff(self);
free(name);
fclose(file);
for (size_t i = 0; i < self->original_len; i++) {
str_dealloc(&self->original[i]);
}
free(self->original);
self->original = str_clone_all(self->buffer, self->lines);
snprintf(log_message, 64, "Edited file [%s]\n", to_chars(&self->filename));
str_push_str(&log_string, log_message);
self->original_len = self->lines;
snprintf(log_message, 64, "Old Length: [%d] New Length: [%d]\n", self->original_len, self->lines);
str_push_str(&log_string, log_message);
// log messages
if (write_log(&log_string) == -1) {
fprintf(stderr, "Error writing log\n");
return -1;
}
printf("wrote log");
str_dealloc(&log_string);
arr_dealloc(&self->diff);
self->diff = arr_init(self->lines);
return 0;
}
@@ -235,7 +370,6 @@ void delchar(Editor* self) {
self->unsaved_changes = true;
if (self->buffer_col == str_len(&self->buffer[self->buffer_line])) {
if (self->buffer_line +1 == self->lines) {
return;
}
@@ -245,7 +379,13 @@ void delchar(Editor* self) {
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]);
self->buffer[self->buffer_line] = str_merge(&old_curr_line, &old_next_line);
if (arr_get(&self->diff, self->buffer_line + 1) == ADDED) {
arr_remove(&self->diff, self->buffer_line + 1);
} else {
arr_set(&self->diff, self->buffer_line + 1, REMOVED);
}
// deallocate the memory for the old lines
str_dealloc(&old_curr_line);
@@ -261,6 +401,8 @@ void delchar(Editor* self) {
refresh_buffer(self);
} else {
// removes the character from the current line
arr_set(&self->diff, self->buffer_line, MODIFIED);
str_remove(&self->buffer[self->buffer_line], self->buffer_col);
delch();
}
@@ -278,6 +420,9 @@ void pressed_enter(Editor* self) {
}
self->buffer = (String_t*)realloc(self->buffer, sizeof(String_t) * (self->capacity));
// logs the change of adding a new line
arr_insert(&self->diff, self->buffer_line + 1, ADDED);
if (self->buffer_line == self->lines -1) {
// if at end of file add an empty line
self->buffer[self->lines - 1] = str_new();
@@ -314,6 +459,12 @@ void addchar(Editor* self, char c) {
if (self->buffer_line == self->lines) {
// if we are at the end of the file then we need to add a new line
if (arr_get(&self->diff, self->buffer_line) == REMOVED) {
arr_set(&self->diff, self->buffer_line, MODIFIED);
} else {
arr_insert(&self->diff, self->buffer_line, ADDED);
}
newline(self);
// reallocate self->buffer to be 1 larger
@@ -327,6 +478,10 @@ void addchar(Editor* self, char c) {
// allocate the memory space for the new line and add it to the buffer
self->buffer[self->buffer_line] = str_new();
} else {
if (arr_get(&self->diff, self->buffer_line) == UNMODIFIED) {
arr_set(&self->diff, self->buffer_line, MODIFIED);
}
}
insch(c);
@@ -339,6 +494,12 @@ 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);
for (size_t i = 0; i < self->original_len; i++) {
str_dealloc(&self->original[i]);
}
free(self->original);
str_dealloc(&self->filename);
}