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 -- 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,
}, },

View File

@ -93,27 +93,27 @@ 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.filetype then if opts.interactive ~= false 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() vim.cmd.startinsert()
end
if opts.close_on_exit then
vim.api.nvim_create_autocmd("TermClose", { vim.api.nvim_create_autocmd("TermClose", {
once = true, once = true,
buffer = float.buf, buffer = float.buf,
@ -123,12 +123,23 @@ function M.open_cmd(cmd, opts)
end, end,
}) })
end end
else 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
local Process = require("lazy.manage.process") local Process = require("lazy.manage.process")
local lines = Process.exec(cmd, { cwd = opts.cwd }) local lines = Process.exec(cmd, { cwd = opts.cwd })
vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines) vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines)
vim.bo[float.buf].modifiable = false vim.bo[float.buf].modifiable = false
end return float
end end
---@return string? ---@return string?

View File

@ -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,
} }