From f8c9e8fd3e8049a49f90c972051a5b9cac78c392 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 6 Aug 2024 07:49:33 +0200 Subject: [PATCH] Open and navigate directories --- src/text_editor/management.cpp | 26 +++++++++++++++++++++----- src/text_editor/text_editor.h | 1 + src/text_editor/todo.txt | 2 -- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index a898e50..67804b0 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -172,9 +172,13 @@ String GetCurrentBufferName() { } String GetCurrentBufferDir() { - String buffer_name = GetCurrentBufferName(); - String name = ChopLastSlash(buffer_name); - if (name.len == buffer_name.len) return {}; + Window *window = GetWindow(ActiveWindow); + if (window->is_title_bar) { + window = GetWindow(window->title_bar_window); + } + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + String name = buffer->is_directory ? buffer->name : ChopLastSlash(buffer->name); return name; } @@ -196,10 +200,22 @@ Buffer *BufferOpenFile(String path) { } if (!FileExists(path)) { - path = Copy(sys_allocator, path); + path = GetAbsolutePath(sys_allocator, path); buffer = CreateBuffer(sys_allocator, path); + } else if (IsDir(path)) { + path = GetAbsolutePath(sys_allocator, path); + buffer = CreateBuffer(sys_allocator, path, 4096 * 2); + buffer->is_directory = true; + + int i = 1; + for (FileIter it = IterateFiles(scratch, path); IsValid(it); Advance(&it)) { + Appendf(buffer, "%.*s", FmtString(it.filename)); + if ((i % 8) == 0) Append(buffer, L"\n"); + else Append(buffer, L" "); + i += 1; + } } else { - path = Copy(sys_allocator, path); + path = GetAbsolutePath(sys_allocator, path); String string = ReadFile(scratch, path); buffer = CreateBuffer(sys_allocator, path, string.len * 4); diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 211ce5d..49c632a 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -39,6 +39,7 @@ struct Buffer { int edit_phase; bool no_history; bool dirty; + bool is_directory; }; struct View { diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index e2be1fd..4cc912c 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,6 +1,4 @@ - search as a command to execute which is going to be in the title bar -- each buffer needs a directory even the special ones: C:\a\b\c\+errors? -- open directories - resulting in buffer with dir listing and proper buffer name - clean \r\n into \n on trim and load - search backwards