From e89e938991701cef7b9ace1913a7251c4c0fa46c Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Thu, 5 Jan 2023 17:36:01 +0100 Subject: [PATCH] refactor: split open_cmd in float_cmd and float_term --- lua/lazy/core/config.lua | 10 ++----- lua/lazy/util.lua | 65 +++++++++++++++++++++++----------------- lua/lazy/view/diff.lua | 4 +-- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/lua/lazy/core/config.lua b/lua/lazy/core/config.lua index d2b3146..f5441e0 100644 --- a/lua/lazy/core/config.lua +++ b/lua/lazy/core/config.lua @@ -72,21 +72,15 @@ M.defaults = { -- open lazygit log ["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 ["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, }, diff --git a/lua/lazy/util.lua b/lua/lazy/util.lua index d1b4257..edd6610 100644 --- a/lua/lazy/util.lua +++ b/lua/lazy/util.lua @@ -93,42 +93,53 @@ function M.throttle(ms, fn) end end ----@class LazyCmdOpts +---@class LazyCmdOptions: LazyFloatOptions ---@field cwd? string ---@field env? table ---@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? diff --git a/lua/lazy/view/diff.lua b/lua/lazy/view/diff.lua index 63dc2e7..24955b8 100644 --- a/lua/lazy/view/diff.lua +++ b/lua/lazy/view/diff.lua @@ -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, }