go.nvim/lua/go/term.lua

66 lines
2.0 KiB
Lua
Raw Normal View History

2022-01-10 04:47:21 +00:00
local utils = require("go.utils")
2021-11-23 23:13:40 +00:00
local api = vim.api
2022-01-10 04:47:21 +00:00
local guihua_term = utils.load_plugin("guihua.lua", "guihua.floating")
2021-11-23 23:13:40 +00:00
if not guihua_term then
2022-01-10 04:47:21 +00:00
utils.warn("guihua not installed, please install ray-x/guihua.lua for GUI functions")
2021-11-23 23:13:40 +00:00
end
local function close_float_terminal()
local cur_buf = api.nvim_get_current_buf()
local has_var, float_term_win = pcall(api.nvim_buf_get_var, cur_buf, "go_float_terminal_win")
2021-11-23 23:13:40 +00:00
if not has_var then
return
end
if float_term_win[1] ~= nil and api.nvim_buf_is_valid(float_term_win[1]) then
2022-01-10 04:47:21 +00:00
api.nvim_buf_delete(float_term_win[1], { force = true })
2021-11-23 23:13:40 +00:00
end
if float_term_win[2] ~= nil and api.nvim_win_is_valid(float_term_win[2]) then
api.nvim_win_close(float_term_win[2], true)
end
end
local term = function(opts)
close_float_terminal()
2021-11-23 23:13:40 +00:00
local columns = api.nvim_get_option("columns")
local lines = api.nvim_get_option("lines")
local cur_buf = api.nvim_get_current_buf()
2021-11-23 23:13:40 +00:00
local win_width, win_height
2022-01-10 04:47:21 +00:00
if columns > 140 then
2021-11-23 23:13:40 +00:00
-- split in right
win_height = math.ceil(lines * 0.98)
2022-05-31 06:16:09 +00:00
win_width = math.ceil(columns * 0.45)
win_width = math.max(80, win_width)
2021-11-23 23:13:40 +00:00
opts.y = win_height
opts.x = columns - win_width
else
2022-05-31 06:16:09 +00:00
win_height = math.ceil(lines * 0.45)
2021-11-23 23:13:40 +00:00
win_width = math.ceil(columns * 0.98)
opts.y = opts.y or lines - win_height
opts.x = opts.x or 1
end
opts.win_height = opts.win_height or win_height
opts.win_width = opts.win_width or win_width
2022-01-10 04:47:21 +00:00
opts.border = opts.border or "single"
2021-11-23 23:13:40 +00:00
if opts.autoclose == nil then
opts.autoclose = true
end
-- run in neovim shell
if type(opts.cmd) == "table" then
opts.cmd = table.concat(opts.cmd, " ")
end
utils.log(opts)
2021-11-23 23:13:40 +00:00
local buf, win, closer = guihua_term.floating_term(opts)
2022-01-10 04:47:21 +00:00
api.nvim_command("setlocal nobuflisted")
api.nvim_buf_set_var(cur_buf, "go_float_terminal_win", { buf, win })
api.nvim_buf_set_var(cur_buf, "shellcmdflag", "shell-unquoting")
2021-11-23 23:13:40 +00:00
return buf, win, closer
end
-- term({cmd = 'echo abddeefsfsafd', autoclose = false})
2022-01-10 04:47:21 +00:00
return { run = term, close = close_float_terminal }