This commit is contained in:
FantasyPvP
2024-11-07 13:52:28 +00:00
parent b40ecc5bb1
commit 214dfc66a5
7 changed files with 68 additions and 29 deletions
BIN
View File
Binary file not shown.
+7 -1
View File
@@ -214,7 +214,13 @@ char* to_chars(String_t* s) {
// printf("%s\n", to_chars(&s));
// int linen = 0;
// String_t* lines = str_lines(&s, &linen);
// for (int i=0; i < linen; i++) {
// printf("%s", to_chars(&lines[i]));
// }
// str_dealloc(&s);
// return 0;
// }
// }
+30 -14
View File
@@ -1,13 +1,12 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "dynstr.h"
#include <ncurses.h>
typedef struct {
int lines;
int screen_line;
int screen_col;
uint32_t lines;
uint32_t screen_line;
uint32_t screen_col;
bool editmode;
String_t* buffer;
} Editor;
@@ -20,7 +19,9 @@ Editor new_editor() {
e.editmode = false;
String_t lines = str_from_chars("");
e.buffer = str_lines(&lines, &e.lines);
int linenum = 0;
e.buffer = str_lines(&lines, &linenum);
e.lines = linenum;
str_dealloc(&lines);
addstr("");
@@ -29,25 +30,39 @@ Editor new_editor() {
}
Editor editor_from(char* input_string) {
// TODO: fix this function
Editor e;
e.lines = 0;
e.screen_line = 0;
e.screen_col = 0;
e.editmode = false;
String_t lines = str_from_chars(input_string);
e.buffer = str_lines(&lines, &e.lines);
str_dealloc(&lines);
int linenum = 0;
String_t lines = str_from_chars(input_string);
e.buffer = str_lines(&lines, &linenum);
e.lines = (size_t)linenum;
fprintf(stderr, "BUFFER: %s", to_chars(&e.buffer[2]));
str_dealloc(&lines);
// String_t s = str_from_chars(input_string);
// int linen = 0;
// String_t* lines = str_lines(&s, &linen);
// for (int i=0; i < linen; i++) {
// fprintf(stderr, "%s", to_chars(&lines[i]));
// }
// e.lines = (size_t)linen;
// e.buffer = lines;
// str_dealloc(&s);
addstr(input_string);
return e;
}
void move_cursor(Editor* self, int x, int y) {
fprintf(stderr, "lns %d, line %d, col %d, mode %d", self->lines, self->screen_line, self->screen_col, self->editmode);
fprintf(stderr, "line: %s", to_chars(&self->buffer[self->screen_line]));
if (y != 0
&& self->screen_line + y >= 0
&& (int)(self->screen_line) + y >= 0
&& self->screen_line + y <= self->lines)
{
self->screen_line += y;
@@ -55,7 +70,8 @@ void move_cursor(Editor* self, int x, int y) {
if (self->screen_col > line_width) {
self->screen_col = line_width;
}
} else if (self->screen_col + x < 0) {
} else if ((int)(self->screen_col) + x < 0) {
fprintf(stderr, "going off left side");
if (self->screen_line - 1 >= 0) {
self->screen_line -= 1;
self->screen_col = str_len(&self->buffer[self->screen_line]);
@@ -67,8 +83,8 @@ void move_cursor(Editor* self, int x, int y) {
}
} else if (x != 0) {
self->screen_col += x;
}
}
fprintf(stderr, "lns %d, line %d, col %d, mode %d", self->lines, self->screen_line, self->screen_col, self->editmode);
move(self->screen_line, self->screen_col);
}
+5 -3
View File
@@ -1,8 +1,10 @@
#include "dynstr.h"
#include <stdint.h>
typedef struct {
int screen_line;
int screen_col;
uint32_t lines;
uint32_t screen_line;
uint32_t screen_col;
bool editmode;
String_t* buffer;
} Editor;
@@ -15,4 +17,4 @@ void move_cursor(Editor* self, int x, int y);
void delchar(Editor* self);
void addchar(Editor* self, char c);
String_t* to_string(Editor* self);
String_t* to_string(Editor* self);
+1
View File
File diff suppressed because one or more lines are too long
+3 -1
View File
@@ -26,10 +26,12 @@ int open_editor() {
Editor editor = editor_from("this is some text to edit\nthis is the second line\nand this is the third lol");
fprintf(stderr, "%d %d %d %d", editor.lines, editor.screen_line, editor.screen_col, editor.editmode);
while (true) {
refresh();
int c = getch();
if (editor.editmode) {
if (editor.editmode == true) {
switch (c) {
case 27:
editor.editmode = false;