Files
FantasyPvP 00042ea092 done
2024-12-05 02:09:18 +00:00

519 lines
15 KiB
C

#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))
#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;
uint32_t x_offset;
bool unsaved_changes;
bool editmode;
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) {
int max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
attron(COLOR_PAIR(2));
// add an entry for the current mode
move(max_y - 1, 0);
char mode[9];
snprintf(mode, 9, "[%6s]", self->editmode ? "Insert" : "Normal");
addstr(mode);
// add an entry for the current line and col
char line_and_col[24];
snprintf(line_and_col, 24, " [ln: %d, col: %d]", self->buffer_line + 1, self->buffer_col + 1);
addstr(line_and_col);
attroff(COLOR_PAIR(2));
// add an entry for unsaved changes
attron(COLOR_PAIR(1));
char unsaved_changes[20];
snprintf(unsaved_changes, 20, " [%s]", self->unsaved_changes ? "Unsaved Changes!" : "No Changes Yet! ");
addstr(unsaved_changes);
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);
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, "%-4d]", i + 1);
addstr(line_no);
attroff(COLOR_PAIR(1));
// 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
);
char* data = to_chars(&line_segment);
addstr(data);
str_dealloc(&line_segment);
free(data);
}
}
}
add_toolbar(self);
refresh_cursor(self);
}
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 + self->x_offset) {
self->x_offset = x - max_x + 6;
offset_changed = true;
}
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 + self->y_offset) {
self->y_offset = y - max_y + 1;
offset_changed = true;
}
if (y < self->y_offset) {
self->y_offset = y;
offset_changed = true;
}
if (offset_changed) {
refresh_buffer(self);
} else {
refresh_cursor(self);
}
}
void switch_mode(Editor* self) {
self->editmode = !self->editmode;
refresh_buffer(self);
}
void newline(Editor* self) {
move(self->buffer_line, 0);
char line_no[6];
snprintf(line_no, 6, "%-5d", self->buffer_line + 1);
// add the line number
attron(COLOR_PAIR(1));
addstr(line_no);
attroff(COLOR_PAIR(1));
move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
}
// instantiates a new editor object given an input string and a filename
Editor editor_from(String_t input_string, String_t filename) {
Editor e;
e.lines = 0;
e.buffer_line = 0;
e.buffer_col = 0;
e.y_offset = 0;
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.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);
return e;
}
// creates a new editor object from a filename with an emptry buffer
Editor new_editor(String_t filename) {
return editor_from(str_new(), filename);
}
// prints the changelog for the editor.
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];
// for each token in the state, we print the corresponding line if there is a change.
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);
}
// saves the current buffer to a file
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
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;
}
}
// debug method
// 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);
self->original_len = self->lines;
snprintf(log_message, 64, "Edited file [%s]\n", to_chars(&self->filename));
str_push_str(&log_string, log_message);
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;
}
// move the cursor around the screen
// this uses extensive bounds checking to ensure that the cursor moves to the next line correctly
void move_cursor(Editor* self, int x, int y) {
if (y != 0
&& (int)(self->buffer_line) + y >= 0
&& self->buffer_line + y < self->lines)
{
// move the cursor up or down
self->buffer_line += y;
int line_width = str_len(&self->buffer[self->buffer_line]);
if (self->buffer_col > line_width) {
self->buffer_col = line_width;
}
} else if ((int)(self->buffer_col) + x < 0) {
// moving the cursor off the left hand side of the line
if ((int)self->buffer_line > 0) {
self->buffer_line -= 1;
self->buffer_col = str_len(&self->buffer[self->buffer_line]);
}
} 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;
}
}
move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
}
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;
}
// 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(&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);
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);
} 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();
}
}
void pressed_enter(Editor* self) {
self->unsaved_changes = true;
// allocate memory immediately since we know a new line is being added
self->lines++;
while (self->lines > self->capacity) {
self->capacity++;
}
self->buffer = (String_t*)realloc(self->buffer, sizeof(String_t) * (self->capacity));
if (self->buffer_col == 0) {
arr_insert(&self->diff, self->buffer_line, ADDED);
} else if (self->buffer_col == str_len(&self->buffer[self->buffer_line])) {
arr_insert(&self->diff, self->buffer_line + 1, ADDED);
} else {
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();
} else {
// else shift each line downwards including everything past the cursor on the current line
for (size_t i = self->lines - 1; i > self->buffer_line + 1; i--) {
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,
str_len(&self->buffer[self->buffer_line])
);
self->buffer[self->buffer_line] = str_slice(
&self->buffer[self->buffer_line],
0,
self->buffer_col
);
str_dealloc(&old_line);
}
// refresh the screen with the new data
refresh_buffer(self);
move_cursor(self, 1, 0);
}
// inserts a character at the cursor
void addchar(Editor* self, char c) {
self->unsaved_changes = true;
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
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();
} else {
if (arr_get(&self->diff, self->buffer_line) == UNMODIFIED) {
arr_set(&self->diff, self->buffer_line, MODIFIED);
}
}
insch(c);
// insert the character into the string at the given index
str_insert(&self->buffer[self->buffer_line], self->buffer_col, c);
move_cursor(self, 1, 0);
}
// closes the editor and ensures that all used resources are deallocated
void shutdown_editor(Editor* self) {
for (size_t i = 0; i < self->capacity; i++) {
str_dealloc(&self->buffer[i]);
}
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);
}