bugfixes
This commit is contained in:
Binary file not shown.
@@ -214,6 +214,12 @@ 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;
|
||||
|
||||
+26
-10
@@ -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,16 +30,28 @@ 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;
|
||||
|
||||
int linenum = 0;
|
||||
String_t lines = str_from_chars(input_string);
|
||||
e.buffer = str_lines(&lines, &e.lines);
|
||||
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);
|
||||
|
||||
@@ -46,8 +59,10 @@ Editor editor_from(char* input_string) {
|
||||
}
|
||||
|
||||
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]);
|
||||
@@ -68,7 +84,7 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
+4
-2
@@ -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;
|
||||
|
||||
File diff suppressed because one or more lines are too long
+3
-1
@@ -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;
|
||||
|
||||
+22
-10
@@ -7,7 +7,6 @@ use std::{
|
||||
io::Write,
|
||||
};
|
||||
|
||||
|
||||
fn somefunc() {
|
||||
let c = "hello";
|
||||
|
||||
@@ -16,7 +15,6 @@ fn somefunc() {
|
||||
let c2 = String::from("hello world");
|
||||
}
|
||||
|
||||
|
||||
struct EditorData {
|
||||
buffer: Vec<Vec<char>>, // outer vec is the line, inner is col;
|
||||
file_line: i32,
|
||||
@@ -60,16 +58,30 @@ impl EditorData {
|
||||
&& self.screen_line + dy <= self.buffer.len() as i32
|
||||
{
|
||||
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 {
|
||||
self.screen_col = line_width;
|
||||
}
|
||||
} else if self.screen_col + dx < 0 {
|
||||
if self.screen_line - 1 >= 0 {
|
||||
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 {
|
||||
self.screen_col = 0;
|
||||
self.screen_line += 1;
|
||||
@@ -107,7 +119,10 @@ impl EditorData {
|
||||
line.insert(self.screen_col as usize, c);
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
@@ -226,9 +241,7 @@ fn open(filename: &str) -> Result<(), &'static str> {
|
||||
ncurses::KEY_RIGHT => data.mv_cursor(0, 1),
|
||||
ncurses::KEY_UP => 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());
|
||||
}
|
||||
@@ -241,7 +254,6 @@ fn open(filename: &str) -> Result<(), &'static str> {
|
||||
119 => {
|
||||
let buff_size = max_y * max_x;
|
||||
|
||||
|
||||
let buff = data.to_string();
|
||||
|
||||
ncurses::endwin();
|
||||
|
||||
Reference in New Issue
Block a user