Ls and some improvements

This commit is contained in:
Krzosa Karol
2024-08-13 12:27:45 +02:00
parent 50e642483d
commit 7db1fc7bd6
3 changed files with 21 additions and 23 deletions

View File

@@ -1,20 +1,6 @@
lua_State *LuaState = NULL;
String16 LuaCommandResult = {};
String ListFiles(String path) {
Scratch scratch;
Array<String> strings = {scratch};
Add(&strings, String{"Open \"..\""});
for (FileIter iter = IterateFiles(scratch, path); IsValid(iter); Advance(&iter)) {
String string = Format(scratch, "Open \"%.*s\"", FmtString(iter.absolute_path));
Add(&strings, string);
}
Add(&strings, Format(scratch, "working dir = %.*s", FmtString(WorkingDir)));
String result = Merge(scratch, strings, "\n");
return result;
}
String FieldString(lua_State *L, String name) {
String result = {};
if (lua_istable(L, -1)) {
@@ -183,13 +169,28 @@ int Lua_GetActiveMainWindowBufferDir(lua_State *L) {
return 1;
}
int Lua_OpenFilesHere(lua_State *L) {
void ListFilesRecursive(Buffer *buffer, String filename) {
Scratch scratch(buffer->line_starts.allocator);
for (FileIter it = IterateFiles(scratch, filename); IsValid(it); Advance(&it)) {
if (it.filename == ".git") continue;
if (it.is_directory) {
ListFilesRecursive(buffer, it.absolute_path);
} else {
IKnowWhatImDoing_Appendf(buffer, "%.*s\n", FmtString(it.absolute_path));
}
}
}
int Lua_Ls(lua_State *L) {
BSet main = GetActiveMainSet();
Scratch scratch;
for (FileIter it = IterateFiles(scratch, GetActiveMainWindowBufferDir()); IsValid(it); Advance(&it)) {
WindowOpenBufferView(main.window, it.absolute_path);
}
String buffer_name = GetUniqueBufferName(scratch, GetDir(main.buffer), "+ls-");
Buffer *buffer = CreateBuffer(GetSystemAllocator(), buffer_name, 4096 * 4);
ListFilesRecursive(buffer, GetActiveMainWindowBufferDir());
WindowOpenBufferView(main.window, buffer_name);
return 0;
}

View File

@@ -11,7 +11,7 @@ luaL_Reg LuaFunctions[] = {
{"GetWorkingDir", Lua_GetWorkingDir},
{"GetActiveMainWindowBufferName", Lua_GetActiveMainWindowBufferName},
{"GetActiveMainWindowBufferDir", Lua_GetActiveMainWindowBufferDir},
{"OpenFilesHere", Lua_OpenFilesHere},
{"Ls", Lua_Ls},
{"Search", Lua_Search},
{"SearchB", Lua_SearchB},
{"Rename", Lua_Rename},

View File

@@ -82,7 +82,7 @@ inline Window *GetActiveWindow() { return GetWindow(ActiveWindow); }
void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 4096) {
buffer->id = AllocBufferID(buffer);
buffer->name = name;
buffer->name = Intern(&GlobalInternTable, name);
buffer->cap = size;
buffer->data = AllocArray(allocator, U16, buffer->cap);
buffer->line_starts.allocator = allocator;
@@ -294,10 +294,8 @@ Buffer *BufferOpenFile(String path) {
}
if (!FileExists(path)) {
path = Intern(&GlobalInternTable, path);
buffer = CreateBuffer(sys_allocator, path);
} else if (IsDir(path)) {
path = Intern(&GlobalInternTable, path);
buffer = CreateBuffer(sys_allocator, path, 4096 * 2);
buffer->is_directory = true;
@@ -305,7 +303,6 @@ Buffer *BufferOpenFile(String path) {
IKnowWhatImDoing_Appendf(buffer, "%.*s\n", FmtString(it.filename));
}
} else {
path = Intern(&GlobalInternTable, path);
String string = ReadFile(scratch, path);
buffer = CreateBuffer(sys_allocator, path, string.len * 4 + 4096);
buffer->len = ConvertUTF8ToUTF16UnixLine(string, buffer->str, buffer->cap);