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)); // 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); // str_dealloc(&s);
// return 0; // return 0;
// } // }
+30 -14
View File
@@ -1,13 +1,12 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "dynstr.h" #include "dynstr.h"
#include <ncurses.h> #include <ncurses.h>
typedef struct { typedef struct {
int lines; uint32_t lines;
int screen_line; uint32_t screen_line;
int screen_col; uint32_t screen_col;
bool editmode; bool editmode;
String_t* buffer; String_t* buffer;
} Editor; } Editor;
@@ -20,7 +19,9 @@ Editor new_editor() {
e.editmode = false; e.editmode = false;
String_t lines = str_from_chars(""); 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); str_dealloc(&lines);
addstr(""); addstr("");
@@ -29,25 +30,39 @@ Editor new_editor() {
} }
Editor editor_from(char* input_string) { Editor editor_from(char* input_string) {
// TODO: fix this function
Editor e; Editor e;
e.lines = 0; e.lines = 0;
e.screen_line = 0; e.screen_line = 0;
e.screen_col = 0; e.screen_col = 0;
e.editmode = false; 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); addstr(input_string);
return e; return e;
} }
void move_cursor(Editor* self, int x, int y) { 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 if (y != 0
&& self->screen_line + y >= 0 && (int)(self->screen_line) + y >= 0
&& self->screen_line + y <= self->lines) && self->screen_line + y <= self->lines)
{ {
self->screen_line += y; self->screen_line += y;
@@ -55,7 +70,8 @@ void move_cursor(Editor* self, int x, int y) {
if (self->screen_col > line_width) { if (self->screen_col > line_width) {
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) { if (self->screen_line - 1 >= 0) {
self->screen_line -= 1; self->screen_line -= 1;
self->screen_col = str_len(&self->buffer[self->screen_line]); 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) { } else if (x != 0) {
self->screen_col += x; 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); move(self->screen_line, self->screen_col);
} }
+5 -3
View File
@@ -1,8 +1,10 @@
#include "dynstr.h" #include "dynstr.h"
#include <stdint.h>
typedef struct { typedef struct {
int screen_line; uint32_t lines;
int screen_col; uint32_t screen_line;
uint32_t screen_col;
bool editmode; bool editmode;
String_t* buffer; String_t* buffer;
} Editor; } Editor;
@@ -15,4 +17,4 @@ void move_cursor(Editor* self, int x, int y);
void delchar(Editor* self); void delchar(Editor* self);
void addchar(Editor* self, char c); 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"); 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) { while (true) {
refresh(); refresh();
int c = getch(); int c = getch();
if (editor.editmode) { if (editor.editmode == true) {
switch (c) { switch (c) {
case 27: case 27:
editor.editmode = false; editor.editmode = false;
+22 -10
View File
@@ -7,7 +7,6 @@ use std::{
io::Write, io::Write,
}; };
fn somefunc() { fn somefunc() {
let c = "hello"; let c = "hello";
@@ -16,7 +15,6 @@ fn somefunc() {
let c2 = String::from("hello world"); let c2 = String::from("hello world");
} }
struct EditorData { struct EditorData {
buffer: Vec<Vec<char>>, // outer vec is the line, inner is col; buffer: Vec<Vec<char>>, // outer vec is the line, inner is col;
file_line: i32, file_line: i32,
@@ -60,16 +58,30 @@ impl EditorData {
&& self.screen_line + dy <= self.buffer.len() as i32 && self.screen_line + dy <= self.buffer.len() as i32
{ {
self.screen_line += dy; self.screen_line += dy;
let line_width = self.buffer.get(self.screen_line as usize).unwrap_or(&Vec::<char>::new()).len() as i32; let line_width = self
.buffer
.get(self.screen_line as usize)
.unwrap_or(&Vec::<char>::new())
.len() as i32;
if self.screen_col > line_width { if self.screen_col > line_width {
self.screen_col = line_width; self.screen_col = line_width;
} }
} else if self.screen_col + dx < 0 { } else if self.screen_col + dx < 0 {
if self.screen_line - 1 >= 0 { if self.screen_line - 1 >= 0 {
self.screen_line -= 1; self.screen_line -= 1;
self.screen_col = self.buffer.get(self.screen_line as usize).unwrap_or(&Vec::<char>::new()).len() as i32; self.screen_col = self
.buffer
.get(self.screen_line as usize)
.unwrap_or(&Vec::<char>::new())
.len() as i32;
} }
} else if self.screen_col + dx > self.buffer.get(self.screen_line as usize).unwrap_or(&Vec::<char>::new()).len() as i32 { } else if self.screen_col + dx
> self
.buffer
.get(self.screen_line as usize)
.unwrap_or(&Vec::<char>::new())
.len() as i32
{
if self.screen_line + 1 <= self.buffer.len() as i32 { if self.screen_line + 1 <= self.buffer.len() as i32 {
self.screen_col = 0; self.screen_col = 0;
self.screen_line += 1; self.screen_line += 1;
@@ -107,7 +119,10 @@ impl EditorData {
line.insert(self.screen_col as usize, c); line.insert(self.screen_col as usize, c);
} else { } else {
self.buffer.push(Vec::new()); self.buffer.push(Vec::new());
self.buffer.get_mut(self.screen_line as usize).unwrap().push(c); self.buffer
.get_mut(self.screen_line as usize)
.unwrap()
.push(c);
} }
self.mv_cursor(0, 1); self.mv_cursor(0, 1);
} }
@@ -226,9 +241,7 @@ fn open(filename: &str) -> Result<(), &'static str> {
ncurses::KEY_RIGHT => data.mv_cursor(0, 1), ncurses::KEY_RIGHT => data.mv_cursor(0, 1),
ncurses::KEY_UP => data.mv_cursor(-1, 0), ncurses::KEY_UP => data.mv_cursor(-1, 0),
ncurses::KEY_DOWN => data.mv_cursor(1, 0), ncurses::KEY_DOWN => data.mv_cursor(1, 0),
ncurses::KEY_ENTER => { ncurses::KEY_ENTER => {}
}
_ => { _ => {
data.addchar(char::from_u32(keystroke as u32).unwrap()); data.addchar(char::from_u32(keystroke as u32).unwrap());
} }
@@ -240,7 +253,6 @@ fn open(filename: &str) -> Result<(), &'static str> {
} }
119 => { 119 => {
let buff_size = max_y * max_x; let buff_size = max_y * max_x;
let buff = data.to_string(); let buff = data.to_string();