mirror of https://github.com/folke/lazy.nvim.git
refactor: split open_cmd in float_cmd and float_term
This commit is contained in:
parent
13af39b83e
commit
e89e938991
|
@ -72,21 +72,15 @@ M.defaults = {
|
||||||
|
|
||||||
-- open lazygit log
|
-- open lazygit log
|
||||||
["<localleader>l"] = function(plugin)
|
["<localleader>l"] = function(plugin)
|
||||||
require("lazy.util").open_cmd({ "lazygit", "log" }, {
|
require("lazy.util").float_term({ "lazygit", "log" }, {
|
||||||
cwd = plugin.dir,
|
cwd = plugin.dir,
|
||||||
terminal = true,
|
|
||||||
close_on_exit = true,
|
|
||||||
enter = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
|
|
||||||
-- open a terminal for the plugin dir
|
-- open a terminal for the plugin dir
|
||||||
["<localleader>t"] = function(plugin)
|
["<localleader>t"] = function(plugin)
|
||||||
require("lazy.util").open_cmd({ vim.go.shell }, {
|
require("lazy.util").float_term(nil, {
|
||||||
cwd = plugin.dir,
|
cwd = plugin.dir,
|
||||||
terminal = true,
|
|
||||||
close_on_exit = true,
|
|
||||||
enter = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
|
@ -93,42 +93,53 @@ function M.throttle(ms, fn)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class LazyCmdOpts
|
---@class LazyCmdOptions: LazyFloatOptions
|
||||||
---@field cwd? string
|
---@field cwd? string
|
||||||
---@field env? table<string,string>
|
---@field env? table<string,string>
|
||||||
---@field float? LazyFloatOptions
|
---@field float? LazyFloatOptions
|
||||||
|
|
||||||
---@param cmd string[]
|
-- Opens a floating terminal (interactive by default)
|
||||||
---@param opts? {cwd:string, filetype:string, terminal?:boolean, close_on_exit?:boolean, enter?:boolean, float?:LazyFloatOptions}
|
---@param cmd? string[]|string
|
||||||
function M.open_cmd(cmd, opts)
|
---@param opts? LazyCmdOptions|{interactive?:boolean}
|
||||||
|
function M.float_term(cmd, opts)
|
||||||
|
cmd = cmd or {}
|
||||||
|
if type(cmd) == "string" then
|
||||||
|
cmd = { cmd }
|
||||||
|
end
|
||||||
|
if #cmd == 0 then
|
||||||
|
cmd = { vim.env.SHELL or vim.o.shell }
|
||||||
|
end
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
local float = M.float(opts.float)
|
local float = M.float(opts)
|
||||||
|
vim.fn.termopen(cmd, vim.tbl_isempty(opts) and vim.empty_dict() or opts)
|
||||||
|
if opts.interactive ~= false then
|
||||||
|
vim.cmd.startinsert()
|
||||||
|
vim.api.nvim_create_autocmd("TermClose", {
|
||||||
|
once = true,
|
||||||
|
buffer = float.buf,
|
||||||
|
callback = function()
|
||||||
|
float:close()
|
||||||
|
vim.cmd.checktime()
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
return float
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Runs the command and shows it in a floating window
|
||||||
|
---@param cmd string[]
|
||||||
|
---@param opts? LazyCmdOptions|{filetype?:string}
|
||||||
|
function M.float_cmd(cmd, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
local float = M.float(opts)
|
||||||
if opts.filetype then
|
if opts.filetype then
|
||||||
vim.bo[float.buf].filetype = opts.filetype
|
vim.bo[float.buf].filetype = opts.filetype
|
||||||
end
|
end
|
||||||
if opts.terminal then
|
local Process = require("lazy.manage.process")
|
||||||
opts.terminal = nil
|
local lines = Process.exec(cmd, { cwd = opts.cwd })
|
||||||
vim.fn.termopen(cmd, opts)
|
vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines)
|
||||||
if opts.enter then
|
vim.bo[float.buf].modifiable = false
|
||||||
vim.cmd.startinsert()
|
return float
|
||||||
end
|
|
||||||
if opts.close_on_exit then
|
|
||||||
vim.api.nvim_create_autocmd("TermClose", {
|
|
||||||
once = true,
|
|
||||||
buffer = float.buf,
|
|
||||||
callback = function()
|
|
||||||
float:close()
|
|
||||||
vim.cmd.checktime()
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local Process = require("lazy.manage.process")
|
|
||||||
local lines = Process.exec(cmd, { cwd = opts.cwd })
|
|
||||||
vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines)
|
|
||||||
vim.bo[float.buf].modifiable = false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return string?
|
---@return string?
|
||||||
|
|
|
@ -43,7 +43,7 @@ M.handlers = {
|
||||||
cmd[#cmd + 1] = diff.from
|
cmd[#cmd + 1] = diff.from
|
||||||
cmd[#cmd + 1] = diff.to
|
cmd[#cmd + 1] = diff.to
|
||||||
end
|
end
|
||||||
Util.open_cmd(cmd, { cwd = plugin.dir, filetype = "git" })
|
Util.float_cmd(cmd, { cwd = plugin.dir, filetype = "git" })
|
||||||
end,
|
end,
|
||||||
|
|
||||||
---@type LazyDiffFun
|
---@type LazyDiffFun
|
||||||
|
@ -57,7 +57,7 @@ M.handlers = {
|
||||||
cmd[#cmd + 1] = diff.from
|
cmd[#cmd + 1] = diff.from
|
||||||
cmd[#cmd + 1] = diff.to
|
cmd[#cmd + 1] = diff.to
|
||||||
end
|
end
|
||||||
Util.open_cmd(cmd, { cwd = plugin.dir, terminal = true, env = { PAGER = "cat" } })
|
Util.float_term(cmd, { cwd = plugin.dir, interactive = false, env = { PAGER = "cat" } })
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue