|
| 1 | +local terminal = require("cmake-tools.terminal") |
| 2 | +local osys = require("cmake-tools.osys") |
| 3 | +local utils = require("cmake-tools.utils") |
| 4 | +---@class vimux : terminal |
| 5 | +local _vimux = { |
| 6 | + id = nil, |
| 7 | +} |
| 8 | + |
| 9 | +function _vimux.show(opts) |
| 10 | + vim.fn.VimuxInspectRunner() |
| 11 | +end |
| 12 | + |
| 13 | +function _vimux.close(opts) |
| 14 | + vim.fn.VimuxCloseRunner() |
| 15 | +end |
| 16 | + |
| 17 | +function _vimux.run(cmd, env_script, env, args, cwd, opts, on_exit, on_output) |
| 18 | + local full_cmd = _vimux.prepare_cmd_for_run(cmd, env, args, cwd) |
| 19 | + vim.fn.VimuxRunCommand(full_cmd) |
| 20 | + terminal.handle_exit(opts, on_exit, opts.close_on_exit) |
| 21 | +end |
| 22 | + |
| 23 | +function _vimux.has_active_job(opts) |
| 24 | + return false |
| 25 | +end |
| 26 | + |
| 27 | +function _vimux.stop(opts) |
| 28 | + vim.fn.VimuxSendKeys("C-c") |
| 29 | +end |
| 30 | + |
| 31 | +---Check if the executor is installed and can be used |
| 32 | +---@return string|boolean |
| 33 | +function _vimux.is_installed() |
| 34 | + if not vim.fn.exists(":VimuxRunCommand") then |
| 35 | + return "Vimux plugin is missing, please install it" |
| 36 | + end |
| 37 | + return true |
| 38 | +end |
| 39 | + |
| 40 | +function _vimux.prepare_cmd_for_run(cmd, env, args, cwd) |
| 41 | + local full_cmd = "" |
| 42 | + |
| 43 | + -- Launch form executable's build directory by default |
| 44 | + full_cmd = "cd " .. utils.transform_path(cwd) .. " &&" |
| 45 | + |
| 46 | + if osys.iswin32 then |
| 47 | + for k, v in pairs(env) do |
| 48 | + full_cmd = full_cmd .. " set " .. k .. "=" .. v .. "&&" |
| 49 | + end |
| 50 | + else |
| 51 | + for k, v in pairs(env) do |
| 52 | + full_cmd = full_cmd .. " " .. k .. "=" .. v .. "" |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + full_cmd = full_cmd .. " " .. utils.transform_path(cmd) |
| 57 | + |
| 58 | + if osys.islinux or osys.iswsl or osys.ismac then |
| 59 | + full_cmd = " " .. full_cmd -- adding a space in front of the command prevents bash from recording the command in the history (if configured) |
| 60 | + end |
| 61 | + |
| 62 | + -- Add args to the cmd |
| 63 | + for _, arg in ipairs(args) do |
| 64 | + full_cmd = full_cmd .. " " .. arg |
| 65 | + end |
| 66 | + |
| 67 | + if osys.iswin32 then -- wrap in sub process to prevent env vars from being persited |
| 68 | + full_cmd = 'cmd /C "' .. full_cmd .. '"' |
| 69 | + end |
| 70 | + |
| 71 | + return full_cmd |
| 72 | +end |
| 73 | + |
| 74 | +return _vimux |
0 commit comments