2023-01-24 07:52:03 +00:00
|
|
|
local utils = require('go.utils')
|
2021-11-23 23:13:40 +00:00
|
|
|
local api = vim.api
|
2023-01-25 05:09:13 +00:00
|
|
|
local log = utils.log
|
2023-11-08 06:03:33 +00:00
|
|
|
local is_windows = utils.is_windows()
|
2023-01-24 07:52:03 +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
|
2023-01-24 07:52:03 +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()
|
2022-03-23 02:07:52 +00:00
|
|
|
local cur_buf = api.nvim_get_current_buf()
|
2023-01-24 07:52:03 +00:00
|
|
|
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)
|
2022-05-01 22:47:45 +00:00
|
|
|
close_float_terminal()
|
|
|
|
|
2023-01-24 07:52:03 +00:00
|
|
|
local columns = api.nvim_get_option('columns')
|
|
|
|
local lines = api.nvim_get_option('lines')
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2022-03-23 02:07:52 +00:00
|
|
|
local cur_buf = api.nvim_get_current_buf()
|
2021-11-23 23:13:40 +00:00
|
|
|
local win_width, win_height
|
2023-01-25 05:09:13 +00:00
|
|
|
local wratio = _GO_NVIM_CFG.floaterm.width
|
|
|
|
local hratio = _GO_NVIM_CFG.floaterm.height
|
|
|
|
local position = _GO_NVIM_CFG.floaterm.posititon
|
|
|
|
local l = 0.98
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2023-01-25 05:09:13 +00:00
|
|
|
if position == 'center' then -- center
|
|
|
|
log('center')
|
|
|
|
opts.x = (columns - math.ceil(columns * wratio)) / 2
|
|
|
|
opts.y = (lines - math.ceil(lines * hratio)) / 2
|
|
|
|
win_height = math.ceil(lines * hratio)
|
|
|
|
win_width = math.ceil(columns * wratio)
|
|
|
|
log(opts, win_height, win_width)
|
|
|
|
elseif position == 'top' then
|
|
|
|
opts.x = 1
|
|
|
|
opts.y = 1
|
|
|
|
win_height = math.ceil(lines * hratio)
|
|
|
|
win_width = math.ceil(columns * l)
|
|
|
|
elseif position == 'bottom' then
|
|
|
|
opts.x = 1
|
|
|
|
opts.y = lines - math.ceil(lines * hratio)
|
|
|
|
win_height = math.ceil(lines * hratio)
|
|
|
|
win_width = math.ceil(columns * l)
|
|
|
|
elseif position == 'left' then
|
|
|
|
opts.x = 1
|
|
|
|
opts.y = 1
|
|
|
|
win_height = math.ceil(lines * l)
|
|
|
|
win_width = math.ceil(columns * wratio)
|
|
|
|
elseif position == 'right' then
|
|
|
|
opts.x = columns - math.ceil(columns * wratio)
|
|
|
|
opts.y = 1
|
|
|
|
win_height = math.ceil(lines * l)
|
|
|
|
win_width = math.ceil(columns * wratio)
|
|
|
|
else -- default to auto
|
|
|
|
if columns > 120 then
|
|
|
|
-- split in right
|
|
|
|
wratio = wratio
|
|
|
|
win_height = math.ceil(lines * l)
|
|
|
|
win_width = math.ceil(columns * wratio)
|
|
|
|
win_width = math.max(80, win_width)
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2023-01-25 05:09:13 +00:00
|
|
|
opts.y = win_height
|
|
|
|
opts.x = columns - win_width
|
|
|
|
elseif lines > 40 then -- bottom
|
|
|
|
win_height = math.ceil(lines * wratio)
|
|
|
|
win_width = math.ceil(columns * l)
|
|
|
|
|
|
|
|
opts.y = lines - win_height
|
|
|
|
opts.x = 1
|
|
|
|
else
|
|
|
|
win_height = math.ceil(lines * l)
|
|
|
|
win_width = math.ceil(columns * l)
|
|
|
|
|
|
|
|
opts.y = 1
|
|
|
|
opts.x = 1
|
|
|
|
end
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
2023-01-25 05:09:13 +00:00
|
|
|
|
2021-11-23 23:13:40 +00:00
|
|
|
opts.win_height = opts.win_height or win_height
|
|
|
|
opts.win_width = opts.win_width or win_width
|
2023-01-24 07:52:03 +00:00
|
|
|
opts.border = opts.border or 'single'
|
2023-10-31 05:30:38 +00:00
|
|
|
opts.title_colors = _GO_NVIM_CFG.floaterm.title_colors
|
2021-11-23 23:13:40 +00:00
|
|
|
if opts.autoclose == nil then
|
|
|
|
opts.autoclose = true
|
|
|
|
end
|
2022-07-02 02:33:20 +00:00
|
|
|
-- run in neovim shell
|
2023-11-08 06:03:33 +00:00
|
|
|
local cmdstr = opts.cmd or 'go'
|
2023-01-24 07:52:03 +00:00
|
|
|
if type(opts.cmd) == 'table' then
|
2023-11-08 06:03:33 +00:00
|
|
|
cmdstr = table.concat(opts.cmd, ' ')
|
2022-07-02 02:33:20 +00:00
|
|
|
end
|
2023-11-08 06:03:33 +00:00
|
|
|
-- convert to string for linux like systems
|
|
|
|
if not is_windows then
|
|
|
|
opts.cmd = cmdstr
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.title = opts.title or cmdstr
|
|
|
|
assert(opts.title ~= nil, 'title is nil' .. cmdstr)
|
|
|
|
opts.title = (opts.title):sub(1, win_width - 4)
|
2023-01-24 07:52:03 +00:00
|
|
|
|
2022-07-02 02:33:20 +00:00
|
|
|
utils.log(opts)
|
2021-11-23 23:13:40 +00:00
|
|
|
local buf, win, closer = guihua_term.floating_term(opts)
|
2023-01-24 07:52:03 +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
|
|
|
|
|
2023-11-08 06:03:33 +00:00
|
|
|
-- term({ cmd = { 'go', 'list' }, autoclose = false })
|
2022-01-10 04:47:21 +00:00
|
|
|
return { run = term, close = close_float_terminal }
|