From 9cabfafc08a1cd9e83762896e0eac5299ceed29b Mon Sep 17 00:00:00 2001 From: zbirenbaum Date: Thu, 21 Apr 2022 23:50:43 -0400 Subject: initial update --- lua/chadterm/termutil.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lua/chadterm/termutil.lua (limited to 'lua/chadterm/termutil.lua') diff --git a/lua/chadterm/termutil.lua b/lua/chadterm/termutil.lua new file mode 100644 index 0000000..92fc632 --- /dev/null +++ b/lua/chadterm/termutil.lua @@ -0,0 +1,51 @@ +local util = {} +local a = vim.api + +util.calc_float_opts = function(opts) + return { + relative = "editor", + width = math.ceil(opts.width*vim.o.columns), + height = math.ceil(opts.height*vim.o.lines), + row = math.floor(opts.row*vim.o.lines), + col = math.floor(opts.col*vim.o.columns), + border = opts.border, + } +end + +util.get_split_dims = function(type, ratio) + local type_switch = type == "horizontal" + local type_func = type_switch and a.nvim_win_get_height or a.nvim_win_get_width + return math.floor(type_func(0) * ratio) +end + +util.execute_type_cmd = function(type, terminals) + local opts = terminals.type_opts[type] + local type_cmds = { + horizontal = function() + local dims = util.get_split_dims(type, opts.split_ratio) + vim.cmd(opts.location .. dims .. " split") + end, + vertical = function() + local dims = util.get_split_dims(type, opts.split_ratio) + vim.cmd(opts.location .. dims .. " vsplit") + end, + float = function() + a.nvim_open_win(0, true, util.calc_float_opts(opts)) + end, + } + type_cmds[type]() +end + + +util.verify_terminals = function (terminals) + terminals.list = vim.tbl_filter(function(term) + return vim.api.nvim_buf_is_valid(term.buf) + end, terminals.list) + terminals.list = vim.tbl_map(function(term) + term.open = vim.api.nvim_win_is_valid(term.win) + return term + end, terminals.list) + return terminals +end + +return util -- cgit v1.2.3-70-g09d2