62 lines
1.8 KiB
Lua
62 lines
1.8 KiB
Lua
local M = {}
|
|
|
|
function M.float_terminal(cmd, opts)
|
|
opts = opts or {}
|
|
local buf = vim.api.nvim_create_buf(false, true)
|
|
local width = math.floor(vim.o.columns * (opts.width_ratio or 0.8))
|
|
local height = math.floor(vim.o.lines * (opts.height_ratio or 0.5))
|
|
local row = math.floor((vim.o.lines - height) / 2)
|
|
local col = math.floor((vim.o.columns - width) / 2)
|
|
vim.api.nvim_open_win(buf, true, {
|
|
style = "minimal", relative = "editor",
|
|
width = width, height = height, row = row, col = col,
|
|
border = opts.border or "rounded",
|
|
-- Set the buffer for the new window
|
|
buf = buf,
|
|
})
|
|
|
|
-- --- START MODIFICATION ---
|
|
|
|
-- Format the command as a string for display
|
|
local cmd_display
|
|
if type(cmd) == 'table' then
|
|
cmd_display = table.concat(cmd, " ")
|
|
else
|
|
cmd_display = cmd
|
|
end
|
|
|
|
-- Write the command string to the top of the buffer
|
|
vim.api.nvim_buf_set_lines(buf, 0, 0, false, {
|
|
"--- RUNNING COMMAND: " .. cmd_display .. " ---",
|
|
"", -- Add an empty line for separation
|
|
})
|
|
|
|
-- --- END MODIFICATION ---
|
|
|
|
local function scroll_bottom()
|
|
local win = vim.api.nvim_get_current_win()
|
|
vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0})
|
|
end
|
|
|
|
-- We need to ensure the terminal starts in the correct buffer
|
|
vim.api.nvim_set_current_buf(buf)
|
|
|
|
local job = vim.fn.termopen(cmd, {
|
|
cwd = opts.cwd,
|
|
on_stdout = function(...) scroll_bottom() end,
|
|
on_stderr = function(...) scroll_bottom() end,
|
|
on_exit = function(_, code, _)
|
|
vim.schedule(function()
|
|
vim.notify(("command exited %d"):format(code), code == 0 and vim.log.levels.INFO or vim.log.levels.ERROR)
|
|
end)
|
|
end,
|
|
env = opts.env,
|
|
})
|
|
|
|
vim.keymap.set("n", "q", "<cmd>bd!<CR>", { buffer = buf, silent = true })
|
|
return job
|
|
end
|
|
|
|
|
|
return M
|