feat(util): open terminal commands in a float

This commit is contained in:
Folke Lemaitre 2022-12-24 11:26:53 +01:00
parent 7c2eb15444
commit 8ad05feef1
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 33 additions and 2 deletions

View File

@ -16,8 +16,7 @@ end
function M.open(uri) function M.open(uri)
if M.file_exists(uri) then if M.file_exists(uri) then
vim.cmd.split() return M.float({ win_opts = { style = "" }, file = uri })
return vim.cmd.view(uri)
end end
local cmd local cmd
if vim.fn.has("win32") == 1 then if vim.fn.has("win32") == 1 then
@ -84,6 +83,38 @@ function M.throttle(ms, fn)
end end
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? ---@return string?
function M.head(file) function M.head(file)
local f = io.open(file) local f = io.open(file)