summaryrefslogtreecommitdiff
path: root/lua/aotrix/compile.lua
diff options
context:
space:
mode:
authorAotrix <[email protected]>2025-12-27 01:02:55 +0100
committerAotrix <[email protected]>2025-12-27 01:02:55 +0100
commit18c3c1e3683d008b85920290a175eb14ebd66a47 (patch)
treeff704785d5f4faa12b939109d375a5f205e96db3 /lua/aotrix/compile.lua
parent3502d53434e9e013b71a202a860f37ac4928db8f (diff)
Add compile shortcuts for CMakeLists.txt targets
Diffstat (limited to 'lua/aotrix/compile.lua')
-rw-r--r--lua/aotrix/compile.lua78
1 files changed, 78 insertions, 0 deletions
diff --git a/lua/aotrix/compile.lua b/lua/aotrix/compile.lua
new file mode 100644
index 0000000..e5f2683
--- /dev/null
+++ b/lua/aotrix/compile.lua
@@ -0,0 +1,78 @@
+---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 generatorExecutable = io.popen([[sed -n "s/add_executable(\(.*\))/\1/p" CMakeLists.txt|sed "s/ .*//p"|uniq]])
+ :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, {
+ relative = "editor",
+ 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
+
+---Compiles a target
+---
+---@return nil
+function compileTarget()
+ function compile(pTarget)
+ createFloatingTerminal()
+ local command = "cmake --build build --target " .. pTarget .. " && ./build/" .. pTarget
+ executeCommandInTerminal(command)
+ end
+
+ chooseTarget(compile)
+end
+
+vim.keymap.set("n", "mt", compileTarget)