Update c.lua
This commit is contained in:
+18
-15
@@ -285,6 +285,7 @@ end
|
|||||||
function M.run()
|
function M.run()
|
||||||
-- Get the current file's directory
|
-- Get the current file's directory
|
||||||
local current_dir = vim.fn.expand("%:p:h")
|
local current_dir = vim.fn.expand("%:p:h")
|
||||||
|
|
||||||
-- Try to find the root of the project by matching the path
|
-- Try to find the root of the project by matching the path
|
||||||
local project_root = current_dir:match("(.*)/src")
|
local project_root = current_dir:match("(.*)/src")
|
||||||
if not project_root then
|
if not project_root then
|
||||||
@@ -295,27 +296,29 @@ function M.run()
|
|||||||
-- Define the build directory
|
-- Define the build directory
|
||||||
local build_dir = project_root .. "/build"
|
local build_dir = project_root .. "/build"
|
||||||
|
|
||||||
-- List files in the build directory and find the .txt file
|
-- List only .txt files in the build directory and filter out unwanted ones like CMakeCache.txt
|
||||||
local files = vim.fn.glob(build_dir .. "/*.txt", true, true)
|
local files = vim.fn.glob(build_dir .. "/*.txt", true, true)
|
||||||
if #files == 0 then
|
local project_name = nil
|
||||||
print("Error: No .txt file found in the build directory.")
|
|
||||||
|
for _, file in ipairs(files) do
|
||||||
|
-- Make sure the file is the correct project name file (not CMakeCache.txt or others)
|
||||||
|
if not vim.fn.match(file, "CMakeCache.txt") then
|
||||||
|
-- Extract the project name (remove the extension)
|
||||||
|
project_name = vim.fn.fnamemodify(file, ":r")
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- If no project name file was found, print an error and return
|
||||||
|
if not project_name then
|
||||||
|
print("Error: No valid project name file found in the build directory.")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Extract the project name from the .txt file's name (without the .txt extension)
|
-- Print the project name for debugging
|
||||||
local project_name = vim.fn.fnamemodify(files[1], ":r") -- :r removes the file extension
|
|
||||||
print("Project_name: " .. project_name)
|
print("Project_name: " .. project_name)
|
||||||
|
|
||||||
-- Define the command to run the project (adjusted for the C project)
|
-- Open floating terminal and run the project executable
|
||||||
|
|
||||||
-- local current_dir = vim.fn.expand("%:p:h")
|
|
||||||
-- local project_root = current_dir:match("(.*)/src")
|
|
||||||
-- if not project_root then
|
|
||||||
-- print("Error: Must be inside the 'src' directory.")
|
|
||||||
-- return
|
|
||||||
-- end
|
|
||||||
--
|
|
||||||
-- local out_dir = project_root .. "/build"
|
|
||||||
M.open_float_terminal("cd " .. build_dir .. " && ./" .. project_name)
|
M.open_float_terminal("cd " .. build_dir .. " && ./" .. project_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user