Skip to content

Commit

Permalink
feat: toggleterm2 runner: add option clear_env, go_back; support Asyn…
Browse files Browse the repository at this point in the history
…cRunStop, autoclose
  • Loading branch information
fcying committed Sep 22, 2024
1 parent d6ba5dc commit 410ef9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,11 @@ When you use toggleterm2 and use the packer.nvim management plugin, you can set
"skywind3000/asyncrun.vim",
as = "asyncrun",
config = function()
require("asyncrun_toggleterm").setup({
require("asyncrun.toggleterm2").setup({
mapping = "<leader>tt",
start_in_insert = false,
clear_env = false,
go_back = true,
})
end,
})
Expand Down
24 changes: 15 additions & 9 deletions lua/asyncrun/toggleterm2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ local M = {}
M.setup = function(opts)
M._asyncrun_mapping = opts.mapping
M._start_in_insert = opts.start_in_insert
M._clear_env = opts.clear_env
M._go_back = opts.go_back
end

function M.reset()
if M._asyncrun_term ~= nil then
if vim.g.asynctasks_term_reuse ~= 1 then
-- TODO: handle multiple terminals
error("Terminal existed is not support . please set g.asynctasks_term_reuse = 1")
else
vim.notify("Delete existing terminal", "info")
end
M._asyncrun_term:shutdown()
M._asyncrun_term = nil
end

M._asyncrun_term = nil
M._asyncrun_term_toggle = nil
end

function M.runner(opts)
M.reset()
M._asyncrun_term = terminal:new({
cmd = opts.cmd,
dir = opts.cwd,
close_on_exit = false,
close_on_exit = (opts.close == "1") and true or false,
hidden = true,
clear_env = M._clear_env or false,
on_open = function(term)
if M._start_in_insert then
vim.cmd("startinsert!")
else
vim.cmd("stopinsert!")
vim.cmd("stopinsert")
end
end
end,
on_exit = function(term, job_id, exit_code, event_name)
vim.g.asyncrun_code = exit_code
vim.cmd("doautocmd User AsyncRunStop")
end,
})

function M._asyncrun_term_toggle()
Expand All @@ -43,11 +46,14 @@ function M.runner(opts)

if not opts.silent then
M._asyncrun_term_toggle()
if M._go_back then
vim.cmd("wincmd p")
end
end

if M._asyncrun_mapping then
vim.api.nvim_set_keymap("n", M._asyncrun_mapping,
"<cmd>lua require('asyncrun_toggleterm')._asyncrun_term_toggle()<CR>", {
"<cmd>lua require('asyncrun.toggleterm2')._asyncrun_term_toggle()<CR>", {
noremap = true,
silent = true
})
Expand Down

0 comments on commit 410ef9f

Please sign in to comment.