Skip to content

Commit

Permalink
feat(util): open terminal commands in a float
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 24, 2022
1 parent 7c2eb15 commit 8ad05fe
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions lua/lazy/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ end

function M.open(uri)
if M.file_exists(uri) then
vim.cmd.split()
return vim.cmd.view(uri)
return M.float({ win_opts = { style = "" }, file = uri })
end
local cmd
if vim.fn.has("win32") == 1 then
Expand Down Expand Up @@ -84,6 +83,38 @@ function M.throttle(ms, fn)
end
end

---@param cmd string[]
---@param opts? {cwd:string, filetype:string, terminal?:boolean, close_on_exit?:boolean, enter?:boolean}
function M.open_cmd(cmd, opts)
opts = opts or {}
local float = M.float()

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()
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

---@return string?
function M.head(file)
local f = io.open(file)
Expand Down

0 comments on commit 8ad05fe

Please sign in to comment.