terminal.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. M.config = function()
  4. lvim.builtin["terminal"] = {
  5. active = true,
  6. on_config_done = nil,
  7. -- size can be a number or function which is passed the current terminal
  8. size = 20,
  9. open_mapping = [[<c-\>]],
  10. hide_numbers = true, -- hide the number column in toggleterm buffers
  11. shade_filetypes = {},
  12. shade_terminals = true,
  13. shading_factor = 2, -- the degree by which to darken to terminal colour, default: 1 for dark backgrounds, 3 for light
  14. start_in_insert = true,
  15. insert_mappings = true, -- whether or not the open mapping applies in insert mode
  16. persist_size = false,
  17. -- direction = 'vertical' | 'horizontal' | 'window' | 'float',
  18. direction = "float",
  19. close_on_exit = true, -- close the terminal window when the process exits
  20. shell = vim.o.shell, -- change the default shell
  21. -- This field is only relevant if direction is set to 'float'
  22. float_opts = {
  23. -- The border key is *almost* the same as 'nvim_win_open'
  24. -- see :h nvim_win_open for details on borders however
  25. -- the 'curved' border is a custom border type
  26. -- not natively supported but implemented in this plugin.
  27. -- border = 'single' | 'double' | 'shadow' | 'curved' | ... other options supported by win open
  28. border = "curved",
  29. -- width = <value>,
  30. -- height = <value>,
  31. winblend = 0,
  32. highlights = {
  33. border = "Normal",
  34. background = "Normal",
  35. },
  36. },
  37. -- Add executables on the config.lua
  38. -- { cmd, keymap, description, direction, size }
  39. -- lvim.builtin.terminal.execs = {...} to overwrite
  40. -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"}
  41. -- TODO: pls add mappings in which key and refactor this
  42. execs = {
  43. { nil, "<M-1>", "Horizontal Terminal", "horizontal", 0.3 },
  44. { nil, "<M-2>", "Vertical Terminal", "vertical", 0.4 },
  45. { nil, "<M-3>", "Float Terminal", "float", nil },
  46. },
  47. }
  48. end
  49. --- Get current buffer size
  50. ---@return {width: number, height: number}
  51. local function get_buf_size()
  52. local cbuf = vim.api.nvim_get_current_buf()
  53. local bufinfo = vim.tbl_filter(function(buf)
  54. return buf.bufnr == cbuf
  55. end, vim.fn.getwininfo(vim.api.nvim_get_current_win()))[1]
  56. if bufinfo == nil then
  57. return { width = -1, height = -1 }
  58. end
  59. return { width = bufinfo.width, height = bufinfo.height }
  60. end
  61. --- Get the dynamic terminal size in cells
  62. ---@param direction number
  63. ---@param size number
  64. ---@return integer
  65. local function get_dynamic_terminal_size(direction, size)
  66. size = size or lvim.builtin.terminal.size
  67. if direction ~= "float" and tostring(size):find(".", 1, true) then
  68. size = math.min(size, 1.0)
  69. local buf_sizes = get_buf_size()
  70. local buf_size = direction == "horizontal" and buf_sizes.height or buf_sizes.width
  71. return buf_size * size
  72. else
  73. return size
  74. end
  75. end
  76. M.init = function()
  77. for i, exec in pairs(lvim.builtin.terminal.execs) do
  78. local direction = exec[4] or lvim.builtin.terminal.direction
  79. local opts = {
  80. cmd = exec[1] or lvim.builtin.terminal.shell,
  81. keymap = exec[2],
  82. label = exec[3],
  83. -- NOTE: unable to consistently bind id/count <= 9, see #2146
  84. count = i + 100,
  85. direction = direction,
  86. size = function()
  87. return get_dynamic_terminal_size(direction, exec[5])
  88. end,
  89. }
  90. M.add_exec(opts)
  91. end
  92. end
  93. M.setup = function()
  94. local terminal = require "toggleterm"
  95. terminal.setup(lvim.builtin.terminal)
  96. if lvim.builtin.terminal.on_config_done then
  97. lvim.builtin.terminal.on_config_done(terminal)
  98. end
  99. end
  100. M.add_exec = function(opts)
  101. local binary = opts.cmd:match "(%S+)"
  102. if vim.fn.executable(binary) ~= 1 then
  103. Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.")
  104. return
  105. end
  106. vim.keymap.set({ "n", "t" }, opts.keymap, function()
  107. M._exec_toggle { cmd = opts.cmd, count = opts.count, direction = opts.direction, size = opts.size() }
  108. end, { desc = opts.label, noremap = true, silent = true })
  109. end
  110. M._exec_toggle = function(opts)
  111. local Terminal = require("toggleterm.terminal").Terminal
  112. local term = Terminal:new { cmd = opts.cmd, count = opts.count, direction = opts.direction }
  113. term:toggle(opts.size, opts.direction)
  114. end
  115. ---Toggles a log viewer according to log.viewer.layout_config
  116. ---@param logfile string the fullpath to the logfile
  117. M.toggle_log_view = function(logfile)
  118. local log_viewer = lvim.log.viewer.cmd
  119. if vim.fn.executable(log_viewer) ~= 1 then
  120. log_viewer = "less +F"
  121. end
  122. Log:debug("attempting to open: " .. logfile)
  123. log_viewer = log_viewer .. " " .. logfile
  124. local term_opts = vim.tbl_deep_extend("force", lvim.builtin.terminal, {
  125. cmd = log_viewer,
  126. open_mapping = lvim.log.viewer.layout_config.open_mapping,
  127. direction = lvim.log.viewer.layout_config.direction,
  128. -- TODO: this might not be working as expected
  129. size = lvim.log.viewer.layout_config.size,
  130. float_opts = lvim.log.viewer.layout_config.float_opts,
  131. })
  132. local Terminal = require("toggleterm.terminal").Terminal
  133. local log_view = Terminal:new(term_opts)
  134. log_view:toggle()
  135. end
  136. M.lazygit_toggle = function()
  137. local Terminal = require("toggleterm.terminal").Terminal
  138. local lazygit = Terminal:new {
  139. cmd = "lazygit",
  140. hidden = true,
  141. direction = "float",
  142. float_opts = {
  143. border = "none",
  144. width = 100000,
  145. height = 100000,
  146. },
  147. on_open = function(_)
  148. vim.cmd "startinsert!"
  149. end,
  150. on_close = function(_) end,
  151. count = 99,
  152. }
  153. lazygit:toggle()
  154. end
  155. return M