diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/aotrix/build.lua | 54 | ||||
| -rw-r--r-- | lua/aotrix/compile.lua | 124 |
2 files changed, 54 insertions, 124 deletions
diff --git a/lua/aotrix/build.lua b/lua/aotrix/build.lua new file mode 100644 index 0000000..b5dc3bb --- /dev/null +++ b/lua/aotrix/build.lua @@ -0,0 +1,54 @@ +local BUILD_COMMAND = "" + +---@return nil +function createFloatingWindow() + local max_height = vim.api.nvim_win_get_height(0) + local max_width = vim.api.nvim_win_get_width(0) + local window_height = math.floor(max_height / 2) + local window_width = math.floor(max_width / 2) + local window_buffer = vim.api.nvim_create_buf(false, true) + vim.api.nvim_open_win(window_buffer, true, { + border = "rounded", + style = "minimal", + relative = "editor", + noautocmd = true, + height = window_height, + width = window_width, + col = math.floor((max_width - window_width) / 2), + row = math.floor((max_height - window_height) / 2) + }) + return window_buffer +end + +---@return nil +function createFloatingTerminal() + createFloatingWindow() + vim.cmd.term() + vim.cmd.startinsert() +end + +---@param command string +---@return nil +function executeCommandInTerminal(command) + local channel = vim.bo.channel + -- 'clear' is used to hide the command + local exit_message = "\nPress enter to exit..." + vim.api.nvim_chan_send(channel, + "clear && " .. command .. "; echo '" .. exit_message .. + "' && read -s && exit" .. "\n") +end + +function setBuildCommand() + BUILD_COMMAND = vim.fn.input("Enter compile command: ", BUILD_COMMAND) +end + +function build() + if string.len(BUILD_COMMAND) == 0 then setBuildCommand() end + if string.len(BUILD_COMMAND) > 0 then + createFloatingTerminal() + executeCommandInTerminal(BUILD_COMMAND) + end +end + +vim.keymap.set("n", "mc", setBuildCommand) +vim.keymap.set("n", "mt", build) diff --git a/lua/aotrix/compile.lua b/lua/aotrix/compile.lua deleted file mode 100644 index e7c9556..0000000 --- a/lua/aotrix/compile.lua +++ /dev/null @@ -1,124 +0,0 @@ -local build_dir - ----Generates a table with available targets ---- ----@return table ----@nodiscard -function tableOfTargets() - -- Ugly way to retrieve targets, but it works - -- Do not hesitate to change the command - local command = "cmake --build " .. build_dir .. - " -t help|sed 1d|grep -v -e " .. [["]] .. - "all\\|clean\\|depend\\|edit_cache\\|rebuild_cache" .. - [["]] .. " -e " .. [["]] .. "\\.\\{3\\} .*\\..*$" .. - [["]] .. "|sed " .. [["]] .. "s/\\.\\.\\. //" .. [["]] - local generatorExecutable = io.popen(command):lines() - local choiceTable = {} - for line in generatorExecutable do table.insert(choiceTable, line) end - return choiceTable -end - ----Displays a menu to let the user choose a target to compile ---- ----@return nil -function chooseTarget(pCallbackFunction) - vim.ui.select(tableOfTargets(), { - prompt = 'Choose target:', - format_item = function(item) return item end - }, pCallbackFunction) -end - ----@return nil -function createFloatingWindow() - local max_height = vim.api.nvim_win_get_height(0) - local max_width = vim.api.nvim_win_get_width(0) - local window_height = math.floor(max_height / 2) - local window_width = math.floor(max_width / 2) - local window_buffer = vim.api.nvim_create_buf(false, true) - vim.api.nvim_open_win(window_buffer, true, { - border = "rounded", - style = "minimal", - relative = "editor", - noautocmd = true, - height = window_height, - width = window_width, - col = math.floor((max_width - window_width) / 2), - row = math.floor((max_height - window_height) / 2) - }) - return window_buffer -end - ----@return nil -function createFloatingTerminal() - createFloatingWindow() - vim.cmd.term() - vim.cmd.startinsert() -end - ----@param command string ----@return nil -function executeCommandInTerminal(command) - local channel = vim.bo.channel - -- 'clear' is used to hide the command - local exit_message = "\nPress enter to exit..." - vim.api.nvim_chan_send(channel, - "clear && " .. command .. "; echo '" .. exit_message .. - "' && read -s && exit" .. "\n") -end - ----@return nil -function defineBuildFolderPath() - -- TODO: One might not want a directory called "build" - build_dir = "build" -end - ----Compiles a target ---- ----@return nil -function compileTarget() - function compile(pTarget) - if not (pTarget == nil) and string.len(pTarget) > 0 then - createFloatingTerminal() - local commandTable = { - "cmake --build " .. build_dir .. " --target " .. pTarget - } - local commandConcatenation = "" - for i, command in ipairs(commandTable) do - commandConcatenation = commandConcatenation .. command - if i < #commandTable then - commandConcatenation = commandConcatenation .. " && " - end - end - executeCommandInTerminal(commandConcatenation) - end - end - - defineBuildFolderPath() - chooseTarget(compile) -end - -function compileAndExecuteTarget() - function compileAndExecute(pTarget) - if not (pTarget == nil) and string.len(pTarget) > 0 then - createFloatingTerminal() - local commandTable = { - "cmake --build " .. build_dir .. " --target " .. pTarget, - build_dir .. "/" .. pTarget - } - local commandConcatenation = "" - for i, command in ipairs(commandTable) do - commandConcatenation = commandConcatenation .. command - if i < #commandTable then - commandConcatenation = commandConcatenation .. " && " - end - end - executeCommandInTerminal(commandConcatenation) - end - end - - defineBuildFolderPath() - chooseTarget(compileAndExecute) -end - -vim.keymap.set("n", "mc", compileTarget) -vim.keymap.set("n", "mt", compileAndExecuteTarget) |
