aboutsummaryrefslogtreecommitdiff
path: root/lua/nvterm/termutil.lua
blob: c6d8614ce1bc9657f5bda47f09b5604f9ab1976c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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