can edit and save files

This commit is contained in:
2025-12-08 22:21:28 -05:00
parent 05dd26804a
commit 4ef5ef0f1b
2 changed files with 50 additions and 21 deletions

3
.gitignore vendored
View File

@@ -1 +1,4 @@
bin
*.dylib
.DS_Store
src/src

View File

@@ -15,26 +15,24 @@ read_file :: proc(path: string) -> (string, bool) {
}
Cursor :: struct {
line: i32,
char: i32,
}
Line :: struct {
start: i32,
end: i32
line: int,
char: int,
}
font_size: f32 = 20.0
text_height: f32
cursor: Cursor
content: [dynamic]u8
lines: [dynamic]Line
lines: [dynamic][dynamic]u8
render_cursor :: proc(cursor: ^Cursor) {
x := cursor.char * i32(font_size / 2)
x := cursor.char * int(font_size / 2)
y := i32(font_size) * i32(cursor.line)
r.DrawLine(x, y, x, y + i32(text_height), r.WHITE)
r.DrawLine(x + 1, y, x + 1, y + i32(text_height), r.WHITE)
r.DrawLine(i32(x), y, i32(x), y + i32(text_height), r.WHITE)
r.DrawLine(i32(x) + 1, y, i32(x) + 1, y + i32(text_height), r.WHITE)
}
current_line :: proc() -> ^[dynamic]u8 {
return &lines[cursor.line]
}
main :: proc() {
@@ -47,7 +45,11 @@ main :: proc() {
content, _success := read_file("src/main.odin")
for line in strings.split_lines_iterator(&content) {
bs := make([dynamic]u8, 0, len(line) == 0 ? 1 : len(line) * 2)
append(&bs, line)
append(&lines, bs)
}
text: string
dirty := true
@@ -60,7 +62,7 @@ main :: proc() {
}
if r.IsKeyPressed(r.KeyboardKey.RIGHT) {
cursor.char += 1
cursor.char = min(int(cursor.char) + 1, len(current_line()^))
}
if r.IsKeyPressed(r.KeyboardKey.LEFT) {
cursor.char -= 1
@@ -71,13 +73,37 @@ main :: proc() {
if r.IsKeyPressed(r.KeyboardKey.DOWN) {
cursor.line += 1
}
if r.IsKeyPressed(r.KeyboardKey.ENTER) {
new_line_cap := len(current_line()) - cursor.char
bs := make([dynamic]u8, 0, len(current_line()) == 0 ? 1 : new_line_cap * 2)
if cursor.char < len(current_line()) {
for c, i in current_line()[cursor.char:] {
append(&bs, c)
}
}
inject_at(&lines, cursor.line + 1, bs)
resize(current_line(), cursor.char)
cursor.line += 1
cursor.char = 0
}
if r.IsKeyDown(r.KeyboardKey.LEFT_SUPER) && r.IsKeyPressed(r.KeyboardKey.S) {
builder := strings.builder_make()
defer strings.builder_destroy(&builder)
for line in lines {
strings.write_bytes(&builder, line[:])
strings.write_byte(&builder, '\n')
}
os.write_entire_file("foo.txt", transmute([]u8)strings.to_string(builder))
fmt.println("Wrote file!")
}
if r.IsKeyPressed(r.KeyboardKey.BACKSPACE) {
if cursor.line == 0 && cursor.char == 0 do continue
if cursor.char == 0 {
append(&lines[cursor.line - 1], string(lines[cursor.line][:]))
old_len := len(lines[cursor.line - 1])
append(&lines[cursor.line - 1], string(current_line()[:]))
ordered_remove(&lines, cursor.line)
cursor.line -= 1
cursor.char = i32(len(lines[cursor.line]))
cursor.char = old_len
} else {
ordered_remove(&lines[cursor.line], cursor.char - 1)
cursor.char -= 1
@@ -87,17 +113,17 @@ main :: proc() {
r.BeginDrawing()
r.ClearBackground(r.BLACK)
for line, line_index in lines {
line := line
for &line, line_index in lines {
if cap(line) == 0 {
reserve(&line, 1)
}
if len(line) == cap(line) {
reserve(&line, len(line) * 2)
}
fmt.println("Here")
raw_data(line)[len(line)] = 0
fmt.println("Here2")
cstr := strings.unsafe_string_to_cstring(string(line[:]))
pos := {0, font_size * f32(line_index)}
pos := r.Vector2{0, font_size * f32(line_index)}
r.DrawTextEx(font, cstr, pos, font_size, 0, r.WHITE)
}