Created
December 5, 2020 12:56
-
-
Save numToStr/95648e124129733c7fcbc9154ec3163b to your computer and use it in GitHub Desktop.
Revisions
-
numToStr created this gist
Dec 5, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,109 @@ local api = vim.api local cmd = vim.cmd local fn = vim.fn local W = {} local base_opt = { relative = 'editor', style = 'minimal' } cmd('hi def link FTerm Normal') function create_borders(height, width) local border_lines = { '┌' .. string.rep('─', width) .. '┐' } local middle_line = '|' .. string.rep(' ', width) .. '|' for i=1, height do table.insert(border_lines, middle_line) end table.insert(border_lines, '└' .. string.rep('─', width) .. '┘') return border_lines end function open_float() vim.g.last_win = fn.nvim_get_current_win() vim.g.last_buf = fn.nvim_get_current_buf() vim.g.last_pos = fn.winsaveview() -- get dimensions local width = api.nvim_get_option("columns") local height = api.nvim_get_option("lines") -- calculate our floating window size local win_width = math.ceil(width * 0.8) local win_height = math.ceil(height * 0.8 - 4) -- and its starting position local col = math.ceil((width - win_width) / 2) local row = math.ceil((height - win_height) / 2 - 1) local opt = vim.tbl_extend('keep', base_opt, { width = win_width, height = win_height, col = col, row = row, }) local padding_opt = vim.tbl_extend('keep', base_opt, { width = win_width + 2, height = win_height + 2, col = col - 1, row = row - 1, }) local padding_buf = api.nvim_create_buf(false, true) local borders = create_borders(win_height, win_width) api.nvim_buf_set_lines(padding_buf, 0, -1, false, borders) local padding_win = api.nvim_open_win(padding_buf, true, padding_opt) fn.nvim_win_set_option(padding_win, 'winhl', 'Normal:FTerm') local buf = api.nvim_create_buf(false, true) local win = api.nvim_open_win(buf, true, opt) fn.nvim_win_set_option(win, 'winhl', 'Normal:FTerm') fn.termopen(os.getenv('SHELL')) cmd("startinsert") cmd("autocmd! TermClose <buffer> lua close_float()") W = { win, padding_win } end function close_float() if next(W) == nil then do return end end for _, win in ipairs(W) do if api.nvim_win_is_valid(win) then api.nvim_win_close(win, {}) end end W = {} end function toggle_float() if next(W) == nil then open_float() do return end end close_float() if next(vim.g.last_pos) then -- cmd("wincmd l") -- fn.setpos('.', last_pos) -- cmd("keepalt b"..last_buf) -- fn.winrestview(vim.g.last_pos) -- last_pos = nil -- print(vim.inspect(vim.g.last_win)) -- cmd(string.format('exe %d . "wincmd c"', vim.g.last_win)) end -- print(vim.inspect(W)) end cmd [[ command! WinOpen lua open_float() ]] cmd [[ command! WinToggle lua toggle_float() ]] cmd [[ command! WinClose lua close_float() ]]