refactor: split open_cmd in float_cmd and float_term

This commit is contained in:
Folke Lemaitre 2023-01-05 17:36:01 +01:00
parent 13af39b83e
commit e89e938991
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 42 additions and 37 deletions

View File

@ -72,21 +72,15 @@ M.defaults = {
-- open lazygit log
["<localleader>l"] = function(plugin)
require("lazy.util").open_cmd({ "lazygit", "log" }, {
require("lazy.util").float_term({ "lazygit", "log" }, {
cwd = plugin.dir,
terminal = true,
close_on_exit = true,
enter = true,
})
end,
-- open a terminal for the plugin dir
["<localleader>t"] = function(plugin)
require("lazy.util").open_cmd({ vim.go.shell }, {
require("lazy.util").float_term(nil, {
cwd = plugin.dir,
terminal = true,
close_on_exit = true,
enter = true,
})
end,
},

View File

@ -93,42 +93,53 @@ function M.throttle(ms, fn)
end
end
---@class LazyCmdOpts
---@class LazyCmdOptions: LazyFloatOptions
---@field cwd? string
---@field env? table<string,string>
---@field float? LazyFloatOptions
---@param cmd string[]
---@param opts? {cwd:string, filetype:string, terminal?:boolean, close_on_exit?:boolean, enter?:boolean, float?:LazyFloatOptions}
function M.open_cmd(cmd, opts)
-- Opens a floating terminal (interactive by default)
---@param cmd? string[]|string
---@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 {}
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
vim.bo[float.buf].filetype = opts.filetype
end
if opts.terminal then
opts.terminal = nil
vim.fn.termopen(cmd, opts)
if opts.enter then
vim.cmd.startinsert()
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
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
return float
end
---@return string?

View File

@ -43,7 +43,7 @@ M.handlers = {
cmd[#cmd + 1] = diff.from
cmd[#cmd + 1] = diff.to
end
Util.open_cmd(cmd, { cwd = plugin.dir, filetype = "git" })
Util.float_cmd(cmd, { cwd = plugin.dir, filetype = "git" })
end,
---@type LazyDiffFun
@ -57,7 +57,7 @@ M.handlers = {
cmd[#cmd + 1] = diff.from
cmd[#cmd + 1] = diff.to
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,
}