added colours for line numbers, improved toolbar

This commit is contained in:
FantasyPvP
2024-11-10 00:40:57 +00:00
parent 380f3e1ff7
commit 6573da5f43
6 changed files with 647 additions and 241 deletions
+39 -12
View File
@@ -9,6 +9,7 @@ typedef struct {
uint32_t buffer_col;
uint32_t y_offset;
uint32_t x_offset;
bool unsaved_changes;
bool editmode;
String_t* buffer;
} Editor;
@@ -17,10 +18,26 @@ 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[8];
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));
}
#define min(x, y) ((x) < (y) ? (x) : (y))
@@ -36,14 +53,15 @@ void refresh_buffer(Editor* self) {
getmaxyx(stdscr, max_y, max_x);
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[6];
snprintf(line_no, 5, "%-5d", i + 1);
// adding the line of text to the buffer
addstr(line_no);
addch(' ');
attron(COLOR_PAIR(1));
char line_no[6];
snprintf(line_no, 6, "%-6d", 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,
@@ -52,16 +70,18 @@ void refresh_buffer(Editor* self) {
addstr(to_chars(&line_segment));
str_dealloc(&line_segment);
addstr("\n");
}
add_toolbar(self);
move(self->buffer_line - self->y_offset, self->buffer_col - self->x_offset + 5);
move(self->buffer_line - self->y_offset, self->buffer_col + 5 - self->x_offset);
}
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;
@@ -104,13 +124,13 @@ void switch_mode(Editor* self) {
void newline(Editor* self) {
move(self->buffer_line, 0);
char line_no[6];
snprintf(line_no, 5, "%-5d", self->buffer_line + 1);
snprintf(line_no, 6, "%-6d", self->buffer_line + 1);
fprintf(stderr, "ln: %d, col: %d", self->buffer_line, self->buffer_col);
// adding the line of text to the buffer
// add the line number
attron(COLOR_PAIR(1));
addstr(line_no);
addch(' ');
attroff(COLOR_PAIR(1));
move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
}
@@ -122,6 +142,7 @@ Editor editor_from(String_t input_string) {
e.y_offset = 0;
e.x_offset = 0;
e.editmode = false;
e.unsaved_changes = false;
int linenum = 0;
e.buffer = str_lines(&input_string, &linenum);
@@ -168,6 +189,8 @@ void move_cursor(Editor* self, int x, int y) {
}
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) {
@@ -192,6 +215,8 @@ void delchar(Editor* self) {
}
void pressed_enter(Editor* self) {
self->unsaved_changes = true;
// allocate memory immediately since we know a new line is being added
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines));
@@ -224,6 +249,8 @@ void pressed_enter(Editor* self) {
// 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
newline(self);