Trans.nvim/lua/Trans/core/show_win.lua

55 lines
1.4 KiB
Lua
Raw Normal View History

2023-01-09 21:30:16 +08:00
local type_check = require("Trans.util.debug").type_check
-- local win_opts = {
-- winhl = 'Normal:TransWinNormal, FloatBorder:TransWinBorder'
-- }
local function caculate_format(height, width)
local col = math.floor((vim.o.lines - height - vim.o.cmdheight) / 2)
local row = math.floor((vim.o.columns - width) / 2)
return row, col
end
2023-01-09 23:20:56 +08:00
2023-01-09 21:30:16 +08:00
local function show_win(opts)
type_check {
opts = { opts, 'table' },
win = { opts.win, 'table' },
2023-01-09 23:20:56 +08:00
border = { opts.border, 'string' },
2023-01-09 21:30:16 +08:00
highlight = { opts.highlight, 'table', true },
}
local is_float = opts.style == 'float'
local win_opts = {
relative = opts.style == 'float' and 'editor' or 'cursor',
width = opts.width,
height = opts.height,
style = 'minimal',
2023-01-09 23:20:56 +08:00
border = opts.border,
2023-01-09 21:30:16 +08:00
title = 'Trans',
title_pos = 'center',
focusable = true,
zindex = 100,
}
if is_float then
win_opts.row, win_opts.col = caculate_format(win_opts.height, win_opts.width)
else
win_opts.row = 2
win_opts.col = 2
end
2023-01-09 23:20:56 +08:00
2023-01-09 21:30:16 +08:00
local winid = vim.api.nvim_open_win(bufnr, is_float, win_opts)
2023-01-09 23:20:56 +08:00
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, opts.lines)
for line, l_hl in ipairs(opts.highlight) do
for i, hl in ipairs(l_hl) do
vim.api.nvim_buf_add_highlight(bufnr, line, hl.name, i, hl._start, hl._end)
end
end
2023-01-09 21:30:16 +08:00
return bufnr, winid
end
return show_win