finally saving files, such wow

memory-refactor
Patrick Cleaveliln 2025-07-10 00:52:17 +00:00
parent a8dbc09085
commit 838664f2b5
5 changed files with 53 additions and 4 deletions

View File

@ -401,6 +401,8 @@ update_file_buffer_index_from_cursor :: proc(buffer: ^FileBuffer) {
file_buffer_line_length :: proc(buffer: ^FileBuffer, index: FileBufferIndex) -> int { file_buffer_line_length :: proc(buffer: ^FileBuffer, index: FileBufferIndex) -> int {
line_length := 0; line_length := 0;
if len(buffer.content_slices) <= 0 do return line_length
first_character := buffer.content_slices[index.slice_index][index.content_index]; first_character := buffer.content_slices[index.slice_index][index.content_index];
left_it := new_file_buffer_iter_with_cursor(buffer, Cursor { index = 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 { buffer := FileBuffer {
allocator = allocator, allocator = allocator,
directory = dir, directory = dir,
file_path = fi.fullpath[4:], file_path = fi.fullpath,
// TODO: fix this windows issue
// file_path = fi.fullpath[4:],
extension = extension, extension = extension,
original_content = slice.clone_to_dynamic(original_content), 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), 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(); return buffer, error();
} else { } 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 { next_buffer :: proc(state: ^State, prev_buffer: ^int) -> int {
index := prev_buffer^; index := prev_buffer^;

View File

@ -73,6 +73,13 @@ register_default_input_actions :: proc(input_map: ^core.InputActions) {
}, "decrease font size"); }, "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"); 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)); register_default_go_actions(&(&input_map.key_actions[.G]).action.(core.InputActions));

View File

@ -336,7 +336,7 @@ main :: proc() {
defer ttf.Quit(); defer ttf.Quit();
sdl_window := sdl2.CreateWindow( sdl_window := sdl2.CreateWindow(
"odin_editor - [now with more ui]", "odin_editor - [less plugins more speed]",
sdl2.WINDOWPOS_UNDEFINED, sdl2.WINDOWPOS_UNDEFINED,
sdl2.WINDOWPOS_UNDEFINED, sdl2.WINDOWPOS_UNDEFINED,
640, 640,

View File

@ -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.open_element(s, fmt.tprintf("%s", state.mode), {})
ui.close_element(s) 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) ui.close_element(s)
} }

View File

@ -5,7 +5,7 @@
# Planned Features # Planned Features
- Save/Load files - Save/Load files
- [ ] Save - [x] Save
- [ ] Load when changed on disk - [ ] Load when changed on disk
- [ ] Simple File Search (vim /) - [ ] Simple File Search (vim /)
- [ ] Auto-indent - [ ] Auto-indent