Skip to content

Commit

Permalink
feat(tmux): Execute and Run with tmux (#258)
Browse files Browse the repository at this point in the history
* initial commit [not-working]

* working runner

* close properly

* add stop functionality

* implement show

* reintroduce removed function after merge

the terminal executor/runner seems to have been changed since the last
time I rebased so I reintroduced the necessary functions to make my
vimux implementation work.

---------

Co-authored-by: amirt-ms <[email protected]>
  • Loading branch information
amirt01 and amirt-ms authored Dec 22, 2024
1 parent f36f0a6 commit 88ebb7d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lua/cmake-tools/const.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ local const = {
require("overseer").open({ enter = false, direction = "right" })
end, -- a function that gets overseer.Task when it is created, before calling `task:start`
},
vimux = {},
terminal = {
name = "Executor Terminal",
prefix_name = "[CMakeTools]: ", -- This must be included and must be unique, otherwise the terminals will not work. Do not use a simple spacebar " ", or any generic name
Expand Down Expand Up @@ -105,6 +106,7 @@ local const = {
}, -- options to pass into the `overseer.new_task` command
on_new_task = function(task) end, -- a function that gets overseer.Task when it is created, before calling `task:start`
},
vimux = {},
terminal = {
name = "Runner Terminal",
prefix_name = "[CMakeTools]: ", -- This must be included and must be unique, otherwise the terminals will not work. Do not use a simple spacebar " ", or any generic name
Expand Down
1 change: 1 addition & 0 deletions lua/cmake-tools/executors/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ return {
terminal = require("cmake-tools.terminal"),
overseer = require("cmake-tools.overseer"),
toggleterm = require("cmake-tools.toggleterm"),
vimux = require("cmake-tools.vimux"),
}
1 change: 1 addition & 0 deletions lua/cmake-tools/runners/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ return {
terminal = require("cmake-tools.terminal"),
overseer = require("cmake-tools.overseer"),
toggleterm = require("cmake-tools.toggleterm"),
vimux = require("cmake-tools.vimux"),
}
74 changes: 74 additions & 0 deletions lua/cmake-tools/vimux.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
local terminal = require("cmake-tools.terminal")
local osys = require("cmake-tools.osys")
local utils = require("cmake-tools.utils")
---@class vimux : terminal
local _vimux = {
id = nil,
}

function _vimux.show(opts)
vim.fn.VimuxInspectRunner()
end

function _vimux.close(opts)
vim.fn.VimuxCloseRunner()
end

function _vimux.run(cmd, env_script, env, args, cwd, opts, on_exit, on_output)
local full_cmd = _vimux.prepare_cmd_for_run(cmd, env, args, cwd)
vim.fn.VimuxRunCommand(full_cmd)
terminal.handle_exit(opts, on_exit, opts.close_on_exit)
end

function _vimux.has_active_job(opts)
return false
end

function _vimux.stop(opts)
vim.fn.VimuxSendKeys("C-c")
end

---Check if the executor is installed and can be used
---@return string|boolean
function _vimux.is_installed()
if not vim.fn.exists(":VimuxRunCommand") then
return "Vimux plugin is missing, please install it"
end
return true
end

function _vimux.prepare_cmd_for_run(cmd, env, args, cwd)
local full_cmd = ""

-- Launch form executable's build directory by default
full_cmd = "cd " .. utils.transform_path(cwd) .. " &&"

if osys.iswin32 then
for k, v in pairs(env) do
full_cmd = full_cmd .. " set " .. k .. "=" .. v .. "&&"
end
else
for k, v in pairs(env) do
full_cmd = full_cmd .. " " .. k .. "=" .. v .. ""
end
end

full_cmd = full_cmd .. " " .. utils.transform_path(cmd)

if osys.islinux or osys.iswsl or osys.ismac then
full_cmd = " " .. full_cmd -- adding a space in front of the command prevents bash from recording the command in the history (if configured)
end

-- Add args to the cmd
for _, arg in ipairs(args) do
full_cmd = full_cmd .. " " .. arg
end

if osys.iswin32 then -- wrap in sub process to prevent env vars from being persited
full_cmd = 'cmd /C "' .. full_cmd .. '"'
end

return full_cmd
end

return _vimux

0 comments on commit 88ebb7d

Please sign in to comment.