forked from Civitasv/cmake-tools.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggleterm.lua
More file actions
129 lines (113 loc) · 3.64 KB
/
toggleterm.lua
File metadata and controls
129 lines (113 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
local has_toggleterm, toggleterm = pcall(require, "toggleterm")
local osys = require("cmake-tools.osys")
local log = require("cmake-tools.log")
local utils = require("cmake-tools.utils")
if not has_toggleterm then
return
end
local _terminal = require("toggleterm.terminal")
---@class _toggleterm : executor, runner
local _toggleterm = {
chan_id = nil,
term = nil,
cmd = nil,
}
function _toggleterm.show(opts)
_toggleterm.term:open()
end
function _toggleterm.close(opts)
_toggleterm.term:close()
end
function _toggleterm.run(cmd, env_script, env, args, cwd, opts, on_exit, on_output)
local full_cmd = ""
-- Launch form executable's build directory by default
full_cmd = "cd " .. utils.shell_quote(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.shell_quote(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 .. " " .. utils.shell_quote(arg)
end
_toggleterm.cmd = full_cmd
if opts.singleton and _toggleterm.term then
_toggleterm.term:close()
end
_toggleterm.term = _terminal.Terminal:new({
--[[ env = {}, -- key:value table with environmental variables passed to jobstart() ]]
cmd = _toggleterm.cmd,
dir = cwd, -- the directory for the terminal
direction = opts.direction, -- the layout for the terminal, same as the main config options
close_on_exit = opts.close_on_exit, -- close the terminal window when the process exits
auto_scroll = opts.auto_scroll, -- automatically scroll to the bottom on terminal output
-- callbacks for processing the output
on_stdout = function(t, job, data, name)
on_output(data)
end, -- callback for processing output on stdout
on_stderr = function(t, job, data, name)
if opts.scroll_on_error then
_toggleterm.term:scroll_bottom()
end
on_output(nil, data)
end, -- callback for processing output on stderr
on_exit = function(t, job, exit_code, name)
on_exit(exit_code)
if exit_code ~= 0 then -- operation failed
if opts.scroll_on_error then
_toggleterm.term:scroll_bottom()
end
if opts.focus_on_error then
vim.cmd("wincmd p")
end
end
_toggleterm.chan_id = nil
_toggleterm.cmd = nil
end, -- function to run when terminal process exits
})
if not _toggleterm.term:is_open() then
_toggleterm.term:open()
if not opts.auto_focus then -- focus back on editor
vim.cmd("wincmd p")
vim.cmd("stopinsert!")
end
end
_toggleterm.chan_id = _toggleterm.term.job_id
end
function _toggleterm.has_active_job(opts)
if _toggleterm.chan_id ~= nil then
log.error(
"A CMake task is already running: "
.. _toggleterm.cmd
.. " Stop it before trying to run a new CMake task."
)
return true
end
return false
end
function _toggleterm.stop(opts)
if _toggleterm.chan_id then
vim.fn.jobstop(_toggleterm.chan_id)
_toggleterm.chan_id = nil
_toggleterm.cmd = nil
_toggleterm.term:close()
end
end
---Check if the executor is installed and can be used
---@return string|boolean
function _toggleterm.is_installed()
if not has_toggleterm then
return "toggleterm plugin is missing, please install it"
end
return true
end
return _toggleterm