From cb4f3803133d0a9fa3b8abc98460631ae96ea2c9 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 24 Jul 2024 14:16:58 +0200 Subject: [PATCH] List files and fix scrolling --- src/basic/basic.h | 1 + src/basic/filesystem.h | 2 ++ src/basic/win32.cpp | 18 ++++++++++++++++++ src/text_editor/commands_window.cpp | 2 +- src/text_editor/lua_api.cpp | 20 ++++++++++++++++++++ src/text_editor/text_editor.cpp | 5 +++-- 6 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/basic/basic.h b/src/basic/basic.h index ee114c1..61dcfc2 100644 --- a/src/basic/basic.h +++ b/src/basic/basic.h @@ -741,6 +741,7 @@ UTF8Iter IterateUTF8Ex(char *data, int64_t len); UTF8Iter IterateUTF8(char *data); UTF8Iter IterateUTF8(String string); +bool IsAlphabetic(char a); #define FmtString(string) (int)(string).len, (string).data bool AreEqual(String a, String b, unsigned ignore_case = false); int64_t CreateWidecharFromChar(wchar_t *buffer, int64_t buffer_size, char *in, int64_t inlen); diff --git a/src/basic/filesystem.h b/src/basic/filesystem.h index 3205319..d19e9b8 100644 --- a/src/basic/filesystem.h +++ b/src/basic/filesystem.h @@ -31,6 +31,8 @@ String GetExeDir(Allocator allocator); bool FileExists(String path); bool IsDir(String path); bool IsFile(String path); +String GetWorkingDir(Allocator arena); +bool IsAbsolute(String path); struct Process { bool is_valid; diff --git a/src/basic/win32.cpp b/src/basic/win32.cpp index 10a3662..89cd407 100644 --- a/src/basic/win32.cpp +++ b/src/basic/win32.cpp @@ -299,3 +299,21 @@ bool IsFile(String path) { bool is_file = (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) == 0; return dwAttrib != INVALID_FILE_ATTRIBUTES && is_file; } + +String GetWorkingDir(Allocator arena) { + wchar_t wbuffer[1024]; + DWORD wsize = GetCurrentDirectoryW(Lengthof(wbuffer), wbuffer); + Assert(wsize != 0); + Assert(wsize < 1022); + wbuffer[wsize++] = '/'; + wbuffer[wsize] = 0; + + String path = ToString(arena, wbuffer, wsize); + NormalizePathInPlace(path); + return path; +} + +bool IsAbsolute(String path) { + bool result = path.len > 3 && IsAlphabetic(path.data[0]) && path.data[1] == ':' && path.data[2] == '/'; + return result; +} diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 6b9741b..17c7eec 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -447,7 +447,7 @@ void HandleActiveWindowBindings(Window *window) { Vec2I visible_size = visible_cells * Vec2I{FontCharSpacing, FontLineSpacing}; Vec2I rect_size = GetSize(window->document_rect); - if (xy.line > visible.max.y - 2) { + if (xy.line >= visible.max.y - 2) { Int set_view_at_line = xy.line - (visible_cells.y - 1); Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y); view.scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y; diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 788b49b..b8712f8 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -9,6 +9,23 @@ int LuaOpenFile(lua_State *L) { return 0; // number of results } +int LuaListFiles(lua_State *L) { + const char *text = luaL_checkstring(L, 1); + String path = text; + + Scratch scratch; + Array strings = {scratch}; + for (FileIter iter = IterateFiles(scratch, path); IsValid(iter); Advance(&iter)) { + if (iter.is_directory) continue; + String string = Format(scratch, "open \"%.*s\"", FmtString(iter.absolute_path)); + Add(&strings, string); + } + + String result = Merge(scratch, strings, "\n"); + lua_pushlstring(LuaState, result.data, result.len); + return 1; +} + int LuaListOpenBuffers(lua_State *L) { Scratch scratch; Array strings = {scratch}; @@ -69,6 +86,9 @@ void InitLua() { lua_pushcfunction(LuaState, LuaListWindows); lua_setglobal(LuaState, "list_windows"); + + lua_pushcfunction(LuaState, LuaListFiles); + lua_setglobal(LuaState, "list_files"); } String16 EvalString(Allocator allocator, String16 string16) { diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 21c2250..8c2edf0 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -31,13 +31,14 @@ #include "lua_api.cpp" /* +- fix pathing in open, make it predictable + - Save file (utf16->utf8) - resize windows - list files and open - file dock on left side - We can actually combine this with command window and lua, it's just going to be a buffer of - open "asd/asd/asd/asd" - - Ctrl + F - word completion @@ -184,7 +185,7 @@ int main(void) { CutTop(&screen_rect, size.y * 0.05f); CutLeft(&screen_rect, size.x * 0.2f); CutRight(&screen_rect, size.x * 0.2f); - Rect2 r = CutTop(&screen_rect, FontLineSpacing * 10.f); + Rect2 r = CutTop(&screen_rect, FontLineSpacing * 30.f); Windows[i].z = 1; Windows[i].total_rect = ToRect2I(r);