Skip to content

Commit e89e938

Browse files
committed
refactor: split open_cmd in float_cmd and float_term
1 parent 13af39b commit e89e938

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

lua/lazy/core/config.lua

+2-8
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,15 @@ M.defaults = {
7272

7373
-- open lazygit log
7474
["<localleader>l"] = function(plugin)
75-
require("lazy.util").open_cmd({ "lazygit", "log" }, {
75+
require("lazy.util").float_term({ "lazygit", "log" }, {
7676
cwd = plugin.dir,
77-
terminal = true,
78-
close_on_exit = true,
79-
enter = true,
8077
})
8178
end,
8279

8380
-- open a terminal for the plugin dir
8481
["<localleader>t"] = function(plugin)
85-
require("lazy.util").open_cmd({ vim.go.shell }, {
82+
require("lazy.util").float_term(nil, {
8683
cwd = plugin.dir,
87-
terminal = true,
88-
close_on_exit = true,
89-
enter = true,
9084
})
9185
end,
9286
},

lua/lazy/util.lua

+38-27
Original file line numberDiff line numberDiff line change
@@ -93,42 +93,53 @@ function M.throttle(ms, fn)
9393
end
9494
end
9595

96-
---@class LazyCmdOpts
96+
---@class LazyCmdOptions: LazyFloatOptions
9797
---@field cwd? string
9898
---@field env? table<string,string>
9999
---@field float? LazyFloatOptions
100100

101-
---@param cmd string[]
102-
---@param opts? {cwd:string, filetype:string, terminal?:boolean, close_on_exit?:boolean, enter?:boolean, float?:LazyFloatOptions}
103-
function M.open_cmd(cmd, opts)
101+
-- Opens a floating terminal (interactive by default)
102+
---@param cmd? string[]|string
103+
---@param opts? LazyCmdOptions|{interactive?:boolean}
104+
function M.float_term(cmd, opts)
105+
cmd = cmd or {}
106+
if type(cmd) == "string" then
107+
cmd = { cmd }
108+
end
109+
if #cmd == 0 then
110+
cmd = { vim.env.SHELL or vim.o.shell }
111+
end
104112
opts = opts or {}
105-
local float = M.float(opts.float)
113+
local float = M.float(opts)
114+
vim.fn.termopen(cmd, vim.tbl_isempty(opts) and vim.empty_dict() or opts)
115+
if opts.interactive ~= false then
116+
vim.cmd.startinsert()
117+
vim.api.nvim_create_autocmd("TermClose", {
118+
once = true,
119+
buffer = float.buf,
120+
callback = function()
121+
float:close()
122+
vim.cmd.checktime()
123+
end,
124+
})
125+
end
126+
return float
127+
end
106128

129+
--- Runs the command and shows it in a floating window
130+
---@param cmd string[]
131+
---@param opts? LazyCmdOptions|{filetype?:string}
132+
function M.float_cmd(cmd, opts)
133+
opts = opts or {}
134+
local float = M.float(opts)
107135
if opts.filetype then
108136
vim.bo[float.buf].filetype = opts.filetype
109137
end
110-
if opts.terminal then
111-
opts.terminal = nil
112-
vim.fn.termopen(cmd, opts)
113-
if opts.enter then
114-
vim.cmd.startinsert()
115-
end
116-
if opts.close_on_exit then
117-
vim.api.nvim_create_autocmd("TermClose", {
118-
once = true,
119-
buffer = float.buf,
120-
callback = function()
121-
float:close()
122-
vim.cmd.checktime()
123-
end,
124-
})
125-
end
126-
else
127-
local Process = require("lazy.manage.process")
128-
local lines = Process.exec(cmd, { cwd = opts.cwd })
129-
vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines)
130-
vim.bo[float.buf].modifiable = false
131-
end
138+
local Process = require("lazy.manage.process")
139+
local lines = Process.exec(cmd, { cwd = opts.cwd })
140+
vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines)
141+
vim.bo[float.buf].modifiable = false
142+
return float
132143
end
133144

134145
---@return string?

lua/lazy/view/diff.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ M.handlers = {
4343
cmd[#cmd + 1] = diff.from
4444
cmd[#cmd + 1] = diff.to
4545
end
46-
Util.open_cmd(cmd, { cwd = plugin.dir, filetype = "git" })
46+
Util.float_cmd(cmd, { cwd = plugin.dir, filetype = "git" })
4747
end,
4848

4949
---@type LazyDiffFun
@@ -57,7 +57,7 @@ M.handlers = {
5757
cmd[#cmd + 1] = diff.from
5858
cmd[#cmd + 1] = diff.to
5959
end
60-
Util.open_cmd(cmd, { cwd = plugin.dir, terminal = true, env = { PAGER = "cat" } })
60+
Util.float_term(cmd, { cwd = plugin.dir, interactive = false, env = { PAGER = "cat" } })
6161
end,
6262
}
6363

0 commit comments

Comments
 (0)