From 838664f2b50ebdbe54ef874d859c2909a58d3b74 Mon Sep 17 00:00:00 2001 From: Patrick Cleaveliln Date: Thu, 10 Jul 2025 00:52:17 +0000 Subject: [PATCH] finally saving files, such wow --- src/core/file_buffer.odin | 30 ++++++++++++++++++++++++++++-- src/input/input.odin | 7 +++++++ src/main.odin | 2 +- src/panels/panels.odin | 16 ++++++++++++++++ todo.md | 2 +- 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/core/file_buffer.odin b/src/core/file_buffer.odin index e6d3a49..0d16138 100644 --- a/src/core/file_buffer.odin +++ b/src/core/file_buffer.odin @@ -401,6 +401,8 @@ update_file_buffer_index_from_cursor :: proc(buffer: ^FileBuffer) { file_buffer_line_length :: proc(buffer: ^FileBuffer, index: FileBufferIndex) -> int { line_length := 0; + if len(buffer.content_slices) <= 0 do return line_length + first_character := buffer.content_slices[index.slice_index][index.content_index]; left_it := new_file_buffer_iter_with_cursor(buffer, Cursor { index = index }); @@ -717,7 +719,9 @@ new_file_buffer :: proc(allocator: mem.Allocator, file_path: string, base_dir: s buffer := FileBuffer { allocator = allocator, directory = dir, - file_path = fi.fullpath[4:], + file_path = fi.fullpath, + // TODO: fix this windows issue + // file_path = fi.fullpath[4:], extension = extension, original_content = slice.clone_to_dynamic(original_content), @@ -731,7 +735,12 @@ new_file_buffer :: proc(allocator: mem.Allocator, file_path: string, base_dir: s input_buffer = make([dynamic]u8, 0, 1024), }; - append(&buffer.content_slices, buffer.original_content[:]); + if len(buffer.original_content) > 0 { + append(&buffer.content_slices, buffer.original_content[:]); + } else { + append(&buffer.added_content, '\n') + append(&buffer.content_slices, buffer.added_content[:]) + } return buffer, error(); } else { @@ -739,6 +748,23 @@ new_file_buffer :: proc(allocator: mem.Allocator, file_path: string, base_dir: s } } +save_buffer_to_disk :: proc(state: ^State, buffer: ^FileBuffer) -> (error: os.Error) { + fd := os.open(buffer.file_path, flags = os.O_RDWR) or_return; + defer os.close(fd); + + offset: i64 = 0 + for content_slice in buffer.content_slices { + os.write_at(fd, content_slice, offset) or_return + + offset += i64(len(content_slice)) + } + os.flush(fd) + + log.infof("written %v bytes", offset) + + return +} + next_buffer :: proc(state: ^State, prev_buffer: ^int) -> int { index := prev_buffer^; diff --git a/src/input/input.odin b/src/input/input.odin index 7686c80..cee2b1c 100644 --- a/src/input/input.odin +++ b/src/input/input.odin @@ -73,6 +73,13 @@ register_default_input_actions :: proc(input_map: ^core.InputActions) { }, "decrease font size"); } + // Save file + core.register_ctrl_key_action(input_map, .S, proc(state: ^State) { + if err := core.save_buffer_to_disk(state, core.current_buffer(state)); err != nil { + log.errorf("failed to save buffer to disk: %v", err) + } + }, "Save file") + core.register_key_action(input_map, .G, core.new_input_actions(), "Go commands"); register_default_go_actions(&(&input_map.key_actions[.G]).action.(core.InputActions)); diff --git a/src/main.odin b/src/main.odin index 24c24b9..a162476 100644 --- a/src/main.odin +++ b/src/main.odin @@ -336,7 +336,7 @@ main :: proc() { defer ttf.Quit(); sdl_window := sdl2.CreateWindow( - "odin_editor - [now with more ui]", + "odin_editor - [less plugins more speed]", sdl2.WINDOWPOS_UNDEFINED, sdl2.WINDOWPOS_UNDEFINED, 640, diff --git a/src/panels/panels.odin b/src/panels/panels.odin index ed63a3f..31ec700 100644 --- a/src/panels/panels.odin +++ b/src/panels/panels.odin @@ -142,6 +142,22 @@ render_file_buffer :: proc(state: ^core.State, s: ^ui.State, buffer: ^core.FileB { ui.open_element(s, fmt.tprintf("%s", state.mode), {}) ui.close_element(s) + + ui.open_element(s, nil, { kind = {ui.Grow{}, ui.Grow{}}}) + ui.close_element(s) + + ui.open_element( + s, + fmt.tprintf( + "%v:%v - Slice %v:%v", + buffer.cursor.line + 1, + buffer.cursor.col + 1, + buffer.cursor.index.slice_index, + buffer.cursor.index.content_index + ), + {} + ) + ui.close_element(s) } ui.close_element(s) } diff --git a/todo.md b/todo.md index 251f319..9126d26 100644 --- a/todo.md +++ b/todo.md @@ -5,7 +5,7 @@ # Planned Features - Save/Load files - - [ ] Save + - [x] Save - [ ] Load when changed on disk - [ ] Simple File Search (vim /) - [ ] Auto-indent