From 79fd4378e05213b7f510b098d9fe3904fa1c80da Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Mon, 10 Mar 2025 14:36:36 -0400 Subject: [PATCH] Update c.lua --- lua/Polydev/c.lua | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/lua/Polydev/c.lua b/lua/Polydev/c.lua index e269b3c..ee88fd9 100644 --- a/lua/Polydev/c.lua +++ b/lua/Polydev/c.lua @@ -233,39 +233,35 @@ public class %s { end function M.build() + -- Get the current file's directory local current_dir = vim.fn.expand("%:p:h") + -- Try to find the root of the project by matching the path local project_root = current_dir:match("(.*)/src") - if not project_root then print("Error: src directory not found.") return end + -- Define the build directory local build_dir = project_root .. "/build" - local files = vim.fn.glob(build_dir .. "/*.txt", true, true) - local project_name = nil - -- Look for the project name by checking for files in the build directory - for _, file in ipairs(files) do - if vim.fn.match(file, "CMakeCache.txt") == -1 then - project_name = vim.fn.fnamemodify(file, ":r") - break - end - end - - if not project_name then - print("Error: No valid project name file found in the build directory.") + -- List files in the build directory and find the .txt file + local files = vim.fn.glob(build_dir .. "/*.txt", true, true) + if #files == 0 then + print("Error: No .txt file found in the build directory.") return end + -- Extract the project name from the .txt file's name (without the .txt extension) + local project_name = vim.fn.fnamemodify(files[1], ":r") -- :r removes the file extension + -- Build command local compile_command = "cd " .. build_dir .. " && cmake .. && make && chmod +x " .. project_name - -- Open floating terminal to execute the build command local term_buf = M.open_float_terminal(compile_command) vim.api.nvim_buf_set_option(term_buf, "modifiable", true) - -- Run the compile command and capture output + -- Run compile command and capture output local compile_status = vim.fn.system(compile_command) -- Set message based on compile result